Table of Contents
Steps to install and configure keepalived ........................................................................ 2
1. Install Keepalived ............................................................................................... 2
2. Install libipset13 (Optional) ................................................................................. 2
3. Configure Keepalived and Enable Keepalived on both nodes ........................... 3
4. Enable keepalived service and check the status on both nodes ........................ 5
Verifying the functionality of Keepalived for HA and failover ........................................... 6
1. Ping the virtual IP ............................................................................................... 6
2. Accessing a web service hosted on Master node through virtual ip: .................. 6
3. Stop Keepalived on the MASTER node: ............................................................ 7
4. Restart the Keepalived service on the MASTER node ....................................... 8
Codes .............................................................................................................................. 9
1. For Master Node: ............................................................................................... 9
2. For Backup Node: .............................................................................................. 9
gautam-shrestha.com.np
Page | 1
Before you begin, ensure you have:
1. Two Linux-based machines (virtual or physical) with Ubuntu, Debian, CentOS, or
Raspbian. These will be your master and worker nodes.
To download the Ubuntu server for vmware: Click here!
Step 1: Download the file from above link, then extract it.
Step 2: Make two copies (one for master node, one for backup node).
Step 3: Modify the static IP addresses and hostnames as per the network requirements.
Ensure each virtual machine has a distinct IP and hostname.
2. SSH access to both machines with sudo privileges.
gautam-shrestha.com.np
Page | 2
Steps to install and configure keepalived
1. Install Keepalived
Install Keepalived on both servers (Nginx1, Nginx2)
apt install keepalived
2. Install libipset13 (Optional)
apt install libipset13
Libipset is a library that provides an infrastructure for managing sets of IP addresses,
networks, and port ranges.
gautam-shrestha.com.np
Page | 3
3. Configure Keepalived and Enable Keepalived on both nodes
Once installed, you need to configure Keepalived on each node. Keepalived uses a
configuration file (keepalived.conf) to define its behavior. Edit the Keepalived
configuration file (/etc/keepalived/keepalived.conf).
sudo nano /etc/keepalived/keepalived.conf
For nginx1: (Click here! for code)
Here,
vrrp_instance VI_1: Defines a VRRP instance with the name VI_1.
state: Specifies whether the node is MASTER or BACKUP.
interface: Network interface where VRRP packets will be sent and received.
virtual_router_id: A unique identifier for the VRRP instance.
priority: Priority of the node; higher priority nodes become MASTER.
advert_int: Advertisement interval (how often the nodes send VRRP advertisements).
unicast_src_ip: Specifies the source IP address that Keepalived will use for sending
VRRP unicast packets. This is typically set to an IP address of the current node.
unicast_peer: Lists the IP addresses of the peer nodes that will receive the VRRP
unicast packets. These are the addresses of the other nodes participating in the VRRP
setup.
authentication: Authentication settings (auth_type and auth_pass).
virtual_ipaddress: Virtual IP address associated with this VRRP instance.
gautam-shrestha.com.np
Page | 4
For nginx2: (Click here! for code)
To configure Keepalived on the other node (assuming it's a backup node in your setup),
you'll essentially replicate the configuration with a few adjustments to ensure proper
failover behavior.
Here,
state BACKUP: Specifies that this node should be in a backup state. The BACKUP
state means it will only take over the MASTER role if the MASTER node fails or
becomes unavailable.
priority 100: Assigns a lower priority to the backup node compared to the MASTER
node. Keepalived uses priorities to determine which node becomes the MASTER (higher
priority wins).
unicast_src_ip: Set the IP address of the backup node.
unicast_peer: Lists the IP address of the MASTER node. This ensures that the backup
node communicates with the MASTER node using unicast for VRRP messages.
gautam-shrestha.com.np
Page | 5
4. Enable keepalived service and check the status on both nodes
sudo systemctl enable now keepalived.service
sudo systemctl status keepalived.service
gautam-shrestha.com.np
Page | 6
Verifying the functionality of Keepalived for HA and failover
1. Ping the virtual IP
Successful ping responses received from the virtual IP, indicating it is correctly assigned
to the MASTER node.
2. Accessing a web service hosted on Master node through virtual ip:
Successfully accessed the service (Hello World 1), confirming the service is reachable
through the virtual IP.
gautam-shrestha.com.np
Page | 7
3. Stop Keepalived on the MASTER node:
sudo systemctl stop keepalived
Refreshing the page displays Hello World 2, indicating successful failover to the BACKUP
node.
gautam-shrestha.com.np
Page | 8
4. Restart the Keepalived service on the MASTER node
sudo systemctl enable now keepalived.service
Refreshing the page displays Hello World 1 again, confirming failback to the MASTER
node.
gautam-shrestha.com.np
Page | 9
Codes
1. For Master Node:
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 55
priority 150
advert_int 1
unicast_src_ip 192.168.230.150
unicast_peer {
192.168.230.151
}
authentication {
auth_type PASS
auth_pass Nginx@11
}
virtual_ipaddress {
192.168.230.200/24
}
}
2. For Backup Node:
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 55
priority 150
advert_int 1
unicast_src_ip 192.168.230.151
unicast_peer {
192.168.230.150
}
authentication {
auth_type PASS
auth_pass Nginx@11
}
virtual_ipaddress {
192.168.230.200/24
}
}