一、环境准备
IP | 配置 | VIP |
---|---|---|
10.240.30.102 | keepalived + nginx | 10.240.30.103 |
10.240.30.100 | keepalived + nginx | 10.240.30.103 |
配置和安装这里我就不说了
二、修改配置文件
nginx 配置
nginx 配置统一的就是 nginx 的欢迎页面,无非在上面加了各自的 IP做为区别。
10.240.30.102 keepalived 配置
! Configuration File for keepalived
global_defs {
# 同一组的广播地址
vrrp_mcast_group4 224.0.0.18
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 虚拟IP必须是同一个网段下
10.240.30.103
}
}
10.240.30.100 keepalived 配置
! Configuration File for keepalived
global_defs {
# 同一组的广播地址
vrrp_mcast_group4 224.0.0.18
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 虚拟IP必须是同一个网段下
10.240.30.103
}
}
测试

关闭 10.230.30.102 的 keepalived,在访问

这里有个问题 10.230.30.100 不是即时顶上,我等了很久才可以访问。
网上有很多的检查 nginx 是否运行的脚,我这里只测试 keepalived 的高可用其余都不测试。
三、解决以上问题
主备之间,只能有一台可以持有VIP,我发现我这里两台都持有了VIP,可以用以下命令查看
ip a
keepalived 的日志打的不是很细,我发现其中有一句, Receive advertisement timeout,主 可以理解,但 从 有这句话一定是有问题的。
Sep 7 13:26:23 localhost Keepalived_vrrp[7634]: (VI_1) Receive advertisement timeout
Sep 7 13:26:23 localhost Keepalived_vrrp[7634]: (VI_1) Entering MASTER STATE
Sep 7 13:26:23 localhost Keepalived_vrrp[7634]: (VI_1) setting VIPs.
最终查到是广播的问题
# 指定keepalived配置的网卡:ens33,固定的VRRP广播地址:224.0.0.18
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens33 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --out-interface ens33 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
# 查看配置的规则
firewall-cmd --direct --get-rules ipv4 filter INPUT
firewall-cmd --direct --get-rules ipv4 filter OUTPUT
firewall-cmd --reload 会重新加载防火墙,导致你之前 iptables的方式失效,需要重新在执行一遍 iptables 命令。
默认 当主挂掉之后,vip会落到从上,当主启动之后,vip会被抢回来。
网友评论