LVS/DR + keepalived build Load Balancer cluster

Source: Internet
Author: User
Tags glob

Although the previous LVS have been configured successfully and load balanced, we found that when a real server stopped the Nginx process, the Director would still forward the request to the past, which caused some requests to be abnormal. So there needs to be a mechanism to detect the state of real server, which is keepalived. In addition to detecting the RS State, it can also detect the state of the standby director, that is, keepalived can implement the HA cluster function, of course, also need a standby director.

Standby Director also need to install keepalived software,ipvsadm;

Keepalived calls LVs to implement its own rules;

Yum install-y keepalived Ipvsadm


Environment Construction work:

Master Director 192.168.11.30 eth1 NIC

From director 192.168.11.40 eth1 NIC

Real server1:192.168.11.100 eth0 NIC

Real server2:192.168.11.101 eth0 NIC

The Linux host 192.168.11.0 network segment for curl testing;

Both the primary DR and the standby Dr are required to install keepalived,ipvsadm;

Two RS installation nginx;


After installation, the Master director's configuration file

vim/etc/keepalived/keepalived.conf//Join as follows:

vrrp_instance vi_1 {    state master    #备用服务器上为  backup     interface eth1    virtual_router_id 51     priority 100   #优先级, the higher the value the greater the priority; 90    advert_int 1  on the standby server    authentication {        auth_type pass         auth_pass 1111    }     virtual_ipaddress {        192.168.11.110     }}virtual_server 192.168.11.110 80 {    delay_loop 6                   # ( Query Realserver status Every 6 seconds, whether it survives)     lb_algo wlc                    # (polling algorithm)     lb_kind DR                   # (Direct Route)      persistence_timeout 0    # (the connection of the same IP is assigned to the same realserver,0 in a few seconds to indicate that it is not connected)      protocol TCP                 # (check realserver status with TCP protocol)     real_server 192.168.11.100 80  {        weight 100                # (weight)         tcp _check {        connect_timeout 10        # (10 seconds No response timeout)         nb_get_retry 3         delay_before_retry 3        connect_port 80         }    }real_server 192.168.11.101 80  {        weight 100         TCP_CHECK {        connect_timeout 10         nb_get_retry 3         delay_before_retry 3        connect_port 80         }     }}

The configuration file from director only needs to modify the following two items:

State MASTER-State BACKUP

Priority 90

After configuring keepalived, you need to turn on port forwarding (master and slave Dr All):

Echo 1 >/proc/sys/net/ipv4/ip_forward

Then, execute the/usr/local/sbin/lvs_dr_rs.sh script on two RS and start the Nginx service

#/etc/init.d/nginx Start

Finally, the two director starts the Keepalived Service (Guthrie):

#/etc/init.d/keepalived Start

Also, be aware that starting the Keepalived service automatically generates VIP and IPVSADM rules.


Use the command #ip addr to view the virtual IP address of Dr, and use Ifconfig to not display the virtual IP;

[[Email protected] keepalived]# IP addreth1: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU qdisc pfifo_fast State unkn OWN qlen link/ether 00:0c:29:97:c3:f6 brd ff:ff:ff:ff:ff:ff inet 192.168.11.30/24 BRD 192.168.11.255 Scope glob Al eth1 inet 192.168.11.110/32 scope Global eth1 inet6 fe80::20c:29ff:fe97:c3f6/64 scope link Valid_lft foreve R Preferred_lft Forever

In other machines Curl test, request rs1 and rs2 number of times equal;

[[email protected] ~]# Curl 192.168.11.110rs1rs1[[email protected] ~]# Curl 192.168.11.110rs2rs2[[email protected]t ~]# C URL 192.168.11.110rs1rs1[[email protected] ~]# Curl 192.168.11.110rs2rs2

Rs2 above the stop nginx, then curl Test, found all the requests to rs1 above;

The log will also record the Remove rs2; log file:/var/log/messages

[[email protected] ~]#/etc/init.d/nginx stop
[[email protected] ~]# Curl 192.168.11.110rs1rs1[[email protected] ~]# Curl 192.168.11.110rs1rs1[[email protected] ~]# CU RL 192.168.11.110rs1rs1[[email protected] ~]# Curl 192.168.11.110rs1rs1
[[email protected] ~]# tail-2/var/log/messagesjun 9 23:27:19 localhost keepalived_healthcheckers[1572]: TCP connection to [192.168.11.101]:80 failed!!! June 9 23:27:19 localhost keepalived_healthcheckers[1572]: removing service [192.168.11.101]:80 from VS [192.168.11.110]: 80


RS2 start Nginx, log file records adding Rs2;curl test, found that the request was evenly distributed to rs1 and RS2;

[[email protected] ~]#/etc/init.d/nginx start
[[email protected] ~]# tail-2/var/log/messagesjun 9 23:31:38 localhost keepalived_healthcheckers[1572]: TCP connection to [192.168.11.101]:80 success. June 9 23:31:38 localhost keepalived_healthcheckers[1572]: Adding service [192.168.11.101]:80 to VS [192.168.11.110]:80
[[email protected] ~]# Curl 192.168.11.110rs1rs1[[email protected] ~]# Curl 192.168.11.110rs2rs2[[email protected] ~]# CU RL 192.168.11.110rs1rs1[[email protected] ~]# Curl 192.168.11.110rs2rs2


Join DR2 standby Dircetor machine;

The Lord stopped keepalive service; After stop, view the bound virtual IP from the upper IP addr, the instructions from taking over the service; switching speed quickly;

After the Lord initiates the Keepalived service, the master binds the virtual IP, takes over the service;

[[Email protected] keepalived]# IP addreth1: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU qdisc pfifo_fast State unkn OWN qlen link/ether 00:0c:29:af:73:3f brd ff:ff:ff:ff:ff:ff inet 192.168.11.40/24 BRD 192.168.11.255 Scope glob Al eth1 inet 192.168.11.110/32 Scope Global eth1


The NC command can scan whether the port is open:

Scan on other machines, 11.100 and 11.101,11.110 80 ports open;

#nc-Z-w2 192.168.11.110 80

[Email protected] ~]# nc-z-w2 192.168.11.100 80Connection to 192.168.11.100 Port [Tcp/http] succeeded! [Email protected] ~]# nc-z-w2 192.168.11.101 80Connection to 192.168.11.101 Port [Tcp/http] succeeded! [Email protected] ~]# nc-z-w2 192.168.11.110 80Connection to 192.168.11.110 Port [Tcp/http] succeeded!



This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://8802265.blog.51cto.com/8792265/1660329

LVS/DR + keepalived build Load Balancer cluster

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.