Linux系统keepalived实现主备高可用方案
环境准备
装备两台机器,IP地址信息如下:
host1: 192.168.18.180 host2: 192.168.18.183 虚拟vip: 192.168.18.188
为了测试,分别在两台机器上安装nginx服务,使下面两个url均可访问
- http://192.168.18.180
- http://192.168.18.183
安装和配置keepalived
1.分别在两台机器上安装keepalived组件
#ubuntu sudo apt-get install keepalived #centos sudo yum install -y keepalived
2.创建并配置keepalived.conf文件
cd /etc/keepalived/ sudo touch keepalived.conf sudo vim keepalived.conf
3.主服务器配置
vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { interface enp0s3 # 根据你的网络接口名称调整 state MASTER #设置角色,这里为主设备 virtual_router_id 51 #设置虚拟路由器ID组,主备一致 priority 101 #优先级,数值越大,优先级越高 advert_int 1 #vrrp组之间的心跳包频率,1s authentication { #组认证信息 auth_type PASS #认证方式,密码 auth_pass 12345 #认证密码 } virtual_ipaddress { 192.168.18.188 #虚拟IP地址 } track_script { #健康检查脚本 chk_nginx } }
4.备服务器配置
vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { interface enp0s3 # 根据你的网络接口名称调整 state BACKUP #设置角色,这里为备设备 virtual_router_id 51 #设置虚拟路由器ID组,主备一致 priority 90 #优先级,数值越大,优先级越高 advert_int 1 #vrrp组之间的心跳包频率,1s authentication { #组认证信息 auth_type PASS #认证方式,密码 auth_pass 12345 #认证密码 } virtual_ipaddress { 192.168.18.188 #虚拟IP地址 } track_script { #健康检查脚本 chk_nginx } }
5.启动keepalived
#ubuntu sudo systemctl start keepalived #centos sudo service keepalived start sudo chkconfig keepalived on
6.创建并配置check_nginx.sh文件
主备都一样
sudo touch check_nginx.sh sudo vim check_nginx.sh #!/bin/bash if systemctl is-active --quiet nginx; then exit 0 else exit 1 fi
7.授予文件执行权限
sudo chmod +x /etc/keepalived/check_nginx.sh
8.检查状态
sudo systemctl status keepalived
验证
访问虚拟vip:http://192.168.18.188 其实访问的内容是:192.168.18.180的主机
- 停止主服务的keepalived组件,再访问虚拟vip:http://192.168.18.188 其实访问的内容变成:192.168.18.183的备机
- 重新启动主的keepalived组件,再访问虚拟vip:http://192.168.18.188 其实访问的内容又变回:192.168.18.180的主机
参考链接:
https://www.cnblogs.com/chuhe42/p/17731967.html
https://cloud.tencent.com/developer/article/1121797?areaSource=102001.9&traceId=DZSfVW7fabllQG92ut0g5
https://www.cnblogs.com/Mr-shen/p/17850038.html
https://blog.csdn.net/weixin_46471797/article/details/132279198
https://blog.csdn.net/qq_37591637/article/details/129810077
https://blog.51cto.com/u_16213624/10115281
还没有评论,来说两句吧...