Skip to content

Commit

Permalink
Using virtual ip as master
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezborder committed Nov 28, 2024
1 parent 6eb997a commit 647375b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 31 deletions.
24 changes: 0 additions & 24 deletions resources/libraries/helper.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
module Keepalived
module Helper
def get_master(virtual_ip, iface, managers)
sorted_managers = managers.map.with_index do |manager, index|
priority = 50 + managers.size - index
{
name: manager.name,
priority: priority,
}
end

top_nodes = sorted_managers.first(2).sort_by { |node| -node[:priority] }

if iface_has_virtual_ip?(iface, virtual_ip)
top_nodes[1][:name]
else
top_nodes[0][:name]
end
end

private

def iface_has_virtual_ip?(iface, virtual_ip)
iface_data = `ip addr show #{iface}`
iface_data.include?(virtual_ip)
end
end
end
5 changes: 2 additions & 3 deletions resources/providers/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# Provider:: config
#

include Keepalived::Helper

action :add do
begin
user = new_resource.user
Expand Down Expand Up @@ -75,6 +73,7 @@
group 'root'
mode '0755'
retries 2
variables(virtual_ip: postgresql_vrrp, iface: postgresql_iface)
end

template '/usr/lib/redborder/bin/rb_keepalived_backup_notify_postgresql.sh' do
Expand All @@ -84,7 +83,7 @@
group 'root'
mode '0755'
retries 2
variables(master_node: get_master(postgresql_vrrp, postgresql_iface, managers))
variables(virtual_ip: postgresql_vrrp, iface: postgresql_iface)
end

execute 'set_keepalived_permissive' do
Expand Down
4 changes: 4 additions & 0 deletions resources/templates/default/keepalived.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ vrrp_instance vi_<%= ip %> {
state BACKUP
interface <%= iface %>
virtual_router_id <%= ((@start_id.nil? ? 111 : @start_id.to_i) + index).to_s %>
<% if services.detect { |x| x["service"] == "postgresql" } %>
priority 100
<% else %>
priority <%= 50+@managers.size-@managers.index { |m| m.name == node.name } %>
<% end %>
advert_int 1
nopreempt
<% if !node["redborder"].nil? and !node["redborder"]["dmidecode"].nil? and !node["redborder"]["dmidecode"]["manufacturer"].nil? and node["redborder"]["dmidecode"]["manufacturer"].to_s.downcase == "xen" %>
Expand Down
33 changes: 30 additions & 3 deletions resources/templates/default/notify_backup_postgresql.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash

MASTER_NODE=<%= @master_node %>
VIRTUAL_IP=<%= @virtual_ip %>
IFACE=<%= @iface %>
NAME=postgresql
LOG_FILE=/tmp/rb_notify_postgresql_backup.log

# Cleaning tmp files
Expand All @@ -10,5 +12,30 @@ rm -f /tmp/rb_notify_postgresql*
date >> "$LOG_FILE"
echo "Executing rb_notify_backup_postgresql" >> "$LOG_FILE"

echo "Executing rb_sync_from_master.sh $MASTER_NODE" >> "$LOG_FILE"
/usr/lib/redborder/bin/rb_sync_from_master.sh $MASTER_NODE >> "$LOG_FILE"
grep vrrp_sync_group /etc/keepalived/keepalived.conf | grep -q $NAME
if [ $? -ne 0 ]; then
echo "$NAME has no virtual ip on this node" >> "$LOG_FILE"
exit 0
fi

# If the VIP is still up, then something is wrong
found=0
sleep 5 # wait 5 seconds to converge keepalived successfully

for i in $(seq 0 2); do
ip addr show dev $IFACE | grep "inet " | awk '{print $2}' | sed 's/\/.*//' | grep -q "^$VIRTUAL_IP$"
if [ $? -eq 0 ]; then
# found virtual ip address
found=1
break
fi
sleep 1
done

if [ $found -eq 1 ]; then
echo "found virtual ip $VIRTUAL_IP, doing nothing" >> "$LOG_FILE"
exit 0
fi

echo "Executing rb_sync_from_master.sh $VIRTUAL_IP" >> "$LOG_FILE"
/usr/lib/redborder/bin/rb_sync_from_master.sh $VIRTUAL_IP >> "$LOG_FILE"
29 changes: 28 additions & 1 deletion resources/templates/default/notify_master_postgresql.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash

MASTER_NODE=<%= @master_node %>
VIRTUAL_IP=<%= @virtual_ip %>
IFACE=<%= @iface %>
NAME=postgresql
LOG_FILE=/tmp/rb_notify_postgresql_master.log

# Cleaning tmp files
Expand All @@ -10,6 +12,31 @@ rm -f /tmp/rb_notify_postgresql*
date >> "$LOG_FILE"
echo "Executing rb_notify_master_postgresql" >> "$LOG_FILE"

grep vrrp_sync_group /etc/keepalived/keepalived.conf | grep -q $NAME
if [ $? -ne 0 ]; then
echo "$NAME has no virtual ip on this node" >> "$LOG_FILE"
exit 0
fi

# If the VIP is not up, then something is wrong
found=0
sleep 5 # wait 5 seconds to converge keepalived successfully

for i in $(seq 0 2); do
ip addr show dev $IFACE | grep "inet " | awk '{print $2}' | sed 's/\/.*//' | grep -q "^$VIRTUAL_IP$"
if [ $? -eq 0 ]; then
# found virtual ip address
found=1
break
fi
sleep 1
done

if [ $found -eq 0 ]; then
echo "error: virtual ip $VIRTUAL_IP not found, exiting" >> "$LOG_FILE"
exit 0
fi

echo "Promoting to master" >> "$LOG_FILE"
touch /tmp/postgresql.trigger
sed -i '/^primary_conninfo/d' /var/lib/pgsql/data/postgresql.conf
Expand Down

0 comments on commit 647375b

Please sign in to comment.