安装keepalive的+nginx

环境:192.168.15.161(主)
192.168.15.155(从)
192.168.15.154
虚拟ip: 192.168.15.10
先安装好nginx
在192.168.15.161上配置nginx.conf,进入/usr/local/nginx/conf/nginx.conf设置权重,161做转发,给154和155
这里写图片描述
配置完启动每一台机器 nginx,/usr/local/nginx/sbin/nginx
修改155的/usr/local/nginx/html/index.html,内容增加155
这里写图片描述
同理,154服务器的也在内容增加154,方便区分开
测试ip访问情况
在浏览器输入192.168.15.161
这里写图片描述
刷新一下
这里写图片描述
Ip转发成功,并可以轮巡

Yum安装keepalived: yum install –y keepalived
安装完之后进入/etc/keepalived/keepalived.conf
配置如下显示
! 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_DEVEL
# vrrp_skip_check_adv_addr (这四句最好#掉,不然会无法访问VIP,亲测)
# vrrp_strict
# vrrp_garp_interval 0
# vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER (从的为BACKUP)
interface eno16777736 (对应网卡接口)
virtual_router_id 51
priority 100(优先级,从的应小于主的)
advert_int 1
authentication {
auth_type PASS
auth_pass 1111

 } 

 virtual_ipaddress {
    192.168.15.10  (设置虚拟VIP号)

}
}

设置完毕,把keepalived启动,systemctl start keepalived
同样,备机192.168.15.155设置keepalived,红色部分注意区分,设置完开启keepalived,systemctl start keepalived

先观察ip的绑定情况,
192.168.15.161
ip addr show
这里写图片描述

192.168.15.155
ip addr show
这里写图片描述
主机有虚拟ip,从机没有
访问虚拟ip 192.168.15.10,访问成功
这里写图片描述

现在把主机161的keepalived服务停掉,systemctl stop keeplived
发现虚拟ip从主机161转移到了备机155,并且访问虚拟ip时,页面为155的页面
如果把主机的Nginx停掉,nginx –s quit ,keepalived没停,虚拟ip并不会转移到备机,这时再访问虚拟ip的话会访问不了,所以要加一个检测nginx服务是否宕机的脚本
我自己在crontab –e 加上系统计划任务,写一个检测脚本check_nginx.sh放在/etc/keepalived下

!/bin/bash

A=Netstat –nplt|grep nginx|wc –l
if [ $A –eq 0 ];then
Systemctl stop keepalived
fi
给脚本增加执行权限 chmod +x check_nginx.sh
在crontab上 * * * * * bash /etc/keepalived/check_nginx.sh ; sleep 2
每2秒检测一次,如果检测到nginx不存在,则kill掉keepalived服务
当然,检测脚本可以在keepalived的配置文件做配置,把脚本路径添加到keepalived.conf中,自己尝试过,可是把nginx停掉的时候,keepalived并没有停止,所以就借用了系统计划任务来实现检测。欢迎各位来讨论下keepalived为什么检测不到nginx的服务是否存在

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值