环境准备
IP | VIP | 环境 | |
客户端 | 192.168.134.174 | ||
Master | 192.168.134.170 | 192.168.134.100 | 需要配置nginx负载均衡 |
Backup | 192.168.134.172 | 192.168.134.100 | 需要配置nginx负载均衡 |
web1服务器 | 192.168.134.171 | ||
web2服务器 | 192.168.134.173 |
1、首先安装nginx服务器(此处采用yum安装)
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum install nginx -y
2、修改nginx的配置文件(配置负载均衡)
cd /etc/nginx/conf.d/
vim web.conf
upstream webPools {
server 192.168.134.171;
server 192.168.134.173;
}
server {
location / {
proxy_pass http://webPools;
}
}
3、启动nginx
systemctl start nginx
4、修改keepalived配置文件
主节点:
! 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_DEVEL1
}
vrrp_instance VI_1 {
state MASTER
# nopreempt
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.134.100
}
}
备节点
修改:
state MASTER
priority 80
此时可以实现keepalived的故障切换和nginx负载均衡,但是如果nginx的主服务器出现故障,那么此时无法实现客户端的正常访问,即需要新的配置来实现高可用,因此,利用·vrrp-script(利用VIP漂移实现服务的可用)去监控集群资源。
5、重新修改keepalived配置文件,添加vrrp-script,在实例中还要调用。
主备节点做一样的修改
! 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_DEVEL1
}
vrrp_script check_nginx { # 自定义资源监控脚本
script "killall -0 nginx"
interval 2
}
vrrp_instance VI_1 {
state MASTER
# nopreempt
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.134.100
}
}
6、还可以用脚本检测服务的方法
cat check_#!/bin/bash
total=$(ps -C nginx --no-header | wc -l)
if [ $total -eq 0 ]
then
nginx_status=1
else
nginx_status=0
fi
exit $nginx_status
在keepalived文件中也进行修改
vrrp_script check_nginx {
#script "killall -0 nginx"
script "/etc/keepalived/check_nginx.sh"
interval 2
}
7、还有一种使用notify的·方法
写notify.sh脚本
#!/bin/bash
case "$1" in
master)
nmap localhost -p 80 | grep "80/tcp open"
if [ $? -ne 0 ];then
systemctl start nginx
fi
;;
backup)
nginx_psr=`ps -C nginx --no-header | wc -l`
if [ $nginx_psr -ne 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup"
;;
esac
chmod +x notify.sh # 给脚本增加执行权限
然后在keepalived文件中修改就行,修改下面几个地方
vrrp_script check_nginx {
#script "killall -0 nginx"
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -30
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.134.100
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
}
8、测试如下:
在停掉nginx后,仍旧可以正常访问
systemctl stop nginx
结果如下:
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1