KeepAlived IP Failover on CentOS & Red Hat

KeepAlived IP Failover

Introduction

Learn how to set up Keepalived for IP failover on CentOS and Red Hat. This step-by-step guide ensures high availability and network redundancy, keeping your critical services online during hardware or network failures.

In the world of high availability and network redundancy, ensuring that your critical services remain operational despite hardware or network failures is paramount. Keepalived is a robust and versatile tool designed to provide IP failover solutions on Linux servers, particularly on CentOS and Red Hat distributions. This guide delves into the configuration and deployment of Keepalived for IP failover, ensuring that your services continue to run smoothly even in the face of unexpected downtimes. Whether you're an IT professional aiming to bolster your infrastructure or a systems administrator looking to implement a reliable failover mechanism, this tutorial offers step-by-step instructions to help you achieve high availability with Keepalived.

Keepalived use highly available to make Virtual IP call VIP . it's a linux implementation make of VRRP

my post, use keepAlived IP Failover on CentOS & Red Hat. I'm running commands as root account.

Links to below you maybe likes:

To install keepalived on centos 

yum update -y
yum install -y keepalived

To configure keepalived

On Node 1
! Configuration File for keepalived
global_defs {
   notification_email {
     sysadmin@mydomain.com
     support@mydomain.com
   }
   notification_email_from node1@mydomain.com
   smtp_server localhost
   smtp_connect_timeout 30
}
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass You-Add-Password-Here
    }
    virtual_ipaddress {
        192.168.10.121
    }
}
On Node 2
! Configuration File for keepalived
global_defs {
   notification_email {
     sysadmin@mydomain.com
     support@mydomain.com
   }
   notification_email_from node2@mydomain.com
   smtp_server localhost
   smtp_connect_timeout 30
}
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass You-Add-Password-Here
    }
    virtual_ipaddress {
        192.168.10.121 dev eth0
    }
}
Note
  • priority: The hight is master, nguoc lai la Backup.
  • virtual_router_id : the same bettween node 1 vs node 2
If you used Unicast, notify and track script then full script as bellow
! Configuration File for keepalived
global_defs {
   notification_email {
     sysadmin@mydomain.com
     support@mydomain.com
   }
   notification_email_from lb1@mydomain.com
   smtp_server localhost
   smtp_connect_timeout 30
}
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass You-Add-Password-Here
    }
 unicast_src_ip 192.168.10.122   # Unicast specific option, this is the IP of the interface keepalived listens on
    unicast_peer {                 # Unicast specific option, this is the IP of the peer instance
      192.168.10.123
    }
    virtual_ipaddress {
        192.168.10.121 dev eth0
    }
vrrp_script check_haproxy {
  script       "/opt/scripts/keepalivecheck_haproxy.sh"
  interval 2
  fall 2
  rise 2
}
track_script {
      check_haproxy
   }
   notify /opt/scripts/keepalivekeepalived.state.sh
}
The bash script keepalivecheck_haproxy.sh
cat /opt/scripts/keepalivecheck_haproxy.sh
As the output below
#!/bin/bash
# Check if haproxy is running, return 1 if not.
# Used by keepalived to initiate a failover in case haproxy is down
HAPROXY_STATUS=$(/bin/ps -e | grep -w haproxy)
if [ "$HAPROXY_STATUS" != "" ]
then
exit 0
else
logger "HAProxy is NOT running. Setting keepalived state to FAULT."
exit 1
fi
The bash script keepalivekeepalived.state.sh
cat /opt/scripts/keepalivekeepalived.state.sh
As the output below
#!/bin/bash
TYPE=$1
NAME=$2
STATE=$3
echo $STATE > /var/run/keepalived.state

Starting keepalived services
service keepalived start
chkconfig keepalived on
To verify keepalived
ip addr show eth1
tailf /var/messages

Conclusion

Implementing IP failover using Keepalived on CentOS or Red Hat not only enhances the resilience of your network but also ensures continuous service availability even in the event of failures. By following the steps outlined in this guide, you can create a highly available system that automatically manages failovers with minimal manual intervention. Keepalived, with its simplicity and effectiveness, stands out as an essential tool in the arsenal of any Linux systems administrator tasked with maintaining service uptime. Thank you for reading the huuphan.com page!

Comments

Popular posts from this blog

zimbra some services are not running [Solve problem]

Bash script list all IP addresses connected to Server with Country Information

Zimbra Client host rejected Access denied fixed