两个节点:10.0.2.18 和 10.0.2.19
1、安装:yum install -y keepalived
2、查看配置文件位置:rpm -qc keepalived
3、修改配置文件:
10.0.2.18上的配置文件: global_defs { router_id zcylb } vrrp_instance VI_1 { state MASTER interface enp0s3 unicast_peer { 10.0.2.19 } virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.2.250 dev enp0s3 } }
其中 10.0.2.250 是我随便设的一个没有被使用的ip,因为我的网卡叫enp0s3,所以配置文件里写的是enp0s3
10.0.2.19上的配置文件: global_defs { router_id zcylb } vrrp_instance VI_1 { state BACKUP interface enp0s3 unicast_peer { 10.0.2.18 } virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.2.250 dev enp0s3 } }
4、启动keepalived
service keepalived start
5、查看效果
6、停掉master的keepalived
service keepalived stop , 发现虚拟ip漂移了
注:在启停keepalived时,也可以查看日志
tail -f /var/log/messages
==============
==下面给keepalived加上执行脚本===
==============
10.0.2.18上的配置文件:
global_defs { router_id zcylb script_user root } vrrp_script check_docker { script "/home/check_docker_by_keep.sh" interval 5 user root } vrrp_instance VI_1 { state MASTER interface enp0s3 unicast_peer { 10.0.2.19 } virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_docker } virtual_ipaddress { 10.0.2.250 dev enp0s3 } }
========
10.0.2.19上的配置文件
global_defs {
router_id zcylb
script_user root
}
vrrp_script check_docker {
script "/home/check_docker_by_keep.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
interface enp0s3
unicast_peer {
10.0.2.18
}
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_docker
}
virtual_ipaddress {
10.0.2.250 dev enp0s3
}
}
[root@zcy ~]# cat /home/check_docker_by_keep.sh #!/bin/bash A=$(service docker status | grep ' active (running)') if [ "${A}" ] then echo "runing zcy" echo "runing zcy" >> /home/mylog else #service keepalived stop echo "not runing" >> /home/mylog service keepalived stop fi
当需要keepalived执行脚本时,需要脚本有可执行权限,且要关闭SELINUX sudo setenforce 0 sudo sed -i ‘s/^SELINUX=enforcing$/SELINUX=permissive/’ /etc/selinux/config
判断脚本可以被成功执行:
grep 'VRRP_Script(check_docker) succeeded' /var/log/messages
查看绑定效果
ip add show