LVS/DR + Keepalived搭建负载均衡集群

  • A+
所属分类:linux服务器应用
摘要

需要有一种机制用来检测real server的状态,这就是keepalived。它的作用除了可以检测rs状态外,还可以检测备用director的状态,也

前面的LVS虽然已经配置成功也实现了负载均衡(见 http://www.linuxidc.com/Linux/2015-06/118646.htm),但是我们测试的时候发现,当某台real server把nginx进程停掉,那么director照样会把请求转发过去,这样就造成了某些请求不正常。所以需要有一种机制用来检测real server的状态,这就是keepalived。它的作用除了可以检测rs状态外,还可以检测备用director的状态,也就是说keepalived可以实现ha集群的功能,当然了也需要一台备用director。
备用director也需要安装一下keepalived软件、ipvsadm;
keepalived调用lvs来实现自己的规则;
yum install -y keepalived ipvsadm

环境搭建工作:
主director    192.168.11.30    eth1网卡
从director    192.168.11.40    eth1网卡
real server1:    192.168.11.100    eth0网卡
real server2:    192.168.11.101    eth0网卡

用来curl测试的linux主机 192.168.11.0 网段即可;
主dr和备用dr都需要安装keepalived,ipvsadm;
两台rs安装nginx;

安装好后,主director的配置文件
vim /etc/keepalived/keepalived.conf  //加入如下:
vrrp_instance VI_1 {
    state MASTER  #备用服务器上为 BACKUP
    interface eth1
    virtual_router_id 51
    priority 100  #优先级,数值越大优先级越高;备用服务器上为90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.11.110
    }
}
virtual_server 192.168.11.110 80 {
    delay_loop 6                  #(每隔6秒查询realserver状态,是否存活)
    lb_algo wlc                  #(轮询算法)
    lb_kind DR                  #(Direct Route)
    persistence_timeout 0    #(同一IP的连接多少秒内被分配到同一台realserver,0表示不连接)
    protocol TCP                #(用TCP协议检查realserver状态)
    real_server 192.168.11.100 80 {
        weight 100              #(权重)
        TCP_CHECK {
        connect_timeout 10      #(10秒无响应超时)
        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
        }
    }
}

从director的配置文件只需要修改下面两项:
state MASTER  -> state BACKUP
priority 100 -> priority 90
配置完keepalived后,需要开启端口转发(主从dr都要做):
echo 1 > /proc/sys/net/ipv4/ip_forward
然后,两个rs上执行 /usr/local/sbin/lvs_dr_rs.sh 脚本,启动nginx服务
# /etc/init.d/nginx start

最后,两个director上启动keepalived服务(先主后从):
# /etc/init.d/keepalived start

另外,需要注意的是,启动keepalived服务会自动生成vip和ipvsadm规则.

使用命令#ip addr    查看dr的虚拟ip地址;直接使用ifconfig不显示虚拟ip;
[root@dr1 keepalived]# ip addr
eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    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 global eth1
   
    inet 192.168.11.110/32 scope global eth1
    inet6 fe80::20c:29ff:fe97:c3f6/64 scope link 
    valid_lft forever preferred_lft forever

在其他机器curl测试,请求rs1和rs2次数相当;
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs2rs2
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs2rs2

rs2上面stop nginx,然后curl测试,发现所有的请求都到rs1上面了;
日志里面也会记录remove rs2;日志文件:/var/log/messages
[root@rs2 ~]# /etc/init.d/nginx stop

[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs1rs1

[root@dr1 ~]# tail -2 /var/log/messages
Jun  9 23:27:19 localhost Keepalived_healthcheckers[1572]: TCP connection to [192.168.11.101]:80 failed !!!
Jun  9 23:27:19 localhost Keepalived_healthcheckers[1572]: Removing service [192.168.11.101]:80 from VS [192.168.11.110]:80

rs2启动nginx,日志文件记录adding rs2;curl测试,发现请求平均分配到rs1和rs2上面了;

[root@rs2 ~]# /etc/init.d/nginx start

[root@dr1 ~]# tail -2 /var/log/messages
Jun  9 23:31:38 localhost Keepalived_healthcheckers[1572]: TCP connection to [192.168.11.101]:80 success.
Jun  9 23:31:38 localhost Keepalived_healthcheckers[1572]: Adding service [192.168.11.101]:80 to VS [192.168.11.110]:80

Linux下搭建HA高可用集群

HA 即 (high available)高可用,又被叫做双机热备,用于关键性业务。 简单理解就是,有两台机器A和B,正常是A提供服务,B待命闲

[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs2rs2
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs2rs2

加入dr2 备用dircetor机器;
主上停止keepalive服务;stop之后,在从上ip addr查看绑定虚拟ip,说明从接管了服务;切换速度很快;
主上启动keepalived服务后,主绑定虚拟ip,接管服务;
[root@dr2 keepalived]# ip addr
eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    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 global eth1
    inet 192.168.11.110/32 scope global eth1

nc命令可以扫描端口是否打开:

在其他机器上扫描,11.100 和11.101,11.110的80端口是否打开;

#nc -z -w2 192.168.11.110 80
[root@localhost ~]# nc -z -w2 192.168.11.100 80
Connection to 192.168.11.100 80 port [tcp/http] succeeded!
[root@localhost ~]# nc -z -w2 192.168.11.101 80
Connection to 192.168.11.101 80 port [tcp/http] succeeded!
[root@localhost ~]# nc -z -w2 192.168.11.110 80
Connection to 192.168.11.110 80 port [tcp/http] succeeded!

LVS+Keepalived实现四层负载及高可用  http://www.linuxidc.com/Linux/2015-02/112695.htm

LVS+Keepalived高可用负载均衡集群架构实验  http://www.linuxidc.com/Linux/2015-01/112560.htm

Heartbeat+LVS构建高可用负载均衡集群  http://www.linuxidc.com/Linux/2014-09/106964.htm

搭建LVS负载均衡测试环境  http://www.linuxidc.com/Linux/2014-09/106636.htm

一个针对LVS的压力测试报告 http://www.linuxidc.com/Linux/2015-03/114422.htm

CentOS 6.3下Haproxy+Keepalived+Apache配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + KeepAlived 实现WEB群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Keepalived+Haproxy配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm

Haproxy+Keepalived构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm

CentOS 7 上配置LVS + Keepalived + ipvsadm http://www.linuxidc.com/Linux/2014-11/109237.htm

Keepalived高可用集群搭建 http://www.linuxidc.com/Linux/2014-09/106965.htm

除非注明,否则均为@蒲公英网原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.aipgy.com/38978.html

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: