Centos 9 and keeplived

What is Keepalived ?

Keepalived is a routing software written in C. The main goal of this project is to provide simple and robust facilities for loadbalancing and high-availability to Linux system and Linux based infrastructures. Loadbalancing framework relies on well-known and widely used Linux Virtual Server (IPVS) kernel module providing Layer4 loadbalancing. Keepalived implements a set of checkers to dynamically and adaptively maintain and manage loadbalanced server pool according their health. On the other hand high-availability is achieved by VRRP protocol. VRRP is a fundamental brick for router failover. In addition, Keepalived implements a set of hooks to the VRRP finite state machine providing low-level and high-speed protocol interactions. In order to offer fastest network failure detection, Keepalived implements BFD protocol. VRRP state transition can take into account BFD hint to drive fast state transition. Keepalived frameworks can be used independently or all together to provide resilient infrastructures.

As always, start with installint the software:

dnf install keepalived -y

Now, move original configuration file and create you own and insert values like below. This is configuration for primary (master) server1:

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.original
vim /etc/keepalived/keepalived.conf

global_defs {
# Keepalived process identifier
router_id apache
}

# Script to check whether apache is running or not
vrrp_script check_httpd {
script "/bin/check_httpd.sh"
interval 2
weight 50
}

# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance apache {
state MASTER
interface ens18
virtual_router_id 151
priority 110

# The virtual ip address shared between the two apache Web Servers which will float
virtual_ipaddress {
192.168.1.10/24
}
track_script {
check_httpd
}
authentication {
auth_type PASS
auth_pass secret
}
}

Save and close the file when you are finished.

On the secondary (slave) server2, edit the keepalived.conf file, very similar to our first:

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.original
vim /etc/keepalived/keepalived.conf

global_defs {
# Keepalived process identifier
router_id apache
}

# Script to check whether apache is running or not
vrrp_script check_httpd {
script "/bin/check_httpd.sh"
interval 2
weight 50
}

# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance apache {
state BACKUP
interface ens18
virtual_router_id 151
priority 100

# The virtual ip address shared between the two apache Web Servers which will float
virtual_ipaddress {
192.168.1.10/24
}
track_script {
check_httpd
}
authentication {
auth_type PASS
auth_pass secret
}
}

Now we will need to create a script to check whether the apache service is running or not. You can create it using the following command:

vim /bin/check_httpd.sh

#!/bin/sh
if [ -z "`pidof httpd`" ]; then
exit 1
fi

Now we set proper permission with the following command:

chmod 755 /bin/check_httpd.sh

Before we start keeplived daemon, if we have (I believe, you have) firewall enabled, enable vrrp protocol:

firewall-cmd --add-protocol=vrrp  --permanent
firewall-cmd --add-protocol=vrrp

Finally, start keepalived service and enable it to start at system reboot using the following command.

systemctl enable keepalived --now

We can also check the status of keepalived service using the following command:

systemctl status keepalived
Total Page Visits: 181 - Today Page Visits: 1