Ever had that heart-stopping moment when your website goes down? π± Or that frantic call from users who can’t access your application? π±β High availability doesn’t have to be complicated or expensive. Enter Keepalived: your friendly neighbourhood service guardian! π¦ΈββοΈ
What is Keepalived? π€
Keepalived is like that reliable friend who always has your back. π« It’s an open-source tool designed to provide high availability for Linux systems by automatically failing over to backup servers when your primary server gets into trouble. π¨
At its core, Keepalived implements the Virtual Router Redundancy Protocol (VRRP), which allows multiple servers to share a virtual IP address. When one server fails, another takes over seamlesslyβlike a superhero swooping in to save the day! π¦ΈββοΈπ¨
Why You’ll Love Keepalived β€οΈ
- Simple yet powerful: Does one thing and does it well πͺ
- Lightweight: Minimal resource usage πͺΆ
- Battle-tested: Used in production environments worldwide π
- Free and open-source: No licensing headaches! π
Installing Keepalived π οΈ
Let’s get Keepalived up and running on your Linux system:
Ubuntu/Debian π§
sudo apt update
sudo apt install keepalived
CentOS/RHEL π©
sudo yum install keepalived
Verification β
Check if Keepalived is installed correctly:
keepalived --version
You should see version information displayed. Success! π
Setting Up Your First High-Availability Pair π―ββοΈ
Let’s say you have two web servers that you want to configure for high availability. Here’s how to do it:
Create the Configuration File π
Primary Server Configuration /etc/keepalived/keepalived.conf
π₯
vrrp_instance WEB_HA {
state MASTER
interface eth0 # Your network interface
virtual_router_id 51
priority 101 # Higher number = higher priority
advert_int 1
authentication {
auth_type PASS
auth_pass yourpassword # Same on both servers
}
virtual_ipaddress {
192.168.1.100 # Your virtual IP address
}
}
Secondary Server Configuration /etc/keepalived/keepalived.conf
π₯
vrrp_instance WEB_HA {
state BACKUP
interface eth0 # Your network interface
virtual_router_id 51
priority 100 # Lower priority than the MASTER
advert_int 1
authentication {
auth_type PASS
auth_pass yourpassword # Same password as primary
}
virtual_ipaddress {
192.168.1.100 # Same virtual IP as primary
}
}
The main differences are:
state
is set to BACKUP instead of MASTER πpriority
is lower (100 instead of 101) β¬οΈ
Start the Service π
On both primary and backup servers:
sudo systemctl enable keepalived
sudo systemctl start keepalived
Test Your Setup π§ͺ
On your primary server, check if the virtual IP is assigned:
ip addr show eth0 #modify accordingly
You should see your virtual IP (192.168.1.100) listed.
Now for the fun part: simulate a failure by stopping Keepalived on your primary server: π
sudo systemctl stop keepalived
Check your secondary serverβit should now have the virtual IP! Your services remain accessible without any downtime. πͺ
Beyond Basic Failover: Getting Creative with Keepalived π¨
Keepalived isn’t just about IP failover. You can use it to:
Monitor Specific Services π
Add health checks to ensure your web server is actually running:
vrrp_script check_web {
script "curl -f http://localhost/ >/dev/null"
interval 2
weight 2
}
vrrp_instance WEB_HA {
# ... other settings ...
track_script {
check_web
}
}
Load Balancing βοΈ
Combine Keepalived with LVS (Linux Virtual Server) for load balancing:
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
protocol TCP
real_server 192.168.1.101 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.102 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
Troubleshooting Tips π§
If things aren’t working as expected:
- Check logs:
sudo journalctl -u keepalived
π - Verify network: Ensure your servers can communicate π
- Firewall rules: Make sure VRRP traffic (protocol 112) is allowed π§±
- Interface names: Double-check your network interface names π·οΈ
The Bottom Line π
Keepalived gives you enterprise-level high availability with minimal setup. It’s like insurance for your servicesβyou hope you never need it, but you’ll be incredibly grateful when you do! π‘οΈ
Whether you’re running a small blog or a mission-critical application, Keepalived helps ensure your services stay accessible when hardware or software decides to take an unexpected vacation. ποΈ
Give it a tryβyour future self (and your users) will thank you when that inevitable server hiccup happens! π