Keepalived+ nginx的安装部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived+ nginx的安装部署相关的知识,希望对你有一定的参考价值。

主机:IP->10.252.3.160  nginx已安装OK(省略)
备机:IP->10.252.3.161  nginx已安装OK(省略)
VIP:10.252.3.162
 

第一步:主备一起安装keepalived   
yum install -y keepalived

第二步:配置/etc/keepalived/keepalived.conf
主机10.252.3.160配置如下:
!Configuration File for keepalived
global_defs{
  notification_email {
      [email protected] #发生邮件目标地址
      [email protected]
      [email protected]
  }
  notification_email_from [email protected]      #发生邮件源地址
  smtp_server 127.0.0.1              #smtp服务器
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
 
vrrp_scriptchk_http_port {
    script "/data/script/nginx.sh"                  #监控服务脚步
    interval 2                    #检测时间间隔(执行脚步间隔)
    weight 2
}
 
vrrp_instanceVI_1 {
    state MASTER  #主备的区别点,标识
    interface eth0          #对应的网卡
    virtual_router_id 60 
    priority 101    #优先级,谁做主(值越大,优先级越高)
    advert_int 2
    authentication {    #验证方式,以及密码必须主备一样
        auth_type PASS
        auth_pass 1234
    }
 
    track_interface {              #监控的网卡。如果只监控服务,这里可以不填
      eth0
    }
    track_script {                      #以脚本为监控chk_http_port是前面填写的
        chk_http_port
    }
    virtual_ipaddress {  #切换方式。切换虚拟IP
        10.252.3.165
    }
}
==============================================================
备机配置: 10.252.3.161
!Configuration File for keepalived
global_defs{
  notification_email {
      [email protected] #发生邮件目标地址
      [email protected]
      [email protected]
  }
  notification_email_from [email protected]      #发生邮件源地址
  smtp_server 127.0.0.1              #smtp服务器
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
 
vrrp_script chk_http_port {
    script "/data/script/nginx.sh"                  #监控服务脚步
    interval 2                    #检测时间间隔(执行脚步间隔)
    weight 2
}
 
vrrp_instanceVI_1 {
    state BACKUP  #主备的区别点,标识
    interface eth0          #对应的网卡
    virtual_router_id 60 
    priority 100    #优先级,谁做主(值越大,优先级越高)
    advert_int 2
    authentication {    #验证方式,以及密码必须主备一样
        auth_type PASS
        auth_pass 1234
    }
 
    track_interface {              #监控的网卡。如果只监控服务,这里可以不填
      eth0
    }
    track_script {                      #以脚本为监控chk_http_port是前面填写的
        chk_http_port
    }
    virtual_ipaddress {  #切换方式。切换虚拟IP
        10.252.3.165
    }
}

 

第三步:编写/data/script/nginx.sh脚本内容
#!/bin/sh
nginxPidNum=`ps -C nginx --no-header |wc -l`
keepalivedPidNum=`ps -C keepalived --no-header |wc -l`
if [ $nginxPidNum -eq 0 ];then
    killall keepalived
elif [ $keepalivedPidNum -eq 0 ];then
    service keepalived start
fi

给脚本权限 chmod +x /data/script/nginx.sh

 

第四步:启动主备keepalived
Service keepalived start
ip addr 查看vip是否起来了

备注:

1 如果没有成功检测是不是{ }少写了

2 sh 测试监控脚本是否正确

以上是关于Keepalived+ nginx的安装部署的主要内容,如果未能解决你的问题,请参考以下文章

keepalived+Nginx高可用集群部署(主从热备模式)

keepalived部署及实验

基于Haproxy+Keepalived构建高可用负载均衡集群

Tomcat+nginx+Keepalived部署反向代理负载均衡高可用

HAproxy+keepalived+nginx实验部署

nginx+keepalived 部署web高可用