What You’ll Need
- At least two servers or virtual machines (referred to as primary and backup nodes).
- A Linux-based OS installed on both (Ubuntu 20.04 or newer is recommended).
- A shared virtual IP address (VIP) for failover.
Step 1: Install Keepalived
-
Log into both nodes and update the system:
sudo apt update && sudo apt upgrade -y
-
Install Keepalived on both nodes:
sudo apt install -y keepalived
-
Verify the installation:
keepalived --version
Step 2: Configure Keepalived on the Primary Node
-
Edit the Keepalived configuration file:
sudo nano /etc/keepalived/keepalived.conf
-
Add the following configuration:
vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass securepassword } virtual_ipaddress { 192.168.1.100 } }
- Replace
eth0
with your network interface name (useip a
to find it). - Replace
192.168.1.100
with your desired virtual IP (VIP). - Use a secure password for
auth_pass
.
- Replace
-
Restart Keepalived:
sudo systemctl restart keepalived
-
Enable Keepalived to start on boot:
sudo systemctl enable keepalived
Step 3: Configure Keepalived on the Backup Node
-
Edit the Keepalived configuration file:
sudo nano /etc/keepalived/keepalived.conf
-
Add the following configuration:
vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass securepassword } virtual_ipaddress { 192.168.1.100 } }
Ensure the
state
is set toBACKUP
andpriority
is lower than the primary node. -
Restart Keepalived:
sudo systemctl restart keepalived
-
Enable Keepalived to start on boot:
sudo systemctl enable keepalived
Step 4: Test the High-Availability Setup
-
Check the VIP on the primary node:
ip a | grep 192.168.1.100
The VIP should be bound to the primary node’s interface.
-
Simulate a failover:
- Stop Keepalived on the primary node:
sudo systemctl stop keepalived
- Check the VIP on the backup node:
ip a | grep 192.168.1.100
The VIP should now be assigned to the backup node.
- Stop Keepalived on the primary node:
-
Restart Keepalived on the primary node:
sudo systemctl start keepalived
Step 5: Monitor and Maintain
-
Check Keepalived logs for troubleshooting:
sudo journalctl -u keepalived
-
Verify that Keepalived is running:
sudo systemctl status keepalived
-
Update Keepalived and system packages regularly:
sudo apt update && sudo apt upgrade -y
FAQs
Q: Can I use Keepalived with multiple backup nodes?
A: Yes, Keepalived supports multiple backup nodes by assigning unique priorities to each one.
Q: Does Keepalived work with IPv6?
A: Yes, Keepalived supports both IPv4 and IPv6 addresses.
Q: How do I troubleshoot failover issues?
A: Check the following:
- Logs in
/var/log/syslog
orjournalctl
. - Network configuration (correct interfaces and VIP settings).
- Ensure the authentication
auth_pass
is identical on all nodes.
Q: Can I use Keepalived for load balancing?
A: Yes, Keepalived supports Layer 4 load balancing through the LVS (Linux Virtual Server) feature.
Q: How do I ensure the VIP is reachable across subnets?
A: Enable proxy ARP or configure static routes for the VIP.
Q: Can Keepalived monitor service health?
A: Yes, Keepalived can monitor services with health-check scripts and perform failover based on service availability.
By setting up Keepalived, you can create a highly available environment for critical services in your homelab. This ensures minimal downtime and a reliable infrastructure. Happy experimenting!