keepalive配置

安装

yum install -y keepalived

 

环境:

10.3.196.31 ap01  
10.3.196.32 ap02  
10.3.196.33 vip  

 

ap01配置:

复制代码
! 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 10.3.196.31
   smtp_connect_timeout 30
   router_id zjjg01
script_user root
enable_script_security

}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"    ## 检测nginx状态的脚本路径
    interval 2    ## 检测时间间隔
    weight -20    ## 如果条件成立,权重-20 
}


vrrp_instance VI_1 {
    state MASTER
    interface bond4
    virtual_router_id 33
    mcast_src_ip 10.3.196.31
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }

## 将track_script块加入instance 配置块
    track_script {
        chk_nginx    ## 执行Nginx监控的服务
}

    virtual_ipaddress {
        10.3.196.33
    }
}
复制代码

 

ap02配置:

  

复制代码
! 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 10.3.196.32
   smtp_connect_timeout 30
   router_id zjjg02
   script_user root
enable_script_security
} vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" ## 检测nginx状态的脚本路径 interval 2 ## 检测时间间隔 weight -20 ## 如果条件成立,权重-20 } vrrp_instance VI_1 { state SLAVE interface bond4 virtual_router_id 33 mcast_src_ip 10.3.196.32 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1234 } ## 将track_script块加入instance 配置块 track_script { chk_nginx ## 执行Nginx监控的服务 } virtual_ipaddress { 10.3.196.33 } }
复制代码

 

 

nginx_check.sh  

复制代码
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi
复制代码

 第二个写法

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
#authour:liuhailong
nodeip=10.3.216.2
nodeaddress="http://${nodeip}"
http_code=`curl -s -o /dev/null -w "%{http_code}" ${nodeaddress}`
 
if [ $http_code == 000 ]
then
/usr/bin/killall  keepalived
else
  exit 0
fi

  

启动

systemctl start keepalived
systemctl enable keepalived

主机

复制代码
global_defs {
   router_id nginx_master
   script_user root
   enable_script_security
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface em1
    virtual_router_id 199
    priority 102
    advert_int 1
    authentication {
       auth_type PASS
       auth_pass 111111
    }
  track_script {
    chk_nginx
    }
  virtual_ipaddress {
     10.3.226.199/32
     }
   }
复制代码

 

 

备机

复制代码
! Configuration File for keepalived
global_defs {
   router_id nginx_backup
   script_user root
   enable_script_security
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface em1
    virtual_router_id 199
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 111111
    }
  track_script {
   chk_nginx
     }
  virtual_ipaddress {
      10.3.226.199/32
    }
}
复制代码

 

posted @   大川哥  阅读(419)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示