Nginx负载均衡+keepalived实现nginx的高可用

1、Nginx下载安装

https://www.cnblogs.com/shamo89/p/7645792.html

2、keepalived下载安装

1)yum install -y keepalived

2)  https://www.cnblogs.com/dcrq/p/5642680.html(未测试)

3、采用2.1方式安装及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 nginx_master
   #vrrp_skip_check_adv_addr
   #vrrp_strict
   #vrrp_garp_interval 0
   #vrrp_gna_interval 0
}

vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #检测脚本执行的间隔
weight -5  #检测失败(脚本返回0)则优先级减5

fall 2   #检测连续 2 次失败才算确定是真失败

rise 1 #检测一次成功就算成功  

}

vrrp_instance VI_1 {
    state MASTER      #instance的初始状态,最终还需通过优先级高低竞选出master-backup
    interface ens192     #ip addr查看具体网卡信息
    virtual_router_id 60  #VRID,相同的VRID为一组,决定多播的MAC地址
    priority 100   #优先级
    advert_int 1   #检查间隔,s
    authentication {
        auth_type PASS
        auth_pass 1111
    }

 track_script {
 chk_http_port #调用检测脚本
 }

    virtual_ipaddress {
        172.253.62.200  #虚拟ip
    }
}

 nginx服务检测脚本  check_nginx_pid.sh

#!/bin/sh
counter=$(ps -C nginx --no-heading|wc -l)
echo "$counter"
if [ "${counter}" = "0" ]; then
      /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
      sleep 2
      counter=$(ps -C nginx --no-heading|wc -l)
      if [ "${counter}" = "0" ]; then
          pkill -9 keepalived
      fi
fi

4、启动、重启

systemctl start keepalived 启动

systemctl restart keepalived 重启

systemctl status keepalived 查看状态

posted @ 2021-08-24 23:21  李大风  阅读(79)  评论(0)    收藏  举报