Keepalived ermöglicht Hochverfügbarkeit durch virtuelle IP-Adressen (VIP). Bei Ausfall des Master-Servers übernimmt automatisch ein Backup-Server die VIP.

Funktionsweise

VRRP-Protokoll

VRRP (Virtual Router Redundancy Protocol)

Master        Backup
Server        Server
   |             |
   +------+------+
          |
        [VIP]
          |
       Clients

Ablauf

1. Master sendet regelmäßig VRRP-Advertisements
2. Backup überwacht Master
3. Bei Ausbleiben der Advertisements:
   - Backup wird Master
   - VIP wandert zum neuen Master
4. Wenn ursprünglicher Master zurückkehrt:
   - Optional: Preemption (Master übernimmt wieder)

Installation

Debian/Ubuntu

apt install keepalived

CentOS/RHEL

dnf install keepalived

Basis-Konfiguration

Master-Server

# /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass secretpassword
    }

    virtual_ipaddress {
        192.168.1.100/24
    }
}

Backup-Server

# /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass secretpassword
    }

    virtual_ipaddress {
        192.168.1.100/24
    }
}

Service starten

systemctl enable keepalived
systemctl start keepalived

Konfigurationsoptionen

Parameter

| Parameter | Beschreibung | |-----------|--------------| | state | MASTER oder BACKUP | | interface | Netzwerk-Interface | | virtual_router_id | Eindeutige ID (1-255) | | priority | Priorität (höher = bevorzugt) | | advert_int | Advertisement-Intervall | | nopreempt | Kein automatisches Zurückwechseln |

Ohne Preemption

vrrp_instance VI_1 {
    state BACKUP
    nopreempt  # Bleibt Master auch nach Recovery
    # ...
}

Unicast statt Multicast

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100

    unicast_src_ip 192.168.1.10
    unicast_peer {
        192.168.1.11
    }

    virtual_ipaddress {
        192.168.1.100/24
    }
}

Health Checks

Skript-basierter Check

vrrp_script check_nginx {
    script "/usr/local/bin/check_nginx.sh"
    interval 2
    weight -50
    fall 3
    rise 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100

    track_script {
        check_nginx
    }

    virtual_ipaddress {
        192.168.1.100/24
    }
}

Check-Skript

#!/bin/bash
# /usr/local/bin/check_nginx.sh

if systemctl is-active --quiet nginx; then
    exit 0
else
    exit 1
fi
chmod +x /usr/local/bin/check_nginx.sh

Interface-Tracking

vrrp_instance VI_1 {
    track_interface {
        eth0
        eth1 weight -100
    }
}

Benachrichtigungen

Notify-Skripte

vrrp_instance VI_1 {
    # ...

    notify_master "/usr/local/bin/notify.sh master"
    notify_backup "/usr/local/bin/notify.sh backup"
    notify_fault "/usr/local/bin/notify.sh fault"
}

Notify-Skript

#!/bin/bash
# /usr/local/bin/notify.sh

STATE=$1
INSTANCE="VI_1"
HOSTNAME=$(hostname)
DATE=$(date)

case $STATE in
    master)
        echo "$DATE: $HOSTNAME became $STATE for $INSTANCE" | \
            mail -s "Keepalived: $HOSTNAME is now MASTER" admin@example.de
        ;;
    backup)
        echo "$DATE: $HOSTNAME became $STATE for $INSTANCE" | \
            mail -s "Keepalived: $HOSTNAME is now BACKUP" admin@example.de
        ;;
    fault)
        echo "$DATE: $HOSTNAME is in $STATE for $INSTANCE" | \
            mail -s "Keepalived: $HOSTNAME FAULT" admin@example.de
        ;;
esac

HAProxy + Keepalived

Vollständige Konfiguration

# /etc/keepalived/keepalived.conf

global_defs {
    router_id LB1
    script_user root
    enable_script_security
}

vrrp_script check_haproxy {
    script "/usr/bin/killall -0 haproxy"
    interval 2
    weight -50
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass secretpw
    }

    track_script {
        check_haproxy
    }

    virtual_ipaddress {
        192.168.1.100/24
    }

    notify_master "/usr/local/bin/keepalived-notify.sh master"
    notify_backup "/usr/local/bin/keepalived-notify.sh backup"
}

Zweiter Server

# Gleich, aber:
# state BACKUP
# priority 90

Mehrere VIPs

vrrp_instance VI_WEB {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100

    virtual_ipaddress {
        192.168.1.100/24 label eth0:web
    }
}

vrrp_instance VI_DB {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 90

    virtual_ipaddress {
        192.168.1.101/24 label eth0:db
    }
}

Synchronisationsgruppe

vrrp_sync_group SG_1 {
    group {
        VI_WEB
        VI_DB
    }

    notify_master "/usr/local/bin/notify.sh master"
    notify_backup "/usr/local/bin/notify.sh backup"
}

Status prüfen

IP prüfen

ip addr show eth0
# VIP sollte auf Master sichtbar sein

Keepalived-Status

systemctl status keepalived

# Logs
journalctl -u keepalived -f

VRRP-Traffic

tcpdump -i eth0 vrrp

Failover testen

Manuell

# Auf Master
systemctl stop keepalived

# Prüfen auf Backup
ip addr show eth0
# VIP sollte jetzt hier sein

Service-Ausfall simulieren

# Auf Master
systemctl stop nginx

# Check-Skript sollte Failover auslösen

Troubleshooting

VIP wird nicht übernommen

# Firewall prüfen
iptables -L -n | grep vrrp

# VRRP erlauben
iptables -A INPUT -p vrrp -j ACCEPT

# Oder
ufw allow to 224.0.0.18

Split-Brain vermeiden

# Gleiche virtual_router_id
# Gleiche authentication
# Unterschiedliche priority

ARP-Cache

# ARP-Cache löschen auf Clients bei Problemen
ip neigh flush dev eth0

Best Practices

Empfehlungen

- Unicast in Cloud-Umgebungen verwenden
- Monitoring für VRRP-Status
- Health-Checks für Dienste
- Notify-Skripte für Alerting
- Regelmäßige Failover-Tests

Sicherheit

# Starkes Passwort
authentication {
    auth_type PASS
    auth_pass "langes_sicheres_passwort"
}

# VRRP nur auf internem Interface
interface eth1  # Internes Netz

Zusammenfassung

| Parameter | Beschreibung | |-----------|--------------| | state | MASTER/BACKUP | | priority | Höher = bevorzugt | | virtual_router_id | Eindeutige ID | | nopreempt | Kein Zurückwechseln | | advert_int | Check-Intervall |

| Befehl | Funktion | |--------|----------| | ip addr show | VIP prüfen | | tcpdump -i eth0 vrrp | VRRP-Traffic | | journalctl -u keepalived | Logs |

| Datei | Funktion | |-------|----------| | /etc/keepalived/keepalived.conf | Konfiguration | | /usr/local/bin/*.sh | Check/Notify-Skripte |

Fazit

Keepalived ist die Standardlösung für IP-Failover unter Linux. Die Kombination mit HAProxy ergibt einen hochverfügbaren Load Balancer. Health Checks stellen sicher, dass nur funktionierende Server Traffic erhalten. Notify-Skripte ermöglichen Alerting bei Failover-Events. Für kritische Produktionsumgebungen ist Keepalived unverzichtbar.