nginx 加keepalived高可用
防火墙全关
开两台nginx
防火墙全关
开两台nginx
----------------修改主配置文件 -------------------------
在server上写入
upstream httpd1 {
server 192.168.200.113:80 weight=1;
server 192.168.200.114:80 weight=1;
}
在location里写入
proxy_pass http://httpd1;
proxy_set_header Host $host;
----------------------------------------------------------------
启动nginx
再分别安装 keepalived
yum -y install keepalived
yum -y install keepalived
另外两台 安装 httpd 并开启 systemctl start httpd
-------------------------------------------
分别配置keepalived 主(从)文件
vim /etc/keepalived/keepalived.conf
引入脚本文件
修改
! Configuration File for keepalived
vrrp_script check_nginx {
script "/shell/nginx_check.sh" // 加上脚本
interval 2
weight -20
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER(BACKUP)
interface eth0 (..)
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port //指定脚本
}
virtual_ipaddress {
192.168.200.254 //虚拟网IP (访问的网)
}
}
}
-----------------------------------------------------------------
分别创建对应脚本 vim /shell/nginx_check.sh
#!/bin/bash
count="$(ps -C nginx --no-header|wc -l)"
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER(BACKUP)
interface eth0 (..)
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port //指定脚本
}
virtual_ipaddress {
192.168.200.254 //虚拟网IP (访问的网)
}
}
}
-----------------------------------------------------------------
分别创建对应脚本 vim /shell/nginx_check.sh
#!/bin/bash
count="$(ps -C nginx --no-header|wc -l)"
if [ $count -eq 0 ];then
/usr/local/nginx/sbin/nginx -s restart
sleep 2
if [ ps -C nginx --no-header|wc -l -eq 0 ];then
systemctl stop keepalived
fi
fi
------------------------------------------------------------------------
加权限给脚本 chmod +x /shell/nginx_check.sh
启动Keepalived nginx
systemctl start keepalived
重启nginx
/usr/local/nginx/sbin/nginx -s restart
sleep 2
if [ ps -C nginx --no-header|wc -l -eq 0 ];then
systemctl stop keepalived
fi
fi
------------------------------------------------------------------------
加权限给脚本 chmod +x /shell/nginx_check.sh
启动Keepalived nginx
systemctl start keepalived
重启nginx