lvs+keepalived实现负载均衡双机热备高可用
在上一章中我们用linux内核模块ip_vsy也就是lvs实现了负载均衡,但随着新的问题就出现了:
– 某台RealServer down了,怎么办?
– LVS本身down了,怎么办?
解决思路:
– 某台RealServer down了,怎么办? — 健康检测
– LVS本身down了,怎么办?—LVS冗余
实现方法:Keepalived – LVS管理软件
– 健康检测:支持4/7监测;
– 主备冗余:采用VRRP协议癿HeartBeat;
– 如何配置?— 配置文件
Keepalived –f /etc/keepalived/keepalived.conf
具体操作:
1. 首先把之前在调度器书写的策略和添加的vip(172.25.32.100)地址清理掉,之后交给keepalived来维护,如果手动维护的话会造成冲突。
清理前的调度器策略和vip地址 [root@lvs1 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.25.32.100:80 rr -> 172.25.32.7:80 Route 1 0 2 -> 172.25.32.8:80 Route 1 0 1 [root@lvs1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 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 group default qlen 1000 link/ether 52:54:00:8f:ed:32 brd ff:ff:ff:ff:ff:ff inet 172.25.32.6/24 brd 172.25.32.255 scope global eth0 valid_lft forever preferred_lft forever inet 172.25.32.100/24 scope global secondary eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe8f:ed32/64 scope link valid_lft forever preferred_lft forever 清理调度器策略和vip(172.25.32.100)地址 [root@lvs1 ~]# ipvsadm -D -t 172.25.32.100:80 [root@lvs1 ~]# ip addr del 172.25.32.100/24 dev eth0 清理后查看策略和vip(172.25.32.100)地址已经没了 [root@lvs1 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@lvs1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 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 group default qlen 1000 link/ether 52:54:00:8f:ed:32 brd ff:ff:ff:ff:ff:ff inet 172.25.32.6/24 brd 172.25.32.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe8f:ed32/64 scope link valid_lft forever preferred_lft forever
2. 在调度节点下载keepalived
[root@lvs1 ~]# yum install keepalived.x86_64 -y Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package keepalived.x86_64 0:1.3.5-6.el7 will be installed --> Processing Dependency: libnetsnmp.so.31()(64bit) for package: keepalived-1.3.5-6.el7.x86_64 --> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: keepalived-1.3.5-6.el7.x86_64 --> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: keepalived-1.3.5-6.el7.x86_64 --> Running transaction check ---> Package net-snmp-agent-libs.x86_64 1:5.7.2-37.el7 will be installed --> Processing Dependency: libsensors.so.4()(64bit) for package: 1:net-snmp-agent-libs-5.7.2-37.el7.x86_64 ---> Package net-snmp-libs.x86_64 1:5.7.2-37.el7 will be installed --> Running transaction check ---> Package lm_sensors-libs.x86_64 0:3.4.0-6.20160601gitf9185e5.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================================================================================== Installing: keepalived x86_64 1.3.5-6.el7 dvd 329 k Installing for dependencies: lm_sensors-libs x86_64 3.4.0-6.20160601gitf9185e5.el7 dvd 42 k net-snmp-agent-libs x86_64 1:5.7.2-37.el7 dvd 705 k net-snmp-libs .......
3.编辑keepalived的配置文件(在/etc/keepalived/keepalived.conf),编辑好后设置开机自启
[root@lvs1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost ##邮件通知从外网改成本机 } notification_email_from keepalived@localhost ##由本机的keepalived用户发出邮件 smtp_server 127.0.0.1 ##本机默认有邮箱,端口默认是25, 只能用与本机 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr # vrrp_strict ###严格遵守VRRP协议,启用此项后以下状况将无法启动服务: 1.无VIP地址 2.配置了单播邻居 3.在VRRP版本2中有IPv6地址,开启动此项并且没有配置vrrp_iptables时会自动开启iptables防火墙规则,默认导致VIP无法访问,建议不加此项配置 vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER ##作为keepalived的master主节点 interface eth0 ##如果你是虚拟机的话,一定要注意查看你的网卡名称,然后改成你自己的 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.25.32.100 ##定义成要被keepalived接管的vip地址 } } virtual_server 172.25.32.100 80 { ##改成vip地址,端口是http的80 delay_loop 6 ##每隔6秒对后端服务器进行健康检测,你也可以设置快一点 lb_algo rr ##使用rr算法 lb_kind DR ##使用DR模式 #persistence_timeout 50 ##意思是持久连接,禁掉这个选项是因为想要看到轮叫的效果,如果加上去的话50秒内调度器会把请求发给同一个后端服务器进行处理,50秒内看到的调度不变 protocol TCP real_server 172.25.32.7 80 { ##定义后端服务器地址 weight 1 ##权重为1 TCP_CHECK { ##将七层的SSL_GET检测改为四层的TCP_CHECK检测 connect_timeout 3 delay_before_retry 3 } } real_server 172.25.32.8 80 { ##和上面的172.25.32.7后端服务器配置一样,但是改成自己的ip地址 weight 1 TCP_CHECK { connect_timeout 3 delay_before_retry 3 } } } [root@lvs1 ~]# systemctl enable --now keepalived.service Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
注意:我这个配置文件做了优化,删除了一些用不上的,所以会和刚下载好软件进来配置文件的时候不太一样。
4. 在调度器查看刚才删除的vip(172.25.32.100)地址和调度器定义的策略,有没有在keeplived配置文件定义启动后重新添加。
[root@lvs1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 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 group default qlen 1000 link/ether 52:54:00:8f:ed:32 brd ff:ff:ff:ff:ff:ff inet 172.25.32.6/24 brd 172.25.32.255 scope global eth0 valid_lft forever preferred_lft forever inet 172.25.32.100/32 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe8f:ed32/64 scope link valid_lft forever preferred_lft forever [root@lvs1 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.25.32.100:80 rr -> 172.25.32.7:80 Route 1 0 15 -> 172.25.32.8:80 Route 1 0 15
在客户端进行访问,看有没有达到负载均衡
[root@foundation32 ~]# for i in {1..10}; do curl 172.25.32.100;done lv2 lv3 lv2 lv3 lv2 lv3 lv2 lv3 lv2 lv3
我们通过keepalived的这种方式实现了lvs的负载均衡,现在来实现keepalived的高可用,在新的keepalived服务器中进行设置(这里的主机名为keepalived,ip为172.25.32.9)。
1. 下载ipvsadm和keepalived
[root@keepalived ~]# yum install ipvsadm.x86_64 -y Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package ipvsadm.x86_64 0:1.27-7.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================================================================================== Installing: ipvsadm x86_64 1.27-7.el7 dvd 45 k Transaction Summary =================================================================================================================================================================================================================== Install 1 Package Total download size: 45 k Installed size: 75 k Downloading packages: ipvsadm-1.27-7.el7.x86_64.rpm | 45 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : ipvsadm-1.27-7.el7.x86_64 1/1 Verifying : ipvsadm-1.27-7.el7.x86_64 1/1 Installed: ipvsadm.x86_64 0:1.27-7.el7 Complete! [root@keepalived ~]# yum install keepalived.x86_64 -y Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package keepalived.x86_64 0:1.3.5-6.el7 will be installed --> Processing Dependency: libnetsnmp.so.31()(64bit) for package: keepalived-1.3.5-6.el7.x86_64 --> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: keepalived-1.3.5-6.el7.x86_64 --> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: keepalived-1.3.5-6.el7.x86_64 --> Running transaction check ---> Package net-snmp-agent-libs.x86_64 1:5.7.2-37.el7 will be installed --> Processing Dependency: libsensors.so.4()(64bit) for package: 1:net-snmp-agent-libs-5.7.2-37.el7.x86_64 ---> Package net-snmp-libs.x86_64 1:5.7.2-37.el7 will be installed --> Running transaction check ---> Package lm_sensors-libs.x86_64 0:3.4.0-6.20160601gitf9185e5.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================================================================================== Installing: keepalived x86_64 1.3.5-6.el7 dvd 329 k Installing for dependencies: lm_sensors-libs x86_64 3.4.0-6.20160601gitf9185e5.el7 dvd 42 k net-snmp-agent-libs x86_64 1:5.7.2-37.el7 dvd 705 k net-snmp-libs x86_64 1:5.7.2-37.el7 dvd 749 k Transaction Summary =================================================================================================================================================================================================================== Install 1 Package (+3 Dependent packages) Total download size: 1.8 M Installed size: 6.0 M Downloading packages: (1/4): keepalived-1.3.5-6.el7.x86_64.rpm | 329 kB 00:00:00 (2/4): net-snmp-agent-libs-5.7.2-37.el7.x86_64.rpm | 705 kB 00:00:00 (3/4): lm_sensors-libs-3.4.0-6.20160601gitf9185e5.el7.x86_64.rpm | 42 kB 00:00:00 (4/4): net-snmp-libs-5.7.2-37.el7.x86_64.rpm | 749 kB 00:00:00 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 42 MB/s | 1.8 MB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 1:net-snmp-libs-5.7.2-37.el7.x86_64 1/4 Installing : lm_sensors-libs-3.4.0-6.20160601gitf9185e5.el7.x86_64 2/4 Installing : 1:net-snmp-agent-libs-5.7.2-37.el7.x86_64 3/4 Installing : keepalived-1.3.5-6.el7.x86_64 4/4 Verifying : lm_sensors-libs-3.4.0-6.20160601gitf9185e5.el7.x86_64 1/4 Verifying : 1:net-snmp-libs-5.7.2-37.el7.x86_64 2/4 Verifying : keepalived-1.3.5-6.el7.x86_64 3/4 Verifying : 1:net-snmp-agent-libs-5.7.2-37.el7.x86_64 4/4 Installed: keepalived.x86_64 0:1.3.5-6.el7 Dependency Installed: lm_sensors-libs.x86_64 0:3.4.0-6.20160601gitf9185e5.el7 net-snmp-agent-libs.x86_64 1:5.7.2-37.el7 net-snmp-libs.x86_64 1:5.7.2-37.el7 Complete!
2. 将刚才在调度服务器中修改的keepalived配置文件scp到新的keepalived主机的/etc/keepalived/中
[root@lvs1 ~]# scp /etc/keepalived/keepalived.conf keepalived:/etc/keepalived/ The authenticity of host 'keepalived (172.25.32.9)' can't be established. ECDSA key fingerprint is SHA256:cxguNL6GA/BmJr4mD3W2Q2f6BkBBFPGKMPjOlEjWhDU. ECDSA key fingerprint is MD5:fe:01:b4:23:61:3f:1e:c8:e4:6c:18:2c:0a:f8:c1:ae. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'keepalived' (ECDSA) to the list of known hosts. root@keepalived's password: keepalived.conf 100% 993 2.1MB/s 00:00
3. 修改从调度器scp到新的keepalived主机中的/etc/keepalived/keepalived.conf,修改好后设置开机自启以生效
[root@keepalived ~ ]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr # vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP ##只需要将原来的MASTER状态修改为BACKUP就好,其他的不用动 interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.25.32.100 } } virtual_server 172.25.32.100 80 { delay_loop 6 lb_algo rr lb_kind DR #persistence_timeout 50 protocol TCP real_server 172.25.32.7 80 { weight 1 TCP_CHECK { connect_timeout 3 delay_before_retry 3 } } real_server 172.25.32.8 80 { weight 1 TCP_CHECK { connect_timeout 3 delay_before_retry 3 } } } [root@keepalived ~]# systemctl enable --now keepalived.service Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
在设置开机自启keepalived生效后,BACKUP节点中策略是生效的,但vip(172.25.32.100)这个地址不会自动添加,因为它只有在MASTER主节点的调度器DOWN掉后才会身份从BACKUP自动接管调度成为MASTER节点,然后才会自动添加vip地址,当MASTER节点正常运行的时候它只做为BACKUP节点,不会自动添加vip地址。
只添加了策略,没有添加vip地址 [root@keepalived ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 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 group default qlen 1000 link/ether 52:54:00:8b:cd:4f brd ff:ff:ff:ff:ff:ff inet 172.25.32.9/24 brd 172.25.32.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe8b:cd4f/64 scope link valid_lft forever preferred_lft forever [root@keepalived ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.25.32.100:80 rr -> 172.25.32.7:80 Route 1 0 0 -> 172.25.32.8:80 Route 1 0 0
可以测试一下,停掉keepalived的MASTER节点,然后看BACKUP节点会不会自动接管,在/var/log/messages日志中可以看到身份的转换
停掉MASTRT节点的keepalived [root@lvs1 ~]# systemctl stop keepalived.service 在BACKUP节点查看身份转换 [root@keepalived ~]# cat /var/log/messages ...... May 10 04:09:08 localhost Keepalived_vrrp[14853]: VRRP_Instance(VI_1) Transition to MASTER STATE May 10 04:09:09 localhost Keepalived_vrrp[14853]: VRRP_Instance(VI_1) Entering MASTER STATE May 10 04:09:09 localhost Keepalived_vrrp[14853]: VRRP_Instance(VI_1) setting protocol VIPs. May 10 04:09:09 localhost Keepalived_vrrp[14853]: Sending gratuitous ARP on eth0 for 172.25.32.100 May 10 04:09:09 localhost Keepalived_vrrp[14853]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.25.32.100 May 10 04:09:09 localhost Keepalived_vrrp[14853]: Sending gratuitous ARP on eth0 for 172.25.32.100 May 10 04:09:09 localhost Keepalived_vrrp[14853]: Sending gratuitous ARP on eth0 for 172.25.32.100 May 10 04:09:09 localhost Keepalived_vrrp[14853]: Sending gratuitous ARP on eth0 for 172.25.32.100 May 10 04:09:09 localhost Keepalived_vrrp[14853]: Sending gratuitous ARP on eth0 for 172.25.32.100 可以看到BACKIP身份自动转换到了MASTER,也可以通过看BACKUP节点有没有vip(172.25.32.100)地址来确定BACKER有没有转换身份到MASTER [root@keepalived ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 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 group default qlen 1000 link/ether 52:54:00:8b:cd:4f brd ff:ff:ff:ff:ff:ff inet 172.25.32.9/24 brd 172.25.32.255 scope global eth0 valid_lft forever preferred_lft forever inet 172.25.32.100/32 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe8b:cd4f/64 scope link valid_lft forever preferred_lft forever 这是测试当lvs的MASTER节点DOWN掉之后,会有BACKUP节点自动接管的双机热备高可用。
当没有设置keepalived的时候,lvs集群是感知不到后端服务器的健康状态的,策略也不会发生改变,会导致客户端访问到后端服务器挂掉之后的错误页面。而设置了keepalived后,当后端服务器挂掉后lvs集群策略会自动剔除掉挂掉的后端服务器,当后端服务器恢复正常后,集群又会自动把它加入进来。
当没有设置keepalived的时候,后端服务器挂掉 [root@lvs2 ~]# systemctl stop httpd.service 在客户端会访问到挂掉后端服务器的错误页面 [root@foundation32 ~]# for i in {1..10}; do curl 172.25.32.100;done lv3 curl: (7) Failed to connect to 172.25.32.100 port 80: Connection refused lv3 curl: (7) Failed to connect to 172.25.32.100 port 80: Connection refused lv3 curl: (7) Failed to connect to 172.25.32.100 port 80: Connection refused lv3 curl: (7) Failed to connect to 172.25.32.100 port 80: Connection refused lv3 curl: (7) Failed to connect to 172.25.32.100 port 80: Connection refused 而且lvs感知不到后端服务器的状态,策略也不会发生改变 [root@lvs1 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.25.32.100:80 rr -> 172.25.32.7:80 Route 1 0 0 -> 172.25.32.8:80 Route 1 0 0 当设置keepalived的时候,后端服务器挂掉 [root@lvs2 ~]# systemctl stop httpd.service 在客户端只会访问到正常的后端服务器页面,错误页面不会被访问到 [root@foundation32 ~]# for i in {1..10}; do curl 172.25.32.100;done lv3 lv3 lv3 lv3 lv3 lv3 lv3 lv3 lv3 lv3 在lvs中查看策略已经剔除了挂掉的后端服务器 [root@lvs1 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.25.32.100:80 rr -> 172.25.32.8:80 Route 1 0 0 You have new mail in /var/spool/mail/root 再把后端服务器恢复到可用状态 [root@lvs2 ~]# systemctl restart httpd.service 在lvs中查看策略,他感知到挂掉的后端服务器健康状态恢复了,所以又把恢复的后端服务器自动加入到了策略中 [root@lvs1 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.25.32.100:80 rr -> 172.25.32.7:80 Route 1 0 0 -> 172.25.32.8:80 Route 1 0 0 You have new mail in /var/spool/mail/root
如果觉得这样查看后端服务器健康状态不明显的话,可以下载mailx这个软件,这是用来阅读linux邮件的客户端
[root@lvs1 ~]# yum install -y mailx.x86_64 Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package mailx.x86_64 0:12.5-19.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================================================================================== Installing: mailx x86_64 12.5-19.el7 dvd 245 k Transaction Summary =================================================================================================================================================================================================================== Install 1 Package Total download size: 245 k Installed size: 466 k Downloading packages: mailx-12.5-19.el7.x86_64.rpm | 245 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mailx-12.5-19.el7.x86_64 1/1 Verifying : mailx-12.5-19.el7.x86_64 1/1 Installed: mailx.x86_64 0:12.5-19.el7 Complete!
因为之前在keepalived的配置文件中设置过可以通过keepalived用户来给本机发送邮件,所以通过mail可以直接查看后端服务器的健康状态 [root@lvs1 ~]# mail Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/root": 9 messages 4 new 8 unread A 1 keepalived@localhost Tue May 9 12:36 19/645 "[LVS_DEVEL] Realserver [172.25.32.7]:80 - DOWN" U 2 Mail Delivery System Tue May 9 12:39 78/2524 "Undelivered Mail Returned to Sender" U 3 keepalived@localhost Tue May 9 12:40 18/631 "[LVS_DEVEL] Realserver [172.25.32.7]:80 - UP" U 4 keepalived@localhost Tue May 9 12:44 18/632 "[LVS_DEVEL] Realserver [172.25.32.8]:80 - DOWN" U 5 keepalived@localhost Tue May 9 12:45 18/631 "[LVS_DEVEL] Realserver [172.25.32.8]:80 - UP" >N 6 keepalived@localhost Wed May 10 04:03 17/622 "[LVS_DEVEL] Realserver [172.25.32.7]:80 - DOWN" N 7 keepalived@localhost Wed May 10 04:03 17/621 "[LVS_DEVEL] Realserver [172.25.32.7]:80 - UP" N 8 keepalived@localhost Wed May 10 04:40 17/622 "[LVS_DEVEL] Realserver [172.25.32.7]:80 - DOWN" N 9 keepalived@localhost Wed May 10 04:44 17/621 "[LVS_DEVEL] Realserver [172.25.32.7]:80 - UP" &
通过这套lvs+keepalived的设置,保证了集群的负载均衡和双机热备高可用,当lvs的MASTER节点DOWN掉后有BACKUP节点来自动接管,当后端服务器挂掉之后lvs会进行健康检测,策略会自动剔除挂掉的后端服务器,客户端也不会访问到错误页面,就解决了文章开头的问题。当然如果MASTER和BACKUP节点都挂掉或者后端服务器都挂掉,那完蛋了,等死吧你!!!