yum install -y keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
# 主要是配置故障发生时的通知对象以及机器标识。
global_defs {
# 标识本节点的字条串,通常为 hostname,但不一定非得是 hostname。故障发生时,邮件通知会用到。
router_id LVS_k8s
}
# 用来做健康检查的,当时检查失败时会将 vrrp_instance 的 priority 减少相应的值。
vrrp_script check_haproxy {
script "killall -0 haproxy" # check process status
interval 3
weight -2
fall 10
rise 2
}
# rp_instance用来定义对外提供服务的 VIP 区域及其相关属性。
vrrp_instance VI_1 {
state MASTER #Current is MASTER,Other BACKUP
interface eth0 # Local Host Enternet
virtual_router_id 51
priority 250
advert_int 1
authentication {
auth_type PASS
auth_pass 35f18asjh3kksf7190d51c9f7f78f37300a0cbd
}
virtual_ipaddress {
192.168.1.100 # VIP
}
track_script {
check_haproxy
}
}
当前节点的配置中 state 配置为 MASTER,master-02节点设置为 BACKUP
# 开机启动
systemctl enable keepalived
# 启动keepalived
systemctl start keepalived
# 查看状态
systemctl status keepalived
ip a
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 de:28:f4:1d:ac:b4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.101/22 brd 192.168.3.255 scope global noprefixroute dynamic eth0
valid_lft 61647sec preferred_lft 61647sec
inet 192.168.1.100/22 scope global secondary eth0
valid_lft forever preferred_lft forever
inet6 fe80::dc28:f4ff:fe1d:acb4/64 scope link noprefixroute
valid_lft forever preferred_lft forever
当关掉当前节点的keeplived服务后将进行虚拟IP转移,将会推选state 为 BACKUP 的节点的某一节点为新的MASTER,可以在那台节点上查看网卡,将会查看到虚拟IP