Author Photo

Ahmer's SysAdmin Recipes is a blog for Linux System Administrators. This blog provides recipes for Installation & Configuration of Linux, Ubuntu, Oracle Database, MySQL, Apache, Nginx, Oracle Cloud Control, FreeIPA, Kerberos & OpenLDAP, Subversion, PXE, DevOps, etc. over Red Hat Enterprise Linux (RHEL), CentOS, Ubuntu and Windows.

Please give me your feedback and help me improve this blog. Please let me know If you want me to write on a specific topic.

Sunday, 19 August 2018

keepalived: Configure Floating IP in CentOS 7 Servers

keepalived-configure-floating-ip-in-centos-7-serversA 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 is a routing software written in C. The main goal of this project is to provide simple and robust facilities for loadbalancing 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 loadbalancing. Keepalived implements a set of checkers to dynamically and adaptively maintain and manage loadbalanced 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.

In this post, we will configure a Floating IP address for the cluster of two webserver nodes using KeepAlived.

 

System Specification:

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

Hostname webserver-01.example.com webserver-02.example.com
IP Address 192.168.116.31/24 192.168.116.32/24
Operating System RHEL 7 RHEL 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.

For Reference  : Install Linux, Apache, MySQL & PHP (LAMP) Stack
  Install Linux, Nginx, MySQL & PHP (LEMP) Stack

Webserver-01 Main PageWebserver-02 Main Page

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

 

Configure KeepAlived on webserver-01:

Connect to webserver-01.example.com and install keepalived.

[root@webserver-01 ~]# yum install -y keepalived Loaded plugins: langpacks, product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. localyum | 4.1 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package keepalived.x86_64 0:1.2.10-2.el7 will be installed --> Processing Dependency: libnetsnmp.so.31()(64bit) for package: keepalived-1.2.10-2.el7.x86_64 --> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: keepalived-1.2.10-2.el7.x86_64 --> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: keepalived-1.2.10-2.el7.x86_64 --> Running transaction check ---> Package net-snmp-agent-libs.x86_64 1:5.7.2-18.el7 will be installed ---> Package net-snmp-libs.x86_64 1:5.7.2-18.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: keepalived x86_64 1.2.10-2.el7 localyum 218 k Installing for dependencies: net-snmp-agent-libs x86_64 1:5.7.2-18.el7 localyum 698 k net-snmp-libs x86_64 1:5.7.2-18.el7 localyum 745 k Transaction Summary ================================================================================ Install 1 Package (+2 Dependent packages) Total download size: 1.6 M Installed size: 5.6 M Downloading packages: -------------------------------------------------------------------------------- Total 2.6 MB/s | 1.6 MB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 1:net-snmp-libs-5.7.2-18.el7.x86_64 1/3 Installing : 1:net-snmp-agent-libs-5.7.2-18.el7.x86_64 2/3 Installing : keepalived-1.2.10-2.el7.x86_64 3/3 Verifying : 1:net-snmp-libs-5.7.2-18.el7.x86_64 1/3 Verifying : 1:net-snmp-agent-libs-5.7.2-18.el7.x86_64 2/3 Verifying : keepalived-1.2.10-2.el7.x86_64 3/3 Installed: keepalived.x86_64 0:1.2.10-2.el7 Dependency Installed: net-snmp-agent-libs.x86_64 1:5.7.2-18.el7 net-snmp-libs.x86_64 1:5.7.2-18.el7 Complete!

Set Linux Kernel parameters as follows to support Floating IP.

[root@webserver-01 ~]# echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf [root@webserver-01 ~]# sysctl -p net.ipv4.ip_nonlocal_bind = 1 [root@webserver-01 ~]#

Now configure keepalived settings.

[root@webserver-01 ~]# cd /etc/keepalived/ [root@webserver-01 keepalived]# mv keepalived.conf keepalived.conf.org [root@webserver-01 keepalived]# vi keepalived.conf

Add following directives and save.

! Configuration File for keepalived global_defs { notification_email { root@webserver-01.example.com } notification_email_from root@webserver-01.example.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.

[root@webserver-01 keepalived]# 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.

[root@webserver-01 keepalived]# 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 [root@webserver-01 keepalived]#

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.example.com and install keepalived.

[root@webserver-02 ~]# yum install -y keepalived Loaded plugins: langpacks, product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. localyum | 4.1 kB 00:00 nginx | 2.9 kB 00:01 rpmforge | 1.9 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package keepalived.x86_64 0:1.2.10-2.el7 will be installed --> Processing Dependency: libnetsnmp.so.31()(64bit) for package: keepalived-1.2.10-2.el7.x86_64 --> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: keepalived-1.2.10-2.el7.x86_64 --> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: keepalived-1.2.10-2.el7.x86_64 --> Running transaction check ---> Package net-snmp-agent-libs.x86_64 1:5.7.2-18.el7 will be installed ---> Package net-snmp-libs.x86_64 1:5.7.2-18.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: keepalived x86_64 1.2.10-2.el7 localyum 218 k Installing for dependencies: net-snmp-agent-libs x86_64 1:5.7.2-18.el7 localyum 698 k net-snmp-libs x86_64 1:5.7.2-18.el7 localyum 745 k Transaction Summary ================================================================================ Install 1 Package (+2 Dependent packages) Total download size: 1.6 M Installed size: 5.6 M Downloading packages: -------------------------------------------------------------------------------- Total 11 MB/s | 1.6 MB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 1:net-snmp-libs-5.7.2-18.el7.x86_64 1/3 Installing : 1:net-snmp-agent-libs-5.7.2-18.el7.x86_64 2/3 Installing : keepalived-1.2.10-2.el7.x86_64 3/3 Verifying : 1:net-snmp-libs-5.7.2-18.el7.x86_64 1/3 Verifying : 1:net-snmp-agent-libs-5.7.2-18.el7.x86_64 2/3 Verifying : keepalived-1.2.10-2.el7.x86_64 3/3 Installed: keepalived.x86_64 0:1.2.10-2.el7 Dependency Installed: net-snmp-agent-libs.x86_64 1:5.7.2-18.el7 net-snmp-libs.x86_64 1:5.7.2-18.el7 Complete!

Set Linux Kernel parameters as follows to support Floating IP.

[root@webserver-02 ~]# echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf [root@webserver-02 ~]# sysctl -p net.ipv4.ip_nonlocal_bind = 1

Now configure keepalived settings.

[root@webserver-02 ~]# cd /etc/keepalived/ [root@webserver-02 keepalived]# mv keepalived.conf keepalived.conf.org [root@webserver-02 keepalived]# vi keepalived.conf

Add following directives and save.

! Configuration File for keepalived global_defs { notification_email { root@webserver-02.example.com } notification_email_from root@webserver-02.example.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 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.

[root@webserver-02 keepalived]# 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.

[root@webserver-02 keepalived]# 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 [root@webserver-02 keepalived]#

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 Main Page Through Floating IP

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

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

[root@webserver-01 ~]# nmcli c down eno16777728

Again Refresh your webpage.

Webserver-02 Main Page Through Floating IP

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

It shows that our High Availability Servers are configured successfully using KeepAlived Floating IP.

keepalived: Configure Floating IP in CentOS 7 Servers


YOU MIGHT ALSO LIKE:

5 comments:

  1. very useful thanks alot

    ReplyDelete
  2. Great explanation sir. Thankyou

    ReplyDelete
  3. small doubt after you moving the keepalived conf file from that path after that how you can open the .conf file from same path.

    [root@webserver-02 ~]# cd /etc/keepalived/
    [root@webserver-02 keepalived]# mv keepalived.conf keepalived.conf.org
    [root@webserver-02 keepalived]# vi keepalived.conf (how can you open it from hear after .conf file is moved to keepa;ived.conf.org)

    ReplyDelete
    Replies
    1. Hi,
      The vi command creates an empty file and then we are adding configurations in it.
      Hope it clarifies your doubt.

      Delete