前面的lvs虽然已经配置成功也实现了负载均衡,但是我们测试的时候发现,当某台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;
[[email protected] 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次数相当;
[[email protected] ~]# curl 192.168.11.110 rs1rs1 [[email protected] ~]# curl 192.168.11.110 rs2rs2 [[email protected] ~]# curl 192.168.11.110 rs1rs1 [[email protected] ~]# curl 192.168.11.110 rs2rs2
rs2上面stop nginx,然后curl测试,发现所有的请求都到rs1上面了;
日志里面也会记录remove rs2;日志文件:/var/log/messages
[[email protected] ~]# /etc/init.d/nginx stop
[[email protected] ~]# curl 192.168.11.110 rs1rs1 [[email protected] ~]# curl 192.168.11.110 rs1rs1 [[email protected] ~]# curl 192.168.11.110 rs1rs1 [[email protected] ~]# curl 192.168.11.110 rs1rs1
[[email protected] ~]# 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上面了;
[[email protected] ~]# /etc/init.d/nginx start
[[email protected] ~]# 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
[[email protected] ~]# curl 192.168.11.110 rs1rs1 [[email protected] ~]# curl 192.168.11.110 rs2rs2 [[email protected] ~]# curl 192.168.11.110 rs1rs1 [[email protected]alhost ~]# curl 192.168.11.110 rs2rs2
加入dr2 备用dircetor机器;
主上停止keepalive服务;stop之后,在从上ip addr查看绑定虚拟ip,说明从接管了服务;切换速度很快;
主上启动keepalived服务后,主绑定虚拟ip,接管服务;
[[email protected] 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
[[email protected] ~]# nc -z -w2 192.168.11.100 80 Connection to 192.168.11.100 80 port [tcp/http] succeeded! [[email protected] ~]# nc -z -w2 192.168.11.101 80 Connection to 192.168.11.101 80 port [tcp/http] succeeded! [[email protected] ~]# nc -z -w2 192.168.11.110 80 Connection to 192.168.11.110 80 port [tcp/http] succeeded!