server1 和 server2 是调度器,server3 和 server4 是服务器
server1 与server2 均要配置
【1】下载keepalived
yum install -y keepalived【2】server1 中修改配置文件
[root@server1 keepalived]# cat keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr #vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script check_haproxy { #script "killall -0 haproxy" script "/opt/check_haproxy.sh" #可以使用脚本,也可以直接killall interval 2 weight 0 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 65 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_haproxy } virtual_ipaddress { 172.25.0.100 } }【3】server2 中修改配置文件(server2是backup,且优先级低于server1)
[root@server2 keepalived]# cat keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr #vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script check_haproxy { #script "killall -0 haproxy" script "/opt/check_haproxy.sh" #可以使用脚本,也可以直接killall interval 2 weight 0 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 65 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_haproxy } virtual_ipaddress { 172.25.0.100 } }记得给可执行权限 两台虚拟机中都需要创建此脚本
[root@server1 opt]# vim /opt/check_haproxy.sh #!/bin/bash systemctl status haproxy &> /dev/null || systemctl restart haproxy &> /dev/null killall -0 haproxy if [ $? -ne 0 ];then systemctl stop keepalived fi [root@server1 opt]# chmod +x /opt/check_haproxy.sh #可执行权限可以先修改好一台虚拟机中的/etc/haproxy/haproxy.cfg文件,然后使用scp命令发送到另一台虚拟机上
# use_backend static if url_static acl read_request method GET acl read_request method HEAD acl write_request method PUT acl write_request method POST use_backend static if read_request use_backend app if write_request default_backend static # default_backend app #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend static balance roundrobin server static 172.25.15.3:80 check #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend app balance roundrobin # balance source # balance static-rr server app1 172.25.15.4:80 check server backup 127.0.0.1:80 backup【1】初始状态 两台虚拟机中的haproxy和keepalived都是打开状态。此时172.25.15.100是在server1上(因为server1的是master优先级高),server2没有172.25.15.100
【2】验证高可用
在server1上: systemctl stop haproxy此时172.25.15.100不再server1上了 关闭haproxy后172.25.15.100直接迁移到server2中了,实现了高可用
【3】验证优先级 重新打开server1中的haproxy之后,ip又会迁移回来。这是因为server1中的优先级高于server2