Keepalived und VRRP – Hochverfügbare IP-Adressen 2025

Virtuelle IP-Adresse automatisch zwischen zwei Servern wechseln

S
SeeColors IT
11. Juni 20264 Min. Lesezeit229 Aufrufe

VRRP-Konzept

Zwei Server, eine virtuelle IP (VIP):

[Server 1 – MASTER]     [Server 2 – BACKUP]
IP: 192.168.1.10        IP: 192.168.1.11
VIP: 192.168.1.100 ←   VIP: (wartet)

Client verbindet zu: 192.168.1.100

Bei Ausfall von Server 1:
→ Server 2 übernimmt VIP 192.168.1.100 in < 3 Sekunden!
→ Client-Verbindung wird neu aufgebaut, seamless für User

Keepalived installieren

# Auf BEIDEN Servern
apt install -y keepalived

# IP-Forwarding und non-local-bind
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf
sysctl -p

Master-Konfiguration

# /etc/keepalived/keepalived.conf (Server 1 – MASTER)
cat > /etc/keepalived/keepalived.conf << 'EOF'
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100             # Master hat höhere Priorität
    advert_int 1             # VRRP-Heartbeat jede Sekunde

    authentication {
        auth_type PASS
        auth_pass sicheres-vrrp-passwort
    }

    virtual_ipaddress {
        192.168.1.100/24     # Virtuelle IP
    }

    # Optional: Script zum Health-Check von HAProxy
    track_script {
        chk_haproxy
    }
}

vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 2
    weight -20               # Priorität senken wenn HAProxy down
}
EOF

systemctl enable --now keepalived

Backup-Konfiguration

# /etc/keepalived/keepalived.conf (Server 2 – BACKUP)
cat > /etc/keepalived/keepalived.conf << 'EOF'
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90              # Backup hat niedrigere Priorität
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass sicheres-vrrp-passwort
    }

    virtual_ipaddress {
        192.168.1.100/24
    }

    track_script {
        chk_haproxy
    }
}

vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 2
    weight -20
}
EOF

systemctl enable --now keepalived

Failover testen

# Auf Client: Ping zur VIP (nicht unterbrechen)
ping 192.168.1.100

# Auf Master: keepalived stoppen (simuliert Ausfall)
systemctl stop keepalived

# Auf Backup prüfen:
ip addr show eth0
# → 192.168.1.100 sollte jetzt auf eth0 erscheinen!

# Ping-Ausgabe auf Client:
# 64 bytes: icmp_seq=47
# Request timeout for icmp_seq=48  (1-2 Pакете Verlust)
# 64 bytes: icmp_seq=49             (Backup übernimmt)

Keepalived Notify-Script

# Benachrichtigung bei Failover
vrrp_instance VI_1 {
    ...
    notify_master "/opt/scripts/master.sh"
    notify_backup "/opt/scripts/backup.sh"
}

# /opt/scripts/master.sh
cat > /opt/scripts/master.sh << 'EOF'
#!/bin/bash
echo "$(date): Wurde MASTER" | mail -s "VRRP Failover: MASTER" [email protected]
EOF

chmod +x /opt/scripts/master.sh

FAQ

Wie schnell ist der Failover?
Standard: 1-3 Sekunden (advert_int=1 + 3x timeout). Mit advert_int=0.5 und Preemption schneller. Typischer DNS-Failover (60s TTL) ist viel langsamer.

Fazit

Keepalived mit VRRP bietet kostenlose Hochverfügbarkeit ohne teure Hardware-Loadbalancer – ideal für HA-HAProxy, NGINX oder Datenbank-VIPs.

Hochverfügbarkeit und Netzwerk für KMU in Heidelberg, Mannheim und der Rhein-Neckar-Region. Anfragen.

Artikel teilen

War dieser Artikel hilfreich?

Dein Feedback hilft uns, bessere Inhalte zu erstellen.

Kommentar hinterlassen

Verwandte Artikel