Keepalived

keepalived实现服务高可用 全面

keepalived之vrrp_script详解

Keepalived状态切换的条件和因素

keepalived安装配置
systemctl start keepalived
keepalived正常运行后,会启动3个进程,其中一个是父进程,负责监控其子进程。一个是vrrp子进程,另外一个是checkers子进程。

[root@ ~]# ps -ef | grep keep
root      47386  47323  0 10:47 pts/0    00:00:00 grep --color=auto keep
root     197314      1  0  2018 ?        00:02:52 /usr/sbin/keepalived -D -d -S 0
root     197315 197314  0  2018 ?        00:50:52 /usr/sbin/keepalived -D -d -S 0
root     197316 197314  0  2018 ?        00:18:22 /usr/sbin/keepalived -D -d -S 0

安装
yum install keepalived -y
或者tar.gz安装

启动
service keepalived start

停止
service keepalived stop

重启
service keepalived restart

https://www.cnblogs.com/darendu/archive/2019/05/17/10881791.html

keepalived实现服务高可用

Keepalived原理与实战精讲–VRRP协议

keepalived双进程双VIP配置及原理图

/opt/context/keepalive_check/checkNginx.sh   注意脚本名字不要跟 nginx 进程名字重了 check_nginx也不行
auth_pass 5678   两进程密码要不一样
要配置抢占 不写nopreempt   配置不抢占写nopreempt的问题 https://www.cnblogs.com/arjenlee/p/9258188.html#auto_id_2

服务器A

! Configuration File for keepalived

#global_defs {
#   notification_email {
#     francs3@163.com
#   }
#   smtp_server 127.0.0.1
#   smtp_connect_timeout 30
#   router_id DB1_PG_HA_MASTER
#}

vrrp_script check_pg_alived {
    script "/usr/local/bin/pg_monitor.sh"
    #script "/home/postgres/bin/psql -c 'select 1;'"
    interval 2
    fall 3    # require 3 failures for KO
}

vrrp_instance VI_POSTGRES {
    state MASTER
    interface bond0.170     #网卡名对应修改
    virtual_router_id 126
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_pg_alived
    }
    virtual_ipaddress {
        11.115.127.170     #keepalive对外提供虚ip
    }
    smtp_alert
    notify_master /usr/local/bin/active_standby.sh
}

vrrp_instance VI_OPENFLOW {
    state BACKUP
    interface bond0.170
    virtual_router_id 151
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1133
    }
    virtual_ipaddress {
        11.115.127.169
    }

    track_script {
       # chk_openflow_process
    }
}

服务器B

! Configuration File for keepalived

#global_defs {
#   notification_email {
#     francs3@163.com
#   }
#   smtp_server 127.0.0.1
#   smtp_connect_timeout 30
#   router_id DB1_PG_HA_SLAVE
#}

# vrrp_script check_pg_alived {
#    script "/usr/local/bin/pg_monitor.sh"
#    #script "/home/postgres/bin/psql -c 'select 1;'"
#    interval 10
#    fall 3    # require 3 failures for KO
#}

vrrp_script chk_openflow_process {
    script "/etc/keepalived/check_of.sh"
    interval 2           
    weight -20 
}

vrrp_instance VI_POSTGRES {
    state BACKUP
    interface bond0.170     #网卡名对应修改
    virtual_router_id 126
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
       #check_pg_alived
    }
    virtual_ipaddress {
        11.115.127.170     #keepalive对外提供虚ip
    }
    #smtp_alert
    #notify_master /usr/local/bin/active_standby.sh
}

vrrp_instance VI_OPENFLOW {
    state MASTER
    interface bond0.170
    virtual_router_id 151
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1133
    }
    virtual_ipaddress {
        11.115.127.169
    }

    track_script {
        chk_openflow_process
    }
}
vrrp_script 里的script返回值为0时认为检测成功,其它值都会当成检测失败;

weight 为负时,脚本检测成功时此weight不影响priority,检测失败时priority – abs(weight)

	主失败:
		主 priority – abs(weight) < 从priority 时会切换主从
	主成功:
		主 priority > 从priority 主依然为主


检测nginx进程存在,count >0  exit 0 脚本检测成功时此weight不影响priority
检测nginx进程不存在,count =0  exit 1 脚本检测失败 priority-20
nginx起来之后,检测nginx进程存在,count >0  exit 0 脚本检测成功时此weight不影响priority,还是原来配置的100

vim /etc/keepalived/check_of.sh

#!/bin/bash

count=`ps aux | grep -v grep | grep nginx | wc -l` 
   
if [ $count -gt 0 ]; then    # -gt 大于
	exit 0
else
	exit 1
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值