nginx+keepalived高可用

参与虚拟机111,12,113,114,111、112安装nginx与keepalived,113,114安装apache

111、112:

1、安装keepalived

[root@node1 ~]# yum -y install keepalived

2、修改keepalived配置文件

[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_MASTER
}
vrrp_script check_nginx {
script "/opt/chk_nginx.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eno16777728
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.254
}
track_script {
check_nginx #引用脚本
}
}

3、修改nginx配置文件:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

keepalive_timeout 65;
upstream httpd_server {
server 192.168.200.113 weight=1;
server 192.168.200.114 weight=1;
}
server {
listen 80;
server_name localhost;
charset utf-8;

location / {
root html;
index index.html index.htm;
proxy_pass http://httpd_server;
proxy_set_header Host $http_host;
}

113、114安装apache:[root@localhost ~]# yum -y install httpd

[root@localhost ~]# systemctl start httpd

4、装备测试文件

113:[root@localhost ~]# echo "11111" > /var/www/html/index.html

114:[root@localhost ~]# echo "22222" > /var/www/html/index.html

去网页进行检测

 5、nginx_check.sh shell文件,配置为周期性任务

[root@localhost ~]# mkdir /shell
[root@localhost ~]# vim /shell/nginx_check.sh


#!/bin/bash
count="$(ps -C nginx --no-header|wc -l)"

if [ $count -eq 0 ];then
nginx
sleep 2
if [ `ps -c nginx --no-header` |wc -l -eq 0 ];then
systemctl stop keepalived
fi
fi

给脚本添加执行权限:

[root@localhost ~]# chmod +x /shell/nginx_check.sh

关掉nginx和keepalived

[root@localhost ~]# killall -9 nginx
[root@localhost ~]# systemctl stop keepalived

[root@localhost ~]# while :
> do
> curl 192.168.200.254
> sleep 1
> done

进行测试。

原文地址:https://www.cnblogs.com/990624lty-jhc/p/11634782.html