هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
Know it before you install
Keepalived uses VRRP: the server with the highest priority holds the VIP and sends "I am alive" messages every second. When they stop — or a track script says the service is down and lowers the priority — the backup takes the VIP and announces it with gratuitous ARP.
| Item | Value |
|---|---|
| Package | keepalived |
| Service | keepalived.service |
| Config | /etc/keepalived/keepalived.conf |
| Protocol | VRRP (IP protocol 112), multicast 224.0.0.18 or unicast |
| Logs | journalctl -u keepalived |
| This guide | lb1 10.0.1.5 (MASTER), lb2 10.0.1.6 (BACKUP), VIP 10.0.1.100 |
ملاحظة
Most public clouds (AWS, Azure, GCP) do not allow VRRP or gratuitous ARP between VMs. There you use the cloud load balancer, or a notify script that moves a cloud "secondary IP / floating IP" through the cloud API. Keepalived as shown here is for on-prem, VMware, Proxmox and bare metal.
Install on RHEL, Rocky, Alma and Fedora
On both servers:
dnf -y install keepalived
firewall-cmd --permanent --add-rich-rule='rule protocol value="vrrp" accept'
firewall-cmd --reloadInstall on Ubuntu and Debian
apt-get update
apt-get install -y keepalived
ufw allow proto vrrp from 10.0.1.0/24(If your UFW version does not know vrrp, add an -A ufw-before-input -p vrrp -j ACCEPT line in /etc/ufw/before.rules.)
So HAProxy/Nginx can bind to the VIP on the backup server too (before it owns it):
First configuration: MASTER and BACKUP
On lb1 (MASTER):
global_defs {
router_id lb1
enable_script_security
script_user root
}
vrrp_script chk_haproxy {
script "/usr/bin/systemctl is-active --quiet haproxy"
interval 2
fall 2
rise 2
weight -30
}
vrrp_instance VI_WEB {
state MASTER
interface eth0
virtual_router_id 51
priority 110
advert_int 1
unicast_src_ip 10.0.1.5
unicast_peer {
10.0.1.6
}
virtual_ipaddress {
10.0.1.100/24 dev eth0
}
track_script {
chk_haproxy
}
}On lb2 (BACKUP) the same file with router_id lb2, state BACKUP, priority 100, unicast_src_ip 10.0.1.6 and the peer 10.0.1.5.
| Setting | Why |
|---|---|
virtual_router_id 51 | Must be the same on both servers, and unique in the network segment. |
priority 110 / 100 | The higher one holds the VIP. |
vrrp_script + weight -30 | If HAProxy stops, the priority drops to 80 (below the backup's 100) and the VIP moves. |
unicast_peer | Talk directly to the other server (works where multicast is blocked). |
enable_script_security | Keepalived refuses scripts that non-root users could change. |
Start on both and check who holds the VIP:
keepalived -t -f /etc/keepalived/keepalived.conf
systemctl enable --now keepalived
ip -br addr show eth0
eth0 UP 10.0.1.5/24 10.0.1.100/24
journalctl -u keepalived -n 10 --output cat
(VI_WEB) Entering MASTER STATETest the failover
From a client, watch the VIP while you break the master:
while true; do curl -s -o /dev/null -w "%{http_code} " http://10.0.1.100/; sleep 0.5; done
systemctl stop haproxy
journalctl -u keepalived -n 5 --output cat
(VI_WEB) Changing effective priority from 110 to 80
(VI_WEB) Entering BACKUP STATE
systemctl start haproxyOn lb2, ip -br addr now shows 10.0.1.100. The curl loop should show only a short gap. When HAProxy on lb1 is back, the VIP returns (add nopreempt with state BACKUP on both if you prefer that it stays where it is).
Notify scripts and the real uses
Run a command when the state changes (an alert, or moving a cloud IP):
notify_master "/usr/local/bin/vip-notify.sh MASTER"
notify_backup "/usr/local/bin/vip-notify.sh BACKUP"
notify_fault "/usr/local/bin/vip-notify.sh FAULT"systemctl reload keepalived
journalctl -u keepalived -f
ip -br addr | grep 10.0.1.100
tcpdump -ni eth0 vrrpReal uses at work:
- Two HAProxy or Nginx load balancers with one VIP — no single point of failure.
- A VIP for the Kubernetes API in front of 3 control plane nodes (Keepalived + HAProxy on each).
- Highly available DNS resolvers, database proxies (PgBouncer, ProxySQL) and NFS gateways.
Make it safe
Security checklist:
- Allow VRRP only from the peer servers (firewall source); use unicast peers.
- Unique
virtual_router_idper segment — a duplicate ID from another team causes strange failovers. enable_script_security; scripts owned by root and not writable by others.- Monitor which node holds the VIP and alert on state changes (notify script → Alertmanager/email).
- Test failover regularly (planned maintenance is a good moment).
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
| Both servers say MASTER (split brain) | VRRP packets blocked, or different virtual_router_id. | Allow VRRP in both firewalls; same ID; check with tcpdump -ni eth0 vrrp. |
| VIP does not move when the service stops | The track script is not used or not allowed. | track_script in the instance; enable_script_security + script_user; check the log for "script … disabled". |
| VIP moves back and forth (flapping) | Health check too strict or the service restarts often. | Higher fall/rise; fix the service; consider nopreempt. |
| HAProxy on the backup can not bind the VIP | The IP is not on that host yet. | net.ipv4.ip_nonlocal_bind = 1. |
| Failover works but clients still go to the old server | ARP cache in switches/clients. | Keepalived sends gratuitous ARP; check switch settings (garp_master_delay, garp_master_refresh). |
| Nothing works in the cloud | VRRP/gratuitous ARP not supported. | Use the cloud load balancer or a notify script that moves the cloud IP. |
Next steps
- Quick how-tos: Highly available IP.
- Command page: keepalived.
- At work: keepalived — failover IP.
- What runs behind the VIP: HAProxy, Nginx, Kubernetes with kubeadm.
1What makes the VIP move when HAProxy stops but the server is still up?
A vrrp_script health check with a negative weight lowers the priority below the backup's.
2Both nodes show MASTER. What is the most common cause?
VRRP packets are blocked by a firewall (or the virtual_router_id differs).