Network

How to install KeepAlived on CentOS 7

Share on Social Media

In this tutorial, you will learn, how to install KeepAlived on CentOS 7 or other Red hat based Linux servers. #centlinux #linux #highavailability

What is KeepAlived?

Keepalived is a routing software written in C. The main goal of this project is to provide simple and robust facilities for load-balancing and high-availability to Linux system and Linux based infrastructures. Loadbalancing framework relies on well-known and widely used Linux Virtual Server (IPVS) kernel module providing Layer4 load-balancing. Keepalived implements a set of checkers to dynamically and adaptively maintain and manage load-balanced server pool according their health. On the other hand high-availability is achieved by VRRP protocol. VRRP is a fundamental brick for router failover. In addition, Keepalived implements a set of hooks to the VRRP finite state machine providing low-level and high-speed protocol interactions. Keepalived frameworks can be used independently or all together to provide resilient infrastructures.

Recommended Online Training: Learn Bash Shell in Linux for Beginners

What is Floating IP Address?

Floating IP address is used to support failover in a high-availability cluster. The cluster is configured such that only the active member of the cluster “owns” or responds to that IP address at any given time. Should the active member fail, then “ownership” of the floating IP address would be transferred to a standby member to promote it as the new active member. Specifically, the member to be promoted issues a gratuitous ARP, announcing the new MAC address–to–IP address association.

KeepAlived performs well with haproxy load balancers. Have a look at our article on how to install haproxy on CentOS 7.

System Specification

We have two webservers webserver-01.centlinux.com and webserver-02.centlinux.com.

Hostname webserver-01.centlinux.com webserver-02.centlinux.com
IP Address 192.168.116.31/24 192.168.116.32/24
Operating System CentOS 7 CentOS 7
Web Server Nginx Nginx

Floating IP Address: 192.168.116.50/24

To ensure that our webservers are properly configured and browsable, open their URLs in a Browser.

webserver-01 page 1
webserver-02 page 1

I have set different index pages on both servers, to differentiate between servers, when we are accessing them via the Floating IP address.

Configure KeepAlived on webserver-01

Connect to webserver-01.centlinux.com and install keepalived by using yum command.

# yum install -y keepalived

Set Linux Kernel parameters as follows to support Floating IP.

# echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf

# sysctl -p
net.ipv4.ip_nonlocal_bind = 1

Now configure keepalived settings.

# cd /etc/keepalived/
# mv keepalived.conf keepalived.conf.org
# vi keepalived.conf

Add following directives and save.

! Configuration File for keepalived

global_defs {
   notification_email {
  root@webserver-01.centlinux.com
   }
   notification_email_from root@webserver-01.centlinux.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eno16777728
    virtual_router_id 51
    priority 101 #used in election, 101 for master & 100 for backup
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.116.50/24
    }
}

Start and enable keepalived service.

# systemctl start keepalived ; systemctl enable keepalived
ln -s '/usr/lib/systemd/system/keepalived.service' '/etc/systemd/system/multi-user.target.wants/keepalived.service'

Check IP Address of the server.

# ip addr | grep "inet" | grep "eno16777728"
    inet 192.168.116.31/24 brd 192.168.116.255 scope global eno16777728
    inet 192.168.116.50/24 scope global secondary eno16777728

You might observe that the Floating IP: 192.168.116.50 has been assigned to the network interface.

Configure KeepAlived on webserver-02

Connect to webserver-02.centlinux.com and install keepalived by using yum command.

# yum install -y keepalived

Set Linux Kernel parameters as follows to support Floating IP.

# echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf

# sysctl -p
net.ipv4.ip_nonlocal_bind = 1

Now configure keepalived settings.

# cd /etc/keepalived/
# mv keepalived.conf keepalived.conf.org
# vi keepalived.conf

Add following directives and save.

! Configuration File for keepalived

global_defs {
   notification_email {
  root@webserver-02.centlinux.com
   }
   notification_email_from root@webserver-02.centlinux.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eno16777728
    virtual_router_id 51
    priority 100 #used in election, 101 for master & 100 for backup
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.116.50/24
    }
}

Start and enable keepalived service.

# systemctl start keepalived ; systemctl enable keepalived
ln -s '/usr/lib/systemd/system/keepalived.service' '/etc/systemd/system/multi-user.target.wants/keepalived.service'

Check IP Address of the server.

# ip addr | grep "inet" | grep "eno16777728"
    inet 192.168.116.32/24 brd 192.168.116.255 scope global eno16777728
    inet 192.168.116.50/24 scope global secondary eno16777728

You might observe that the Floating IP: 192.168.116.50 has been assigned to the network interface.

Testing KeepAlived Configuration

Open the Floating IP Address http://192.168.116.50 in a browser.

webserver-01 page 1

Refresh webpage multiple times, and you will always get the result from same server i.e. webserver-01.centlinux.com.

Now, make webserver-01.centlinux.com unavailable by disconnecting its network connection.

# nmcli c down eno16777728

Again Refresh your webpage.

webserver-02 page 1

You can see that, due to unavailability of webserver-01.centlinux.com the Floating IP is now moved to webserver-02.centlinux.com.

Conclusion – Install KeepAlived on CentOS 7

We have successfully install KeepAlived on CentOS 7 and configured Floating IP on two server nodes. This article is written completely in command line environment, therefore if you feel any difficulty, then you should buy and read The Linux Command Line, 2nd Edition: A Complete Introduction (PAID LINK) by William Shotts.

Alaric Bird

Alaric Bird is a seasoned Linux System Administrator with over a decade of experience in managing and optimizing Linux-based servers and infrastructure. Known for his expertise in server deployment, security hardening, and performance tuning, Alaric has a deep understanding of various Linux distributions, including Ubuntu, CentOS, and Red Hat Enterprise Linux. His skills extend to cloud platforms like AWS, where he effectively manages virtual private servers and services. Alaric is also proficient in scripting languages such as Bash and Python, which he uses to automate routine tasks, enhancing efficiency and reliability. With a strong commitment to continuous learning, he stays updated with the latest developments in open-source technologies and best practices. His problem-solving abilities, combined with excellent communication skills, make him a valuable asset to any IT team. In addition to his technical expertise, Alaric is passionate about mentoring junior administrators and fostering a collaborative environment.

Recent Posts

Change Apache Document Root in Linux

Learn how to change Apache document root in Linux by following this step-by-step guide. Adjust…

2 weeks ago

How to Change Apache Port in Linux

Discover how to change Apache port in Linux easily. Follow our simple guide to modify…

2 weeks ago

How to Create Virtual Host in Apache Server

Learn how to create a virtual host in Apache Server with this comprehensive guide. Set…

3 weeks ago

10 Practical Tasks for RHCSA Exam with Solutions

Discover 10 practical tasks for the RHCSA exam with step-by-step solutions. Boost your Linux skills…

3 weeks ago

Ultimate Fail2ban Configuration Guide

Discover the ultimate Fail2ban configuration guide. Learn how to set up, customize, and optimize Fail2ban…

3 weeks ago

VPS Server: The Ultimate Guide to Virtual Private Servers

Explore the ultimate guide to VPS servers and learn everything about Virtual Private Servers, including…

4 weeks ago

This website uses cookies.