keepalived主从架构
一 环境规划
节点 | IP | 功能 | VIP |
node-01 | 192.168.174.120 | keepalived master | 192.168.174.20 |
node-02 | 192.168.174.121 | keepalived slave | 192.168.174.20 |
二 抢占模式
抢占模式,即当高优先级的主机恢复在线后,会抢占低先级的主机的master角色,造成网络抖动,建议设置为非抢占模式 nopreempt ,即高优级主机恢复后,并不会抢占低优先级主机的master角色
2.1 修改keepalived配置
2.1.1 node-01节点配置
点击查看代码
root@node-01:/etc/keepalived# cat keepalived.conf ! Configuration File for keepalived
global_defs {
notification_email {
1304005320@qq.com
}
notification_email_from 1304005320@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id node-01
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 101
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.174.20
}
}
2.1.2 node-02节点配置
root@node-02:~# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived
global_defs {
notification_email {
1304995320@qq.com
}
notification_email_from 1304995320@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id node-02
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 101
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.174.20
}
}
2.2 验证VIP
2.2.1 查看node-01节点
root@node-01:~# hostname -I 192.168.174.120 192.168.174.20 root@node-01:~# ping -c 2 192.168.174.20 PING 192.168.174.20 (192.168.174.20) 56(84) bytes of data. 64 bytes from 192.168.174.20: icmp_seq=1 ttl=64 time=0.013 ms 64 bytes from 192.168.174.20: icmp_seq=2 ttl=64 time=0.026 ms
--- 192.168.174.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1016ms
rtt min/avg/max/mdev = 0.013/0.019/0.026/0.006 ms
2.2.2 查看node-02节点
root@node-02:~# hostname -I 192.168.174.121 root@node-02:~# ping -c 2 192.168.174.20 PING 192.168.174.20 (192.168.174.20) 56(84) bytes of data. 64 bytes from 192.168.174.20: icmp_seq=1 ttl=64 time=0.598 ms 64 bytes from 192.168.174.20: icmp_seq=2 ttl=64 time=0.442 ms
--- 192.168.174.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1030ms
rtt min/avg/max/mdev = 0.442/0.520/0.598/0.078 ms
2.3 模拟node-01故障
2.3.1 停止node-01节点keepalived
root@node-01:~# systemctl stop keepalived root@node-01:~# hostname -I 192.168.174.120 root@node-01:~# ping -c 2 192.168.174.20 PING 192.168.174.20 (192.168.174.20) 56(84) bytes of data. 64 bytes from 192.168.174.20: icmp_seq=1 ttl=64 time=0.676 ms 64 bytes from 192.168.174.20: icmp_seq=2 ttl=64 time=0.353 ms
--- 192.168.174.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1008ms
rtt min/avg/max/mdev = 0.353/0.514/0.676/0.161 ms
2.3.2 查看node-02节点
root@node-02:~# hostname -I 192.168.174.121 192.168.174.20 root@node-02:~# ping -c 2 192.168.174.20 PING 192.168.174.20 (192.168.174.20) 56(84) bytes of data. 64 bytes from 192.168.174.20: icmp_seq=1 ttl=64 time=0.013 ms 64 bytes from 192.168.174.20: icmp_seq=2 ttl=64 time=0.023 ms
--- 192.168.174.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1031ms
rtt min/avg/max/mdev = 0.013/0.018/0.023/0.005 ms
2.4 node-01节点故障恢复
2.4.1 查看node-01节点
root@node-01:~# systemctl start keepalived root@node-01:~# hostname -I 192.168.174.120 192.168.174.20 root@node-01:~# ping -c 2 192.168.174.20 PING 192.168.174.20 (192.168.174.20) 56(84) bytes of data. 64 bytes from 192.168.174.20: icmp_seq=1 ttl=64 time=0.014 ms 64 bytes from 192.168.174.20: icmp_seq=2 ttl=64 time=0.025 ms
--- 192.168.174.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1017ms
rtt min/avg/max/mdev = 0.014/0.019/0.025/0.005 ms
2.4.2 查看node-02节点
root@node-02:~# hostname -I 192.168.174.121 root@node-02:~# ping -c 2 192.168.174.20 PING 192.168.174.20 (192.168.174.20) 56(84) bytes of data. 64 bytes from 192.168.174.20: icmp_seq=1 ttl=64 time=0.284 ms 64 bytes from 192.168.174.20: icmp_seq=2 ttl=64 time=0.271 ms
--- 192.168.174.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1013ms
rtt min/avg/max/mdev = 0.271/0.277/0.284/0.006 ms
三 非抢占模式
注意:要关闭 VIP抢占,必须将各 keepalived 服务器state配置为BACKUP
3.1 设置keepalived.conf文件
3.1.1 node-01节点
点击查看代码
root@node-01:~# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived
global_defs {
notification_email {
1304005320@qq.com
}
notification_email_from 1304005320@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id node-01
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 101
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.174.20
}
}
3.1.2 node-02节点
点击查看代码
root@node-02:~# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived
global_defs {
notification_email {
1304995320@qq.com
}
notification_email_from 1304995320@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id node-02
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 101
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.174.20
}
}