keepalived安装及使用

vrrp

vrrp: 虚拟路由冗余协议

vrrp相关术语

· 虚拟路由器:Virtual Router
· 虚拟路由器标识:VRID(0-255),唯一标识虚拟路由器
·VIP:Virtual IP
· VMAC:Virutal MAC (00-00-5e-00-01-VRID)
· 物理路由器:
master:主设备
backup:备用设备
priority:优先级

vrrp相关技术

通告:心跳,优先级等;周期性
工作方式:抢占式,非抢占式
安全认证:
无认证
简单字符认证:预共享密钥
MD5
工作模式:
主/备:单虚拟路由器
主/主:主/备(虚拟路由器1),备/主(虚拟路由器2)

keepalived

安装

1, 关闭SELinux

[root@localhost ~]#vim /etc/sysconfig/selinux
修改SELINUX 值
SELINUX=disabled	

2,关闭防火墙

systemctl disable --now firewalld.service

keepalived 安装包在linux 自带的软件包中已经存在可使用yum 直接安装

[root@localhost ~]#yum -y install keepalived

主配置文件位置

	/etc/keepalived/keepalived.conf

主配置文件详解

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc # #keepalived 发生故障切换时邮件发送的目标邮箱,可以按行区分写多个
     failover@firewall.loc 
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc  #发邮件的地址
   smtp_server 192.168.200.1  #邮件服务器地址
   smtp_connect_timeout 30  #邮件服务器连接timeout
   router_id LVS_DEVEL  #每个keepalived主机唯一标识,建议使用当前主机名,但多节点重名不影响 
   vrrp_skip_check_adv_addr  #对所有通告报文都检查,会比较消耗性能,启用此配置后,如果收到的通告报文和上一个报文是同一个路由器,则跳过检查,默认值为全检查
   vrrp_strict  #严格遵守VRRP协议,启用此项后以下状况将无法启动服务:1.无VIP地址 2.配置了单播邻居 3.在VRRP版本2中有IPv6地址,开启动此项并没有配置vrrp_iptables时会自动开启iptables防火墙规则,默认导致VIP无法访问,建议不加此项配置
   vrrp_garp_interval 0 # 报文发送延迟,0表示不延迟
   vrrp_gna_interval 0 # 消息发送延迟
    vrrp_iptables #  #此项和vrrp_strict同时开启时,则不会添加防火墙规则,如果无配置vrrp_strict项,则无需启用此项配置
}


vrrp_instance VI_1 {  # <String>为vrrp的实例名,一般为业务名称
    state MASTER  # 当前节点在此虚拟路由器上的初始状态,状态为MASTER或者BACKUP
    interface ens33  # 绑定为当前虚拟路由器使用的物理接口
    virtual_router_id 51  # 每个虚拟路由器惟一标识,范围:0-255,
    priority 100 # 当前物理节点在此虚拟路由器的优先级,范围:1-254,
    advert_int 1 # vrrp通告的时间间隔,默认1s
    authentication {  # 认证机制 
        auth_type PASS  # AH为IPSEC认证(不推荐),PASS为简单密码(建议使用)
        auth_pass 1111  #预共享密钥,仅前8位有效,同一个虚拟路由器的多个keepalived节点必须一样
    }
    virtual_ipaddress {  #虚拟IP,生产环境可能指定上百个IP地址
        192.168.144.100 #指定VIP,不指定网卡,默认为eth0,注意:不指定/prefix,默认为/32
         192.168.144.102/24 dev ens33 label ens33:1 #指定VIP的网卡label
    }
}

启动keepalive

[root@localhost ~]#systemctl start keepalived.service 

启动keepalive 日志功能

1,配置日志输出至系统syslog 服务
 [root@localhost ~]#vim /etc/sysconfig/keepalived
 添加下面内容:
 KEEPALIVED_OPTIONS="-D -S 6"

在这里插入图片描述

 2,修改syslog 配置文件,接收日志并输出到指定文件
 [root@localhost ~]#vim /etc/rsyslog.conf
 添加如下内容:
 local6.*                                                /var/log/keepalived.log

在这里插入图片描述

重启 keepalive 及日志服务
 [root@localhost ~]#systemctl restart keepalived.service rsyslog.service
 查看日志:
 [root@localhost ~]#tail -f /var/log/keepalived.log

在这里插入图片描述

keepalive 架构实现

实现master/slave的 Keepalived 单主架构

master 配置

[root@localhost ~]# cat /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  #
   vrrp_strict  
   vrrp_garp_interval 0 
   vrrp_gna_interval 0 
   vrrp_iptables  #此项和vrrp_strict同时开启时,则不会添加防火墙规则,如果无配置vrrp_strict项,则无需启用此项配置
}


vrrp_instance VI_1 {  
    state MASTER  # 当前节点为MASTER
    interface ens33  # 绑定为当前虚拟路由器使用的物理接口
    virtual_router_id 51  # 每个虚拟路由器惟一标识,各节点之间保持一致
    priority 100 # 主节点 这里设置为100
    advert_int 1 
    authentication {  
        auth_type PASS  # 各节点之间保持一致
        auth_pass 1111  
    }
    virtual_ipaddress {  #虚拟IP,生产环境可能指定上百个IP地址
        192.168.144.100  #指定VIP,不指定网卡,默认为eth0,注意:不指定/prefix,默认为/32
	192.168.144.102/24 dev ens33 label ens33:1
    }
}

重启keepalived

[root@localhost ~]#systemctl restart keepalived.service

查看虚拟IP是否存在

[root@localhost ~]#	ip  addr 

在这里插入图片描述

slave 配置

修改备用节点配置文
修改 下面注释的三项,其他保持和主节点一致

! 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
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_iptables
}

vrrp_instance VI_1 {
    state BACKUP #备用节点 修改为BACKUP
    interface ens33
    virtual_router_id 51
    priority 80 # 修改优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
	192.168.144.100
	192.168.144.102/24 dev ens33 label ens33:1

    }

}

测试:
1,停止主节点keepalive

[root@localhost ~]#systemctl stop keepalived.service

2, 观察slave节点 ,VIP是否漂移到备用节点

 [root@localhost ~]#ip addr 

在这里插入图片描述

keepalive 配置双主

在上面主备的基础上增加 vrrp_instance 语句块

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 
   vrrp_strict 
   vrrp_garp_interval 0 
   vrrp_gna_interval 0 
   vrrp_iptables 
}


vrrp_instance VI_1 { 
    state MASTER 
    interface ens33 
    virtual_router_id 51 
    priority 100
    advert_int 1
    authentication {
        auth_type PASS 
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.144.100/24 dev ens33 lavel ens33:1  

    }
}
######在主备基础上增加如下#######

vrrp_instance VI_2 {
    state BACKUP  #另外一个节点为MASTER
    interface ens33
    virtual_router_id 66 # 每个不同的vrrp_instance要唯一的路由ip
    priority 80  #另外主节点为 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.144.102/24 dev ens33 label ens33:2

    }

}

重启两个节点的keepalive 之后会看到每个机器上都有一个VIP

[root@localhost ~]# ip addr

在这里插入图片描述
在这里插入图片描述

keepalive+Nginx 实现高可用负载均衡

编写nginx状态监控脚本

#!/bin/bash
cunt=`ps -C nginx --no-header | wc -l`
date=`date -d "2 second" +"%Y-%m-%d %H:%M.%S"`
echo $date "   Nginx jincheng shu :"$cunt>>/var/log/nginx/nginx_status.log
if [ $cunt -eq 0 ];
then
   echo $date"  relod Nginx">>/var/log/nginx/nginx_status.log
   /usr/local/nginx/sbin/nginx
   sleep 5
   cunt=`ps -C nginx --no-header | wc -l`
   if [ $cunt -eq 0 ];
   then
      #systemctl stop keepalived
      echo $date "  重启失败,停止keepalived">>/var/log/nginx/nginx_status.log
      exit 0
   fi
fi

将脚本放到/usr/local/src/目录下,并赋予执行权限
在这里插入图片描述
并赋予执行权限

[root@localhost src]# chmod u+x check_nginx.sh

配置keepalived 执行监控脚本

在master 节点 配置文件中 添加全局配置

vrrp_script check_nginx {
        script "/usr/local/src/check_nginx.sh"
        interval 4 # 间隔多长时间执行一次脚本
        weight -20 # 如果脚本返回状态为0 节点优先级 -20
}

在vrrp_instance 语句块中引用脚本

    track_script {
    check_nginx
}

总的配置

[root@localhost src]# cat /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  
   vrrp_strict  
   vrrp_garp_interval 0 
   vrrp_gna_interval 0
   vrrp_iptables
}

vrrp_script check_nginx { 添加检测脚本 ,一搬在主节点添加即可
	script "/usr/local/src/check_nginx.sh"
	interval 4
	weight -20  
}
vrrp_instance VI_1 { 
    state MASTER 
    interface ens33  
    virtual_router_id 51  
    priority 100 
    advert_int 1 
    authentication {  
        auth_type PASS  
        auth_pass 1111  
    }
    track_script { #  引用监控脚本
        check_nginx
    }
    virtual_ipaddress { 
        192.168.144.100/24 dev ens33 lavel ens33:1 	
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 66
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.144.102/24 dev ens33 label ens33:2
    }

}

测试:
关闭nginx 看nginx 时候会自动重启。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨煮夕阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值