Keepalived runs on an active LVS server as well as one or more optional backup LVS server. The active LVS server serves two roles:
- To balance the load across the real servers.
- To check the integrity of the services on each real server.
The active (master) server informs the backup server of its active status using the Virtual Router Redundancy Protocol (VRRP), which requires the master server to send out advertisements at regular intervals. If the active server stops sending advertisements, a new master is elected.
Prerequisites
- Need two linux servers any web server configured and should be up and running.
- Port 80 should we open from firewall level
In this example I am using two CentOS-7 servers and I am using nginx as my webserver. You can use any OS and webserver as you like.
Nginx Server 2 --> 192.168.1.18
Install and configure Keepalived
Thin this setup we need another additional IP which need to configure as our VIP. This VIP can we assign to either "Webserver 1" or "Webserver-2".In this example we are using 192.168.1.20 as our VIP.
- Create a loopback interface in each servers (Webserver-1 and Webserver-2)
Go to below location and create a file call "ifcfg-lo:vip". This file should contain below content.
DEVICE=lo:vipIPADDR=192.168.1.20 # Change this to your VIPNETMASK=255.255.255.255#NETWORK=# If you're having problems with gated making 127.0.0.0/8 a martian,# you can change this to something else (255.255.255.255, for example)#BROADCAST=ONBOOT=yesNAME=loopback
ifup ifcfg-lo:vip
- Install keepalived in both servers
yum install keepalived
- Enable the keepalived service
systemctl enable keepalived
- Goto keepalived configuration folder and backup the existing configuration file.
- Create a nginx status check script with below content.
#!/bin/bash_status=`pgrep -f "nginx.conf" | wc -l`if [ $_status -gt 0 ];thenexit 0elseexit 1fi
- Set Execution permission for that script
chmod 755 /usr/bin/status_nginx
- Create a keepalived.conf file and add below content in each server.
Values highlighted in yellow should be change according to your environment.
In Webserver-1
vrrp_script chk_nginx_status {script "/usr/bin/status_nginx"interval 10}vrrp_instance Float_NGINX {state BACKUPinterface enp0s3 #Interface IDvirtual_router_id 93 #This should be unique within the networkpriority 101 #101 on master, 100 on backupadvert_int 1nopreempttrack_script {chk_nginx_status #Nginx status check script}virtual_ipaddress {192.168.1.20/24 #VIP need to assign}}
virtual_server 192.168.1.20 80 {lvs_sched rrlvs_method DRprotocol TCPreal_server 192.168.1.17 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3retry 3delay_before_retry 2}}real_server 192.168.1.18 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3retry 3delay_before_retry 2}}}
In Webserver-2
vrrp_script chk_nginx_status {script "/usr/bin/status_nginx"interval 10}vrrp_instance Float_NGINX {state BACKUPinterface enp0s3 #Interface IDvirtual_router_id 93 #This should be unique within the networkpriority 100 #101 on master, 100 on backupadvert_int 1nopreempttrack_script {chk_nginx_status #Nginx status check script}virtual_ipaddress {192.168.1.20/24 #VIP need to assign}}
virtual_server 192.168.1.20 80 {lvs_sched rrlvs_method DRprotocol TCPreal_server 192.168.1.17 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3retry 3delay_before_retry 2}}real_server 192.168.1.18 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3retry 3delay_before_retry 2}}}
- vrrp_instance defines an individual instance of the VRRP protocol running on an interface. I have arbitrarily named this instance VI_1.
- state defines the initial state that the instance should start in.
- interface defines the interface that VRRP runs on.
- virtual_router_id is the unique identifier that you learned about in the first article of this series.
- priority is the advertised priority that you learned about in the first article of this series. As you will learn in the next article, priorities can be adjusted at runtime.
- advert_int specifies the frequency that advertisements are sent at (1 second, in this case).
- virtual_ipaddress defines the IP addresses (there can be multiple) that VRRP is responsible for.
Now check the VIP is successfuly assign to a server. It can be Webserver-1 or Webserver-2
You can simply type "ip addr" command to verify this.
You have to install "ipvsadm" package to inspect the virtual server table. From this tool you can see available nodes under your keepalived instance.
Type below command to get the available instance
ipvsadm -L -n


























































