Keepalived 实现 web 服务高可用
前言
本环境是基于 Centos 7.8 系统构建 Keepalived 学习环境
具体构建,请参考 Keepalived 环境部署
环境准备
role | host | ip | keepalive-version | httpd-version |
---|---|---|---|---|
MASTER | node01 | 192.168.5.11 | keepalived-2.0.12 | httpd-2.4.6 |
BACKUP | node02 | 192.168.5.12 | keepalived-2.0.12 | httpd-2.4.6 |
Client | node03 | 192.168.5.13 | ---- | ---- |
部署 http服务
---node01
[root@node01 ~]# yum install httpd -y
[root@node01 ~]# echo "`hostname -I` httpd test page..." > /var/www/html/index.html
[root@node01 ~]# systemctl start httpd
---node02
[root@node02 ~]# yum install httpd -y
[root@node02 ~]# echo "`hostname -I` httpd test page..." > /var/www/html/index.html
[root@node02 ~]# systemctl start httpd
配置 MASTER
[root@node01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.5.10
smtp_connect_timeout 30
router_id LVS_DEVEL1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.20
}
}
[root@node01 ~]# systemctl restart keepalived.service
配置 BACKUP
[root@node02 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.5.10
smtp_connect_timeout 30
router_id LVS_DEVEL2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.20
}
}
[root@node02 ~]# systemctl restart keepalived.service
客户端通过 VIP 访问 web 服务
node01 发生故障后
[root@node01 ~]# systemctl stop keepalived.service
[root@node01 ~]# ps -ef | grep keepalived
root 32598 31955 0 10:19 pts/0 00:00:00 grep --color=auto keepalived
此时 node02 成为 MASTER VIP 漂移到了node02!!!
当node01 web 服务发生故障后
[root@node01 ~]# systemctl start keepalived.service
[root@node01 ~]# ps -ef | grep keepalived
root 32611 1 0 10:21 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 32612 32611 0 10:21 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 32614 31955 0 10:21 pts/0 00:00:00 grep --color=auto keepalived
[root@node01 ~]# systemctl stop httpd
[root@node01 ~]# ps -ef | grep httpd
root 32625 31955 0 10:21 pts/0 00:00:00 grep --color=auto httpd
此时 VIP 在node01 上,且node01 web 服务发生故障…
我们可以通过脚本的方式,keepalive 对web服务进行健康检查!以确保服务的高可用性
---node01
[root@node01 ~]# vim /etc/keepalived/check_web_server_keepalive.sh
#!/bin/bash
while true
do
http_pid=$(ps -C httpd --no-header | wc -l)
echo $http_pid
if [ ${http_pid} -eq 0 ]
then
systemctl start httpd
sleep 4
fi
http_pid=$(ps -C httpd --no-header | wc -l)
if [ ${http_pid} -eq 0 ]
then
systemctl stop keepalived
fi
sleep 3
done
[root@node01 ~]# chmod +x /etc/keepalived/check_web_server_keepalive.sh
[root@node01 ~]# cd /etc/keepalived/
[root@node01 keepalived]# nohup ./check_web_server_keepalive.sh &
---node02
[root@node02 ~]# vim /etc/keepalived/check_web_server_keepalive.sh
#!/bin/bash
while true
do
http_pid=$(ps -C httpd --no-header | wc -l)
echo $http_pid
if [ ${http_pid} -eq 0 ]
then
systemctl start httpd
sleep 4
fi
http_pid=$(ps -C httpd --no-header | wc -l)
if [ ${http_pid} -eq 0 ]
then
systemctl stop keepalived
fi
sleep 3
done
[root@node02 ~]# chmod +x /etc/keepalived/check_web_server_keepalive.sh
[root@node02 ~]# cd /etc/keepalived/
[root@node02 keepalived]# nohup ./check_web_server_keepalive.sh &
测试
node02 停止httpd
[root@node01 ~]# systemctl stop httpd
查看node01 web 服务状态
[root@node01 ~]# systemctl is-active httpd
active
node01 web 服务正常响应!!!
停止node01 keepalived 关闭node02 web 服务
[root@node01 ~]# systemctl stop keepalived
[root@node01 ~]# systemctl is-active keepalived
unknown
[root@node02 keepalived]# systemctl stop httpd
node01 web 服务正常响应!!!