To instantly check your Keepalived instance's current state (Master/Backup), run:
ip addr show | grep "inet " | grep eth0
# OR for newer systems:
ip addr show | grep "inet " | grep -E 'eth0|ens[0-9]+'
The VIP (Virtual IP) will only appear on the master node. You can also use:
journalctl -u keepalived --no-pager -n 20
# Shows last 20 keepalived logs
For more comprehensive status checks:
# Method 1: Check process status
systemctl status keepalived
# Method 2: View full state information
keepalived -v
keepalived -d
# Debug mode shows election process (run temporarily)
Create a monitoring script /usr/local/bin/check_keepalived.sh
:
#!/bin/bash
STATE=$(systemctl status keepalived | grep -oP "State: \K\w+")
VIP="192.168.1.100" # Your virtual IP
if ip addr | grep -q $VIP; then
echo "Status: MASTER (VIP active)"
echo "Service: $STATE"
else
echo "Status: BACKUP"
echo "Service: $STATE"
fi
Make it executable:
chmod +x /usr/local/bin/check_keepalived.sh
To extract state transitions:
grep -E "Entering MASTER|Entering BACKUP" /var/log/syslog
# Or for journald systems:
journalctl -u keepalived | grep -E "Entering MASTER|Entering BACKUP"
If state checks aren't working:
- Verify config file syntax:
keepalived -t -f /etc/keepalived/keepalived.conf
- Check firewall rules for VRRP protocol (usually IP protocol 112)
- Ensure multicast is allowed between nodes
The most direct way is using ipvsadm
combined with system commands:
# Check current role
ip addr show | grep -A 1 'VIRTUAL'
# Alternative method using systemctl
systemctl status keepalived --no-pager | grep -i 'state'
For comprehensive status checking, these approaches work on most Linux distributions:
# Method 1: Using keepalived's own status file
cat /var/run/keepalived.state
# Method 2: Checking VIP assignment
ip -br addr show dev eth0 | grep -E '(MASTER|BACKUP)'
# Method 3: Parsing logs in real-time
journalctl -u keepalived -f -n 0 | grep -E 'Entering|Leaving'
Create a monitoring script (/usr/local/bin/check_keepalived.sh
):
#!/bin/bash
STATE=$(cat /var/run/keepalived.state 2>/dev/null || echo "UNKNOWN")
VIP=$(ip -o addr show | grep -v 'inet6' | awk '/scope global/ {print $4}')
echo "Keepalived State: ${STATE}"
echo "VIP Assignment: ${VIP:-None}"
exit 0
- Verify configuration:
keepalived -t -f /etc/keepalived/keepalived.conf
- Check process:
ps aux | grep '[k]eepalived'
- Test failover:
systemctl stop keepalived
on master node
For programmatic access to the status:
curl -s http://localhost:8080/status # Requires HTTP checker configured
# Sample response: {"state":"MASTER","vip":"192.168.1.100"}