Skip to main content

Install keepalived With Nginx on Debian

· 2 min read
Mohammad Zakery

Keepalived

Keepalived Debian

Install Keepalived

1.Install Required Packages.

sudo apt-get update
sudo apt-get install linux-headers-$(uname -r)

2.Install Keepalived.

sudo apt-get install keepalived

Setup Keepalived

3.Now create or edit Keepalived configuration /etc/keepalived/keepalived.conf file on Node1.

! Configuration File for keepalived
global_defs {
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 50
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 50
priority 120
advert_int 1
virtual_ipaddress {
192.168.69.200/24
}
track_script {
check_nginx
}
}

4.Now create or edit Keepalived configuration /etc/keepalived/keepalived.conf file on Node2.

! Configuration File for keepalived
global_defs {
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 50
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 50
priority 110
advert_int 1
virtual_ipaddress {
192.168.69.200/24
}
track_script {
check_nginx
}
}

5.Now create or edit Keepalived configuration /etc/keepalived/keepalived.conf file on Node3.

! Configuration File for keepalived
global_defs {
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 50
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 50
priority 100
advert_int 1
virtual_ipaddress {
192.168.69.200/24
}
track_script {
check_nginx
}
}

6.Create or edit check_nginx script /etc/keepalived/check_nginx.sh file on each of the 3 nodes.

#!/bin/sh
if [ -z "`/bin/pidof nginx`" ]; then
/sbin/ip addr del 192.168.69.200/24 dev ens192
exit 1
fi

Firewall Settings

7.Edit nftables configurations /etc/nftables.conf to allow VRRP. Add the following content to the input chain.

ip protocol vrrp accept

8.Reload nftables.

systemctl reload nftables.service