keepalived快速入门

keepalived和nginx热备步骤操作

Master:

yum install -y keepalived

配置文件在这 /etc/keepalived

global_defs {
	#注释部分为SMTP邮件报警
    #notification_email {
    #    762357658@qq.com
    #}
    #notification_email_from itsection@example.com
    #smtp_server mail.example.com
    #smtp_connect_timeout 30
    router_id LVS_DEVEL
}

# vrrp_script 脚本模块
vrrp_script chk_nginx
{
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight 2
    fall 3
    rise 2
}


vrrp_instance VI_1 {
    state MASTER # 设置为主
    interface eth0 # 设置VIP绑定的网卡
    virtual_router_id 2
    priority 101 # 权重值 必须大于BACKUP
    advert_int 2 # 心跳检查间隔
    authentication {
    	# 认证设置
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    	# 设置VIP和子网掩码
        172.17.0.210/16
    }
    # 使用脚本
    track_script {
       chk_nginx
    }

}

nginx 心跳检测脚本,当检测当nginx挂掉后,keepalived就会自杀,
脚本记得 chmod +x 赋予执行权限

#!/bin/bash
A=`ps -ef | grep -w nginx | grep -v grep | wc -l`
echo $A 
if [ $A -eq 0 ];then
  if [ `ps -ef | grep -w nginx | grep -v grep | wc -l` -eq 0 ];then
      echo 'kiil!!!!'	
      ps -ef|grep keepalived|grep -v grep|awk '{print $2}'|xargs kill -9
  fi
fi

启动keepalived和nginx

 systemctl start keepalived.service 
 
 systemctl start nginx.service

用ip addr 查看是否绑定成功VIP

[root@3ffdfe156bbd /]: ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
25: eth0@if26: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.4/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet 172.17.0.210/16 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:4/64 scope link 
       valid_lft forever preferred_lft forever

BACKUP:

主要配置文件和Master的有一点不同,心跳配置文件一致

! Configuration File for keepalived
global_defs {
#notification_email {
#762357658@qq.com
#}
#notification_email_from itsection@example.com
#smtp_server mail.example.com
#smtp_connect_timeout 30
router_id LVS_DEVEL

}

vrrp_script chk_nginx
 {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -5
fall 3
rise 2
}


vrrp_instance VI_1 {
state BACKUP #状态需要为BACKUP
interface eth0
virtual_router_id 2
priority 30 #权重必须小于MASTER
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.0.210/16
}
track_script {
chk_nginx
}

}

成功启动BACKUP后会发现,检测到到了脚本执行

[root@e7bba4d70d71 keepalived]: systemctl status keepalived.service 
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-06-01 10:28:50 UTC; 8s ago
  Process: 10014 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 10015 (keepalived)
   CGroup: /system.slice/docker-e7bba4d70d719078a8e2ba26a9d89761018c69bf18dd7f6bfa4eeb62e44aba13.scope/system.slice/keepalived.service
           ├─10015 /usr/sbin/keepalived -D
           ├─10016 /usr/sbin/keepalived -D
           └─10017 /usr/sbin/keepalived -D

Jun 01 10:28:50 e7bba4d70d71 Keepalived_vrrp[10017]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 01 10:28:50 e7bba4d70d71 Keepalived_vrrp[10017]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
Jun 01 10:28:50 e7bba4d70d71 Keepalived_vrrp[10017]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
Jun 01 10:28:50 e7bba4d70d71 Keepalived_vrrp[10017]: VRRP_Instance(VI_1) removing protocol VIPs.
Jun 01 10:28:50 e7bba4d70d71 Keepalived_vrrp[10017]: Using LinkWatch kernel netlink reflector...
Jun 01 10:28:50 e7bba4d70d71 Keepalived_vrrp[10017]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jun 01 10:28:50 e7bba4d70d71 Keepalived_vrrp[10017]: VRRP sockpool: [ifindex(27), proto(112), unicast(0), fd(10,11)]
Jun 01 10:28:50 e7bba4d70d71 Keepalived_healthcheckers[10016]: Initializing ipvs
Jun 01 10:28:50 e7bba4d70d71 Keepalived_healthcheckers[10016]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 01 10:28:50 e7bba4d70d71 Keepalived_vrrp[10017]: VRRP_Script(chk_nginx) succeeded

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值