1、keepalived的VRRP功能,能防止单点故障,自动实现VIP的转移
2、环境说明
主机名 | IP地址 |
vmctl2 | eth0 192.168.2.100 VIP 192.168.2.101 |
vmctl3 | eth0 192.168.2.200 VIP 192.168.2.101 |
vmctl4 | eth0 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 keepalived
6、修改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
#启动keepalive