Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 81Keepalived · Network

Install and set up Keepalived (floating IP for high availability)

Keepalived gives two (or more) servers one virtual IP address (VIP). Clients always connect to the VIP; if the active server or its service dies, the VIP moves to the backup in about one second. It is the classic way to make HAProxy, Nginx, DNS or a database proxy highly available on your own servers, and to put a VIP in front of a kubeadm control plane. This guide installs Keepalived on RHEL and Ubuntu, configures a MASTER/BACKUP pair with a health check, opens the firewall for VRRP, tests failover, and covers split-brain and cloud limits.

  • 10 Lessen
  • 11 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

81.1

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.

ItemValue
Packagekeepalived
Servicekeepalived.service
Config/etc/keepalived/keepalived.conf
ProtocolVRRP (IP protocol 112), multicast 224.0.0.18 or unicast
Logsjournalctl -u keepalived
This guidelb1 10.0.1.5 (MASTER), lb2 10.0.1.6 (BACKUP), VIP 10.0.1.100

Notitie

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.

81.2

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 --reload

81.3

Install 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):

echo 'net.ipv4.ip_nonlocal_bind = 1' > /etc/sysctl.d/90-keepalived.conf
sysctl --system

81.4

First configuration: MASTER and BACKUP

On lb1 (MASTER):

/etc/keepalived/keepalived.conf (lb1)
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.

SettingWhy
virtual_router_id 51Must be the same on both servers, and unique in the network segment.
priority 110 / 100The higher one holds the VIP.
vrrp_script + weight -30If HAProxy stops, the priority drops to 80 (below the backup's 100) and the VIP moves.
unicast_peerTalk directly to the other server (works where multicast is blocked).
enable_script_securityKeepalived 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 STATE

81.5

Test 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 haproxy

On 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).

81.6

Notify scripts and the real uses

Run a command when the state changes (an alert, or moving a cloud IP):

/etc/keepalived/keepalived.conf (inside vrrp_instance)
    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 vrrp

Real 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.

81.7

Make it safe

Security checklist:

  • Allow VRRP only from the peer servers (firewall source); use unicast peers.
  • Unique virtual_router_id per 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).

81.8

Update and remove

dnf -y upgrade keepalived
apt-get install --only-upgrade -y keepalived
systemctl restart keepalived
systemctl disable --now keepalived

Upgrade the BACKUP first, then fail over and upgrade the old MASTER.

81.9

Fix common problems

ProblemCauseFix
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 stopsThe 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 VIPThe IP is not on that host yet.net.ipv4.ip_nonlocal_bind = 1.
Failover works but clients still go to the old serverARP cache in switches/clients.Keepalived sends gratuitous ARP; check switch settings (garp_master_delay, garp_master_refresh).
Nothing works in the cloudVRRP/gratuitous ARP not supported.Use the cloud load balancer or a notify script that moves the cloud IP.

81.10

Next steps

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).

Alle commando's in dit hoofdstuk