Nginx 高可用方案keepalived

两台虚拟机安装好nginx(openresty)

vm01 192.168.119.11 MASTER
vm02 192.168.119.12 BACKUP

两台机器安装keepalived

yum -y install keepalived

yum方式安装的会生产配置文件在/etc/keepalived

192.168.119.11 MASTER配置修改

#检测脚本
vrrp_script chk_http_port {
    script "/opt/openresty/nginx/check_nginx_pid.sh" #心跳执行的脚本,检测nginx是否启动
    interval 2                          #(检测脚本执行的间隔,单位是秒)
    weight 2                            #权重
}
#vrrp 实例定义部分
vrrp_instance VI_1 {
    state MASTER            # 指定keepalived的角色,MASTER为主,BACKUP为备
    interface ens33         # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡
    virtual_router_id 66    # 虚拟路由编号,主从要一直
    priority 100            # 优先级,数值越大,获取处理请求的优先级越高
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
    #授权访问
    authentication {
        auth_type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
        auth_pass 1111
    }
    track_script {
        chk_http_port            #(调用检测脚本)
    }
    virtual_ipaddress {
        192.168.119.210            # 定义虚拟ip(VIP),可多设,每行一个
    }
}

192.168.119.12 BACKUP 配置修改

#检测脚本
vrrp_script chk_http_port {
    script "/opt/openresty/nginx/check_nginx_pid.sh" #心跳执行的脚本,检测nginx是否启动
    interval 2                          #(检测脚本执行的间隔)
    weight 2                            #权重
}
#vrrp 实例定义部分
vrrp_instance VI_1 {
    state BACKUP                        # 指定keepalived的角色,MASTER为主,BACKUP为备
    interface ens33                      # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网
卡
    virtual_router_id 66                # 虚拟路由编号,主从要一直
    priority 99                         # 优先级,数值越大,获取处理请求的优先级越高
    advert_int 1                        # 检查间隔,默认为1s(vrrp组播周期秒数)
    #授权访问
    authentication {
        auth_type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
        auth_pass 1111
    }
    track_script {
        chk_http_port                   #(调用检测脚本)
    }
    virtual_ipaddress {
        192.168.119.210                  # 定义虚拟ip(VIP),可多设,每行一个
    }
}

检测脚本check_nginx_pid.sh

#!/bin/bash
source /etc/bashrc #编译安装的,需要加载环境变量
#检测nginx是否启动了
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then    #如果nginx没有启动就启动nginx                        
      nginx #重启nginx,不通安装方式,重启命令不同
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败,则停掉keepalived服务,进行VIP转移
              killall keepalived                    
      fi
fi

添加执行权限

chmod 755 check_nginx_pid.sh

测试效果

修改nginx默认网页,添加区分标识

192.168.119.11 MASTER配置修改

vi /opt/openresty/nginx/html/index.html

192.168.119.12 BACKUP 配置修改

启动nginx,启动keeealived,添加开启启动

sbin/nginx
systemctl start keepalived
systemctl enable keepalived

打开地址,当前nginx为vm01

停止vm01的nginx后,nginx进程依然存在,因为检测脚本检测到进程不存在会重启nginx。可以修改一下脚本,模拟nginx重启失败

[root@vm01 nginx]# nginx -s stop
[root@vm01 nginx]# ps -ef |grep nginx
root      45863      1  0 11:17 ?        00:00:00 nginx: master process nginx
nobody    45864  45863  0 11:17 ?        00:00:00 nginx: worker process
root      45869  30003  0 11:17 pts/0    00:00:00 grep --color=auto nginx
[root@vm01 nginx]# 
#!/bin/bash
source /etc/bashrc #编译安装的,需要加载环境变量
#检测nginx是否启动了
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then    #如果nginx没有启动就启动nginx                        
      #nginx 注释掉重启命令,模拟nginx重启失败
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败,则停掉keepalived服务,进行VIP转移
              killall keepalived                    
      fi
fi

再次查看nginx已经完全停止

[root@vm01 nginx]# nginx -s stop
[root@vm01 nginx]# ps -ef |grep nginx
root      46499  30003  0 11:19 pts/0    00:00:00 grep --color=auto nginx
[root@vm01 nginx]#

同时查看日志输出

vm01 停止keepalived,进行VIP转移

/var/log/messages

Apr 21 11:19:45 vm01 Keepalived[45179]: Stopping
Apr 21 11:19:45 vm01 Keepalived_vrrp[45181]: VRRP_Instance(VI_1) sent 0 priority
Apr 21 11:19:45 vm01 Keepalived_vrrp[45181]: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 21 11:19:45 vm01 Keepalived_healthcheckers[45180]: Stopped
Apr 21 11:19:45 vm01 Keepalived_vrrp[45181]: Stopped
Apr 21 11:19:45 vm01 Keepalived[45179]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Apr 21 11:20:01 vm01 systemd: Started Session 355 of user root.

vm02 切换到MASTER状态

Apr 21 11:19:46 localhost Keepalived_vrrp[27599]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 21 11:19:47 localhost Keepalived_vrrp[27599]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 21 11:19:47 localhost Keepalived_vrrp[27599]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 21 11:19:47 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210
Apr 21 11:19:47 localhost Keepalived_vrrp[27599]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.119.210
Apr 21 11:19:47 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210
Apr 21 11:19:47 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210
Apr 21 11:19:47 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210
Apr 21 11:19:47 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210
Apr 21 11:19:52 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210
Apr 21 11:19:52 localhost Keepalived_vrrp[27599]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.119.210
Apr 21 11:19:52 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210
Apr 21 11:19:52 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210
Apr 21 11:19:52 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210
Apr 21 11:19:52 localhost Keepalived_vrrp[27599]: Sending gratuitous ARP on ens33 for 192.168.119.210

刷新VIP网址,已经切换到vm02


Nginx 高可用方案keepalived
https://www.hechunyu.com/archives/nginx-gao-ke-yong-fang-an-keepalived
作者
chunyu
发布于
2022年07月30日
许可协议