如何配置Keepalived仅在两个接口都故障时进入Fault/Backup状态?
Absolutely! The default track_interface behavior triggers a state change if any tracked interface fails, but we can customize this to only act when both interfaces are down using a custom health check script combined with Keepalived's vrrp_script functionality. Here's how to implement it:
Step 1: Create a Custom Interface Check Script
First, write a script that checks the status of both your target interfaces. The script will return a failure code only if both interfaces are down—otherwise, it returns success.
Save this as /etc/keepalived/check_both_interfaces.sh:
#!/bin/bash # Define the two interfaces you want to monitor INTERFACE1="eth0" INTERFACE2="eth1" # Check if each interface is down if1_down=$(ip link show "$INTERFACE1" | grep -q "DOWN" && echo 1 || echo 0) if2_down=$(ip link show "$INTERFACE2" | grep -q "DOWN" && echo 1 || echo 0) # Only fail if BOTH are down if [ "$if1_down" -eq 1 ] && [ "$if2_down" -eq 1 ]; then exit 1 # Signal failure to Keepalived else exit 0 # Signal success (at least one interface is up) fi
Make the script executable:
chmod +x /etc/keepalived/check_both_interfaces.sh
Step 2: Update Keepalived Configuration
Modify your Keepalived config to replace the default track_interface directive with our custom script. This setup will adjust the VRRP instance priority (or trigger Fault state) only when the script detects both interfaces are down.
Here's a sample configuration:
global_defs { router_id LVS_SERVER } vrrp_instance VI_MAIN { state MASTER interface eth0 # This is the VRRP control interface (included in our script checks) virtual_router_id 50 priority 100 # Base priority for MASTER # Track our custom script instead of individual interfaces track_script { check_both_interfaces } # Define the script parameters vrrp_script check_both_interfaces { script "/etc/keepalived/check_both_interfaces.sh" interval 2 # Check every 2 seconds weight -50 # Lower priority by 50 on failure (enough to trigger failover) fall 2 # Mark as failed after 2 consecutive failures rise 2 # Mark as recovered after 2 consecutive successes } # Your virtual IP configuration virtual_ipaddress { 192.168.0.10/24 dev eth0 } }
How This Works:
- The script runs every 2 seconds. If both interfaces are down, it returns
exit 1, which tells Keepalived to lower the instance's priority by 50 (from 100 to 50). - If your Backup instance has a priority of 90, this drop will make the Backup instance take over as MASTER.
- When at least one interface comes back up, the script returns
exit 0, and Keepalived restores the original priority, allowing the instance to resume MASTER status (ifnopreemptisn't enabled).
For Direct Fault State Trigger:
If you want to immediately enter Fault state (instead of priority adjustment) when both interfaces fail, set the weight to -100 (matching your MASTER's base priority). This will drop the priority to 0, forcing the instance into Fault mode until the script reports success again.
内容的提问来源于stack exchange,提问作者Ilie Soltanici

