Keepalived实现Nginx高可用配置文件示例一:
[root@node1 ~]# 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.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL_MASTER
}
vrrp_script check_nginx {
script "killall -0 nginx"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.188.100
}
}
该示例是通过" killall -0 nginx "命令实现对Nginx服务的高可用,如果Nginx服务启动,则该命令的退出码应为0,反之,退出码为1。而" killall "命令是" psmisc "包提供的。
Keepalived实现Nginx高可用配置文件示例二:
[root@node1 ~]# 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.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL_MASTER
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx1.sh" ##脚本的绝对路径
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.188.100
}
}
该示例是通过脚本的方式对Nginx实现高可用,通过判断脚本的退出码来实现。
脚本代码1:
[root@node1 ~]# vim /etc/keepalived/check_nginx1.sh
#!/bin/bash
nginx_stats=`ps -C nginx --no-header | wc -l`
if [ $nginx_stats -eq 0 ]
then
nginxStats=1
else
nginxStats=0
fi
exit $nginxStats
脚本代码2:
[root@node1 ~]# vim /etc/keepalived/check_nginx2.sh
#!/bin/bash
while true
do
nginx_state=`ps -C nginx --no-header | wc -l`
if [ $httpd_state -eq 0 ]
then
systemctl start nginx
sleep 2
nginx_state=`ps -C nginx --no-header | wc -l`
if [ $httpd_state -eq 0 ]
then
systemctl stop keepalived
fi
fi
done
以上都是Master节点的配置,而BACKUP节点只需要对角色,优先级,router_id进行修改即可。