实验前提
server1 和 server2 是调度器,server3 和 server4 是服务器
sever1:172.25.21.1sever2:172.25.21.2sever3:172.25.21.3sever4:172.25.21.4
调度器中进行配置 haproxy+keepalived
在server1和server2这两个调度器都要进行配置
1)keepalived
- yum install -y keepalived
![](https://i-blog.csdnimg.cn/blog_migrate/a0ca207bcb0206933127eea9ce1dee96.png)
- 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.21.100
}
}
- 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.21.100
}
}
2)编写脚本(记得给可执行权限)
两台虚拟机中都需要创建此脚本
[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 #可执行权限
3)haproxy
可以先修改好一台虚拟机中的/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.21.3:80 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
# balance source
# balance static-rr
server app1 172.25.21.4:80 check
server backup 127.0.0.1:80 backup
测试结果
1)初始状态
两台虚拟机中的haproxy和keepalived都是打开状态。此时172.25.21.100是在server1上(因为server1的是master优先级高)
![](https://i-blog.csdnimg.cn/blog_migrate/be26c413434c052e674548c161b0a4ef.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e23f6de79ff7bb0965eddce68ff45c52.png)
2)验证高可用
此时不再server1上了
![](https://i-blog.csdnimg.cn/blog_migrate/7f2d38ff0002e474a624f21f3a9fb0bc.png)
关闭后直接迁移到server2中了,
实现了高可用
![](https://i-blog.csdnimg.cn/blog_migrate/043c7c354b7e97ee7345e6c3cbf52117.png)
3)验证优先级
重新打开server1中的haproxy之后,ip又会迁移回来。这是因为server1中的优先级高于server2