Two nodes (Web Server) Load balance and Fail-over with keepalived in RHEL

Posted: September 16, 2015 in KeepAlived

Two nodes (Web Server) Load balance and Failover with keepalived

Screenshot_4

Nodes and IP Details

LB-Primary Node – 192.168.183.139

LB-Backup Node – 192.168.183.140

Web Server1 – 192.168.183.137

Web Server 2 – 192.168.183.138

VIP – 192.168.183.150

 

For Testing Keepalived Setup Lab Requirement

Hardware: 512MB RAM I core Processor and 10 GB HDD

OS: RHEL 6.4+ (minimal installation) and Yum repository

Screenshot_7

Web Server Node Setup

Install and configure Web Service with Basic Setup. Install wget, tcpdump, VIM packages (for Testing) using yum repository.

 

Screenshot_6

 

LB-Primary Node Installation and configuration

 

Install Required Packages

# yum install gcc kernel-headers kernel-devel

# yum install wget tcpdump vim ipvsadm

# yum install keepalived

 

Edit Keepalived.conf Configuration

#vim /etc/keepalived/keepalived.conf

vrrp_instance master_lb {

state MASTER

interface eth0

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.183.150/24

}

}

 

virtual_server 192.168.183.150 80 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 50

protocol TCP

 

real_server 192.168.183.138 80 {

weight 1

HTTP_GET {

url {

path /index.html

}

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

 

real_server 192.168.183.137 80 {

weight 1

HTTP_GET {

url {

path /index.html

}

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

}

 

Restart Keepalived Service

[root@LB1 ~]# /etc/init.d/keepalived restart

Stopping keepalived:                                       [ OK ]

Starting keepalived:                                       [ OK ]

 

[root@LB1 ~]# chkconfig keepalived on

 

[root@LB1 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:c2:93:95 brd ff:ff:ff:ff:ff:ff

inet 192.168.183.139/24 brd 192.168.183.255 scope global eth0

inet 192.168.183.150/24 scope global secondary eth0

inet6 fe80::20c:29ff:fec2:9395/64 scope link

valid_lft forever preferred_lft forever

 

 

 

LB-Backup Node Installation and configuration

 

Install Required Packages

# yum install gcc kernel-headers kernel-devel

# yum install wget tcpdump vim ipvsadm

# yum install keepalived

 

Edit Keepalived.conf Configuration

#vim /etc/keepalived/keepalived.conf

vrrp_instance Backup_lb {

state MASTER

interface eth0

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.183.150/24

}

}

 

virtual_server 192.168.183.150 80 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 50

protocol TCP

 

real_server 192.168.183.138 80 {

weight 1

HTTP_GET {

url {

path /index.html

}

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

 

real_server 192.168.183.137 80 {

weight 1

HTTP_GET {

url {

path /index.html

}

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

}

 

Restart Keepalived Service

[root@LB2 ~]# /etc/init.d/keepalived restart

Stopping keepalived:                                       [ OK ]

Starting keepalived:                                       [ OK ]

 

[root@LB2 ~]# chkconfig keepalived on

 

[root@LB2 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:c2:93:95 brd ff:ff:ff:ff:ff:ff

inet 192.168.183.139/24 brd 192.168.183.255 scope global eth0

inet6 fe80::20c:29ff:fec2:9395/64 scope link

valid_lft forever preferred_lft forever

 

Add Virtual IP to access Web server via iptables in Both Web Server 1 and Web Server 2 node

 

Screenshot_13

Web Server 1 Node

[root@one ~]# iptables -t nat -I PREROUTING –src 0/0 –dst 192.168.183.150 -p tcp –dport 80 -j REDIRECT –to-ports 80

 

Web Server 2 Node

[root@two ~]# iptables -t nat -I PREROUTING –src 0/0 –dst 192.168.183.150 -p tcp –dport 80 -j REDIRECT –to-ports 80

 

Verify able to access the web Server using VIP

Screenshot_8

Screenshot_9

Screenshot_10

Testing LB Node Failover

Manually down the Master Node and Test able to access Web service?

[root@LB1 ~]# init 0

[root@LB1 ~]#

Screenshot_12

Backup Node should contain VIP after failover from Master Node

# [root@LB2 ~]# ip a

eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:c2:de:b3 brd ff:ff:ff:ff:ff:ff

inet 192.168.183.140/24 brd 192.168.183.255 scope global eth0

   inet 192.168.183.150/32 scope global eth0

inet6 fe80:: 20c:29ff:fec2:deb3/64 scope link

 

Screenshot_11

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s