1、keepalived的VRRP功能,能防止单点故障,自动实现VIP的转移
2、环境说明
主机名IP地址vmctl2eth0 192.168.2.100
VIP 192.168.2.101
vmctl3eth0 192.168.2.200
VIP 192.168.2.101
vmctl4eth0 192.168.2.150 (测试用)3、vmct2 的IP和web配置
#IP配置 [root@vmctl2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 NAME=eth0 DEVICE=eth0 TYPE=Ethernet IPADDR=192.168.2.100 NATMASK=255.255.255.0 ONBOOT=yes BOOTPROTO=static #web页面配置 [root@vmctl2 ~]# curl http://192.168.2.100:8080/keep/index.html 192.168.2.100 [root@vmctl2 ~]#4、vmctl3 的IP和web配置
#IP配置 [root@vmctl3 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 NAME=eth0 DEVICE=eth0 TYPE=Ethernet IPADDR=192.168.2.200 NATMASK=255.255.255.0 ONBOOT=yes BOOTPROTO=static #web页面配置 [root@vmctl3 ~]# curl http://192.168.2.200:8080/keep/index.html 192.168.2.200 [root@vmctl3 ~]#5、两台都需要安装keepalived,iso镜像文件的中就有keepalived软件包,只需配置好yum源
[root@vmctl2 ~]# yum -y install keepalived [root@vmctl3 ~]# yum -y install keepalived6、修改vmctl2的keepalived配置文件,用脚本方式定义健康检查
[root@vmctl2 ~]# vim /etc/keepalived/keepalived.conf #根据自己实际需要情况配置 ! Configuration File for keepalived global_defs { #notification_email { #acassen@firewall.loc #设置报警收件人邮箱 #} #notification_email_from Alexandre.Cassen@firewall.loc #设置发件人 #smtp_server 192.168.200.1 #设置邮件服务器 #smtp_connect_timeout 30 router_id vmctl2 #设置路由ID号(且不能相同) #vrrp_skip_check_adv_addr #vrrp_strict #vrrp_garp_interval 0 #vrrp_gna_interval 0 } #添加监控检查,自定义脚本方式 vrrp_script check_web { script "/etc/keepalived/check_web_status.sh" interval 5 } vrrp_instance VI_1 { state MASTER #主是MASTER,辅是BACKUP interface eth0 #网络接口 virtual_router_id 51 #主辅VRID号必须一致 priority 100 #优先级,数值大的优先级高 advert_int 1 authentication { auth_type PASS auth_pass 1111 #主辅密码必须一致 } virtual_ipaddress { 192.168.2.101 #VIP地址 } track_script { #自定义健康检查脚本 check_web #配置上面的vrrp脚本调用名 } } #编写check_web_status.sh 脚本 [root@vmctl2 keepalived]# cat /etc/keepalived/check_web_status.sh #!/bin/bash curl -I http://192.168.2.100:8080/keep/index.html &> /dev/null if [ $? -ne 0 ];then systemctl stop keepalived fi #启动keepalived [root@vmctl2 ~]# systemctl start keepalived #查看状态 [root@vmctl2 keepalived]# systemctl status keepalived ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2020-07-02 12:02:01 PDT; 8s ago Process: 5290 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 5292 (keepalived) CGroup: /system.slice/keepalived.service ├─5292 /usr/sbin/keepalived -D ├─5293 /usr/sbin/keepalived -D └─5294 /usr/sbin/keepalived -D Jul 02 12:02:03 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:03 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:03 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:03 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 [root@vmctl2 keepalived]#7、修改vmctl3的keepalived配置文件
[root@vmctl3 ~]# vim /etc/keepalived/keepalived.conf global_defs { router_id vmctl3 } vrrp_script check_web { script "/etc/keepalived/check_web_status.sh" inerval 5 } vrrp_instance VI_1 { state BACKUP nopreempt #不抢占VIP,意思就是主故障恢复后还是主 interface eth0 virtual_router_id 51 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.101 } track_script { check_web } } #编辑check_web_status.sh 脚本 [root@vmctl3 ~]# cd /etc/keepalived/ [root@vmctl3 keepalived]# vim check_web_status.sh #!/bin/bash curl -I http://192.168.2.200:8080/keep/index.html &> /dev/null if [ $? -ne 0 ];then systemctl stop keepalived fi [root@vmctl3 keepalived]# ll total 8 -rwxr-xr-x 1 root root 126 Jul 2 10:34 check_web_status.sh -rw-r--r-- 1 root root 436 Jul 2 11:44 keepalived.conf #启动keepalived [root@vmctl2 ~]# systemctl start keepalived #查看状态 [root@vmctl3 keepalived]# systemctl status keepalived ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2020-07-02 11:47:27 CST; 18min ago Process: 2018 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 2019 (keepalived) CGroup: /system.slice/keepalived.service ├─2019 /usr/sbin/keepalived -D ├─2020 /usr/sbin/keepalived -D └─2021 /usr/sbin/keepalived -D Jul 02 11:58:01 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 11:58:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 11:58:06 vmctl3 Keepalived_vrrp[2021]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.2.101 Jul 02 11:58:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 11:58:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 11:58:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 11:58:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:01:59 vmctl3 Keepalived_vrrp[2021]: VRRP_Instance(VI_1) Received advert with higher priority 100, ours 80 Jul 02 12:01:59 vmctl3 Keepalived_vrrp[2021]: VRRP_Instance(VI_1) Entering BACKUP STATE Jul 02 12:01:59 vmctl3 Keepalived_vrrp[2021]: VRRP_Instance(VI_1) removing protocol VIPs. [root@vmctl3 keepalived]#8、客户端(vmctl4)测试 及验证
#客户端访问VIP,会调用主的web页面,也就是vmctl2的 [root@vmctl4 ~]# curl http://192.168.2.101:8080/keep/index.html 192.168.2.100 [root@vmctl4 ~]# curl http://192.168.2.101:8080/keep/index.html 192.168.2.100 [root@vmctl4 ~]# #停掉主 vmctl2的web应用 [root@vmctl2 keepalived]# /mnt/apache-tomcat-8.0.30/bin/shutdown.sh Using CATALINA_BASE: /mnt/apache-tomcat-8.0.30 Using CATALINA_HOME: /mnt/apache-tomcat-8.0.30 Using CATALINA_TMPDIR: /mnt/apache-tomcat-8.0.30/temp Using JRE_HOME: /usr Using CLASSPATH: /mnt/apache-tomcat-8.0.30/bin/bootstrap.jar:/mnt/apache-tomcat-8.0.30/bin/tomcat-juli.jar [root@vmctl2 keepalived]# #查看状态 [root@vmctl2 keepalived]# systemctl status keepalived ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: inactive (dead) Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:02:08 vmctl2 Keepalived_vrrp[5294]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:02 vmctl2 Keepalived[5292]: Stopping Jul 02 12:09:02 vmctl2 systemd[1]: Stopping LVS and VRRP High Availability Monitor... Jul 02 12:09:02 vmctl2 Keepalived_vrrp[5294]: VRRP_Instance(VI_1) sent 0 priority Jul 02 12:09:02 vmctl2 Keepalived_vrrp[5294]: VRRP_Instance(VI_1) removing protocol VIPs. Jul 02 12:09:03 vmctl2 systemd[1]: Stopped LVS and VRRP High Availability Monitor. [root@vmctl2 keepalived]# #去辅vmctl3上查看状态(状态中不会显示优先级的值了) [root@vmctl3 keepalived]# systemctl status keepalived ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2020-07-02 11:47:27 CST; 22min ago Process: 2018 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 2019 (keepalived) CGroup: /system.slice/keepalived.service ├─2019 /usr/sbin/keepalived -D ├─2020 /usr/sbin/keepalived -D └─2021 /usr/sbin/keepalived -D Jul 02 12:09:01 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:01 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:01 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:01 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 [root@vmctl3 keepalived]# #客户端访问 [root@vmctl4 ~]# curl http://192.168.2.101:8080/keep/index.html 192.168.2.200 [root@vmctl4 ~]# curl http://192.168.2.101:8080/keep/index.html 192.168.2.200 [root@vmctl4 ~]# #恢复vmctl2的web应用和启动keepalived [root@vmctl2 keepalived]# systemctl start keepalived [root@vmctl2 keepalived]# systemctl status keepalived ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2020-07-02 12:11:50 PDT; 7s ago Process: 5739 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 5741 (keepalived) CGroup: /system.slice/keepalived.service ├─5741 /usr/sbin/keepalived -D ├─5742 /usr/sbin/keepalived -D └─5743 /usr/sbin/keepalived -D Jul 02 12:11:51 vmctl2 Keepalived_vrrp[5743]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:11:51 vmctl2 Keepalived_vrrp[5743]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:11:51 vmctl2 Keepalived_vrrp[5743]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:11:51 vmctl2 Keepalived_vrrp[5743]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:11:56 vmctl2 Keepalived_vrrp[5743]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:11:56 vmctl2 Keepalived_vrrp[5743]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.2.101 Jul 02 12:11:56 vmctl2 Keepalived_vrrp[5743]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:11:56 vmctl2 Keepalived_vrrp[5743]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:11:56 vmctl2 Keepalived_vrrp[5743]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:11:56 vmctl2 Keepalived_vrrp[5743]: Sending gratuitous ARP on eth0 for 192.168.2.101 [root@vmctl2 keepalived]# #去vmctl3查看状态(显示优先级数值,是辅)(keepavlied配置文件配置了不抢占VIP) [root@vmctl3 keepalived]# systemctl status keepalived ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2020-07-02 11:47:27 CST; 24min ago Process: 2018 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 2019 (keepalived) CGroup: /system.slice/keepalived.service ├─2019 /usr/sbin/keepalived -D ├─2020 /usr/sbin/keepalived -D └─2021 /usr/sbin/keepalived -D Jul 02 12:09:01 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:09:06 vmctl3 Keepalived_vrrp[2021]: Sending gratuitous ARP on eth0 for 192.168.2.101 Jul 02 12:11:48 vmctl3 Keepalived_vrrp[2021]: VRRP_Instance(VI_1) Received advert with higher priority 100, ours 80 Jul 02 12:11:48 vmctl3 Keepalived_vrrp[2021]: VRRP_Instance(VI_1) Entering BACKUP STATE Jul 02 12:11:48 vmctl3 Keepalived_vrrp[2021]: VRRP_Instance(VI_1) removing protocol VIPs. [root@vmctl3 keepalived]# #客户端访问 [root@vmctl4 ~]# curl http://192.168.2.101:8080/keep/index.html 192.168.2.100 [root@vmctl4 ~]# curl http://192.168.2.101:8080/keep/index.html 192.168.2.100 [root@vmctl4 ~]#