HAPROXY+KEEPALIVE构建高可用集群
source link: https://www.hi-roy.com/posts/haproxy-keepalive%E6%9E%84%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
HAPROXY+KEEPALIVE构建高可用集群
新增一台HA节点192.168.0.155,虚拟IP192.168.0.160,其它IP见上文。
安装KEEPALIVE:yum install keepalived
分别在151和155 安装完成后,修改/etc/keepalived/keepalived.conf
,我使用151作为主节点,155为备节点。
151配置如下:
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
###########
vrrp_script chk_ha {
script "/opt/chk_ha.sh"
interval 2
weight 2
}
###########
vrrp_instance VI_1 {
state MASTER # 主节点
interface eth0 #根据实际情况修改
virtual_router_id 51
mcast_src_ip 192.168.0.151 # 本机ip
priority 100 # 这里要大于备节点
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_ha
}
virtual_ipaddress {
192.168.0.160 # 注意这里改成虚拟IP
}
}
155配置如下:
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
###########
vrrp_script chk_ha {
script "/opt/chk_ha.sh"
interval 2
weight 2
}
###########
vrrp_instance VI_1 {
state BACKUP # 备份节点
interface eth0 #根据实际情况修改
virtual_router_id 51
mcast_src_ip 192.168.0.155 # 本机ip
priority 95 # 这里要小于主节点
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_ha
}
virtual_ipaddress {
192.168.0.160 # 注意这里改成虚拟IP
}
}
其中,chk_ha是用于检测HAPROXY是否存活的脚本,内容如下:
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
/etc/init.d/haproxy start
fi
sleep 2
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
/etc/init.d/keepalived stop
fi
添加执行权限后,给151和155 分别添加虚拟IP:ifconfig eth0:0 192.168.0.160 netmask 255.255.255.0 up
添加后结果如下
[root@test5 ~]# ifconfig eth0:0
eth0:0 Link encap:Ethernet HWaddr AE:A9:9C:02:C3:28
inet addr:192.168.0.160 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:17
155上的HA配置完全和151一样,但这里注意把listen status改成虚拟IP的192.168.0.160:8080。
启动KEEPALIVED服务:
[root@test5 ~]# service keepalived start
Starting keepalived: [ OK ]
由于有chk_ha这个脚本存在所以ha服务会自动启动。
此时,即便151的HA节点故障,155将自动接替151的工作。
参考资料:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK