我正在尝试使用虚拟 IP 创建负载均衡器。我参考了此链接使用虚拟 IP 的负载均衡器。配置错误后,我先启动了备份服务器的 keepalived 服务,然后启动了主服务器的 keepalived。问题是备份服务器的 keepalived 工作正常,但我无法启动主服务器的 keepalived。我尝试卸载 keepalived 并再次安装,但没有成功。这是配置文件
主服务器
vrrp_script chk_haproxy {
script "killall -0 haproxy" # verify the pid existance
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK
}
vrrp_instance VI_1 {
interface eth0 # interface to monitor
state MASTER
virtual_router_id 51 # Assign one ID for this route
priority 101 # 101 on master, 100 on backup
virtual_ipaddress {
192.168.0.54 # the virtual IP
}
track_script {
chk_haproxy
}
}
备份服务器
vrrp_script chk_haproxy {
script "killall -0 haproxy" # verify the pid existance
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK
}
vrrp_instance VI_1 {
interface eth0 # interface to monitor
state MASTER
virtual_router_id 51 # Assign one ID for this route
priority 100 # 101 on master, 100 on backup
virtual_ipaddress {
192.168.0.54 # the virtual IP
}
track_script {
chk_haproxy
}
}
启动 keepalived 服务时主服务器的输出
$ sudo service keepalived start
* Starting keepalived keepalived [fail]
启动 keepalived 服务时备份服务器的输出
$ sudo service keepalived start
* Starting keepalived keepalived [Ok]
有人建议我在这里做错了什么吗?
根据评论更新: 主服务器日志:
cat /var/log/syslog | grep VRRP_Instance
Here it does not prints anything which mean that the keepalived does not start on Master server
备份服务器日志:
cat /var/log/syslog | grep VRRP_Instance
Mar 27 02:39:22 ubuntu keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 27 02:39:23 ubuntu keepalived_vrrp: VRRP_Instance(VI_1) Entering to MASTER STATE
Mar 27 02:39:38 ubuntu keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 27 02:39:39 ubuntu keepalived_vrrp: VRRP_Instance(VI_1) Entering to MASTER STATE
Mar 27 02:52:16 ubuntu keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Mar 27 02:52:16 ubuntu keepalived_vrrp: VRRP_Instance(VI_1) Entering to BACKUP STATE
另外,在参考网站后,我将备份服务器的 keepalived.conf 文件中的状态更改为 BACKUP
答案1
您的 VRRP 实例配置错误。两个节点上都有state MASTER
,但只能有一个主节点。将备份服务器设置为,state BACKUP
它就可以正常工作。一般来说,规则非常简单:
两台服务器上相同:
- virtual_router_id - 对于每组服务器来说必须是唯一的
- 虚拟 IP
两台服务器有所不同:
- 实例名称(vrrp_instance THIS-IS-INSTANCE-NAME {)
- 状态(一个是 MASTER,其他是 BACKUP)
- 优先级(每个节点都有唯一优先级,主节点优先级最高)
答案2
state MASTER
只能设置其中一个节点。或者,两个节点都设置为state BACKUP
,系统将根据优先级自动选择MASTER。
答案3
如果您能重新发布当前配置,那将非常有用。不过,也许可以尝试一下。
主服务器
vrrp_script chk_haproxy {
script "pidof haproxy" # check the pid not kill it
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK
}
vrrp_instance VI_1 {
interface eth0 # interface to monitor
state MASTER
virtual_router_id 51 # Assign one ID for this route
priority 150 # 150 on master, 100 on backup
unicast_peer {
<backup ip address>
}
virtual_ipaddress {
192.168.0.54 # the virtual IP
}
track_script {
chk_haproxy
}
}
备份服务器
与主程序完全相同,但有变化:
state BACKUP
priority 100
unicast_peer {
<master ip address>
}
确保在主服务器和备份服务器之间允许 VRRP 协议(IP 协议 112)。在 /var/run/keepalived.INSTANCE.VI_1.state 检查每个服务器的状态,确保 haproxy 在 keepalived 之前启动。