keepalived 实现web应用的高可用
项目需求:公司需要我做privacyIDEA认证服务器的高可用,我的思路是在两台服务器上搭两套应用程序,后端存储公用一个mysql数据库,没有mysql数据库可以主备的形式,然后再做一下数据库的异地备份。基本思路就是这样的。https://s1.运维网.com/wyfs02/M01/99/7E/wKiom1lI4bTg4ysFAAIPFLB51fc601.png-wh_500x0-wm_3-wmp_4-s_4073473503.png
我主要介绍一下使用keepalived实现高可用。
本次实现使用两台机器master:10.236.44.62 ,slave:10.236.45.91
一、安装需要的软件(master和slave都操作)
yum install keepalived -y
yum install -y httpd
二、配置 master机器的 /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs { #全局定义段
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_fromAlexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_running {
script "/usr/local/bin/check_running"
interval 3
weight 10
}
vrrp_instance VI_1 { #实例名称
state MASTER #当前节点为主节点
interface eno16777736 #指定网卡
virtual_router_id 132 #虚拟路由ID,默认为51
priority 100 #优先级,主节点要数字要大于从节点
advert_int 1
authentication {
auth_type PASS
auth_pass 111111
}
virtual_ipaddress { #虚拟IP地址
10.236.44.77
}
track_script {
check_running weight 20
}
} 三、配置 slave机器的 /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs { #全局定义段
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_fromAlexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_running {
script "/usr/local/bin/check_running"
interval 1
weight 10
}
vrrp_instance VI_1 { #实例名称
state BACKUP #当前节点为主节点
interface eno16777736 #指定网卡
virtual_router_id 132 #虚拟路由ID,默认为51
priority 99 #优先级,主节点要数字要大于从节点
advert_int 1
authentication {
auth_type PASS
auth_pass 111111
}
virtual_ipaddress { #虚拟IP地址
10.236.44.77
}
track_script {
check_running weight 20
}
} 四、编辑脚本文件:/usr/local/bin/check_running (master和slave都一样)
#!/bin/bash
systemctl status httpd |grep running
if [ $? -ne 0 ]
then
systemctl restart httpd.service
sleep 1
systemctl status httpd |grep running
if [ $? -ne 0 ]
then
systemctl stop keepalived.service
fi
fi 给文件可执行权限 chmod +x /usr/local/bin/check_running
五、把httpd和keeplaived都启起来。(主和从上都操作)
systemctlrestart httpd.service
systemctlrestart keepalived.service
六、测试
编辑/var/www/html/index.html
master机器文件内容:master 机器slave机器文件内容:slave 机器
①:访问vip:10.236.44.77
https://s3.运维网.com/wyfs02/M00/99/7F/wKioL1lI5YqiLYmlAAAeVbD_FPw906.png-wh_500x0-wm_3-wmp_4-s_3376640082.png
②:把master的http停掉,然后再访问vip:10.236.44.77
systemctlstop httpd
https://s2.运维网.com/wyfs02/M01/99/7F/wKioL1lI5hWzMOYgAAAeVbD_FPw472.png-wh_500x0-wm_3-wmp_4-s_3694562541.png
这是因为我的脚本里会做判断,如果httpd停了,就把它启起来。
③:随便修改一下httpd的配置文件,让它不能正常启动,这个时候我们再把它停掉
https://s5.运维网.com/wyfs02/M02/99/7F/wKioL1lI5rDzu8l-AAAoB170Z6I163.png-wh_500x0-wm_3-wmp_4-s_1328203716.png
systemctlstop httpd.service
这个时候先看下主的ip show
https://s2.运维网.com/wyfs02/M02/99/7F/wKioL1lI51KyYLsmAAA7_aDLPG4635.png-wh_500x0-wm_3-wmp_4-s_389493454.png
再看下从的ip show
https://s1.运维网.com/wyfs02/M01/99/7F/wKioL1lI54DgDfPRAABNW1901zM296.png-wh_500x0-wm_3-wmp_4-s_183833358.png
这个时候访问一下vip:10.236.44.77
https://s4.运维网.com/wyfs02/M00/99/7F/wKioL1lI58TR8xx7AAAbLLrwEsY843.png-wh_500x0-wm_3-wmp_4-s_1049035921.png
页:
[1]