keepalived

Posted 码出未来_远

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived相关的知识,希望对你有一定的参考价值。

keepalived

1. keepalived简介

1.1 keepalived是什么?

Keepalived 软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:nginx、Haproxy、mysql等)的高可用解决方案软件。

Keepalived软件主要是通过VRRP协议实现高可用功能的。VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。

所以,Keepalived 一方面具有配置管理LVS的功能,同时还具有对LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。

keepalived官网

1.2 keepalived的重要功能

keepalived 有三个重要的功能,分别是:

  • 管理LVS负载均衡软件
  • 实现LVS集群节点的健康检查
  • 作为系统网络服务的高可用性(failover)

1.3 keepalived高可用故障转移的原理

Keepalived 高可用服务之间的故障切换转移,是通过 VRRP (Virtual Router Redundancy Protocol ,虚拟路由器冗余协议)来实现的。

在 Keepalived 服务正常工作时,主 Master 节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备 Backup 节点自己还活看,当主 Master 节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master 节点的心跳了,于是调用自身的接管程序,接管主 Master 节点的 IP 资源及服务。而当主 Master 节点恢复时,备 Backup 节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。

那么,什么是VRRP呢?
VRRP ,全 称 Virtual Router Redundancy Protocol ,中文名为虚拟路由冗余协议 ,VRRP的出现就是为了解决静态踣甶的单点故障问题,VRRP是通过一种竞选机制来将路由的任务交给某台VRRP路由器的。

1.4 keepalived原理

1.4.1 keepalived高可用架构图

image-20210622004658257

1.4.2 keepalived工作原理描述

Keepalived高可用对之间是通过VRRP通信的,因此,我们从 VRRP开始了解起:

  1. VRRP,全称 Virtual Router Redundancy Protocol,中文名为虚拟路由冗余协议,VRRP的出现是为了解决静态路由的单点故障。
  2. VRRP是通过一种竟选协议机制来将路由任务交给某台 VRRP路由器的。
  3. VRRP用 IP多播的方式(默认多播地址(224.0_0.18))实现高可用对之间通信。
  4. 工作时主节点发包,备节点接包,当备节点接收不到主节点发的数据包的时候,就启动接管程序接管主节点的开源。备节点可以有多个,通过优先级竞选,但一般 Keepalived系统运维工作中都是一对。
  5. VRRP使用了加密协议加密数据,但Keepalived官方目前还是推荐用明文的方式配置认证类型和密码。

介绍完 VRRP,接下来我再介绍一下 Keepalived服务的工作原理:

Keepalived高可用是通过 VRRP 进行通信的, VRRP是通过竞选机制来确定主备的,主的优先级高于备,因此,工作时主会优先获得所有的资源,备节点处于等待状态,当主挂了的时候,备节点就会接管主节点的资源,然后顶替主节点对外提供服务。

在 Keepalived 服务之间,只有作为主的服务器会一直发送 VRRP 广播包,告诉备它还活着,此时备不会枪占主,当主不可用时,即备监听不到主发送的广播包时,就会启动相关服务接管资源,保证业务的连续性.接管速度最快可以小于1秒。

2. keepalived配置文件讲解

2.1 keepalived默认配置文件

keepalived 的主配置文件是 /etc/keepalived/keepalived.conf。其内容如下:

[root@master ~]# 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_instance VI_1 {        //定义实例
    state MASTER            //指定keepalived节点的初始状态,可选值为MASTER|BACKUP
    interface eth0          //VRRP实例绑定的网卡接口,用户发送VRRP包
    virtual_router_id 51    //虚拟路由的ID,同一集群要一致
    priority 100            //定义优先级,按优先级来决定主备角色,优先级越大越优先
    nopreempt               //设置不抢占
    advert_int 1            //主备通讯时间间隔
    authentication {        //配置认证
        auth_type PASS      //认证方式,此处为密码
        auth_pass 1111      //同一集群中的keepalived配置里的此处必须一致,推荐使用8位随机数
    }
    virtual_ipaddress {     //配置要使用的VIP地址
        192.168.200.16
    }
}

virtual_server 192.168.200.16 1358 {    //配置虚拟服务器
    delay_loop 6        //健康检查的时间间隔
    lb_algo rr          //lvs调度算法
    lb_kind NAT         //lvs模式
    persistence_timeout 50      //持久化超时时间,单位是秒
    protocol TCP        //4层协议

    sorry_server 192.168.200.200 1358   //定义备用服务器,当所有RS都故障时用sorry_server来响应客户端

    real_server 192.168.200.2 1358 {    //定义真实处理请求的服务器
        weight 1    //给服务器指定权重,默认为1
        HTTP_GET {
            url {
              path /testurl/test.jsp    //指定要检查的URL路径
              digest 640205b7b0fc66c1ea91c463fac6334d   //摘要信息
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3       //连接超时时间
            nb_get_retry 3          //get尝试次数
            delay_before_retry 3    //在尝试之前延迟多长时间
        }
    }

    real_server 192.168.200.3 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
ip类型ip地址
VIP192.168.207.100
DIP1192.168.207.128
DIP2192.168.207.131
RIP1192.168.207.136
RIP2192.168.207.137
[root@131 ~]# yum -y install keepalived
[root@128 ~]# yum -y install keepalived
[root@136 ~]# yum -y install httpd
[root@137 ~]# yum -y install httpd

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

关闭防火墙和selinux

[root@137 ~]# systemctl disable --now firewalld
[root@137 ~]# setenforce 0
[root@137 ~]# vim /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted
[root@136 ~]# systemctl disable --now firewalld
[root@136 ~]# setenforce 0
[root@136 ~]# vim /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted
[root@131 ~]# systemctl disable --now firewalld
[root@131 ~]# setenforce 0
[root@131 ~]# vim /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted
[root@128 ~]# systemctl disable --now firewalld
[root@128 ~]# setenforce 0
[root@128 ~]# vim /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted

在调度器上配置VIP和DIP

[root@131 ~]# dnf -y install ipvsadm
[root@131 ~]# dnf -y install keepalived
[root@128 ~]# dnf -y install ipvsadm
[root@128 ~]# dnf -y install keepalived
[root@131 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR0=192.168.207.131
PREFIX0=24
IPADDR1=192.168.207.100
PREFIX1=24
GATEWAY=192.168.207.2
DNS1=114.114.114.114
[root@128 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR0=192.168.207.128
PREFIX0=24
IPADDR1=192.168.207.100
PREFIX1=24
GATEWAY=192.168.207.2
DNS1=114.114.114.114

在RS上先关闭arp包的通告和响应然后配置RIP和VIP

[root@136 ~]# dnf -y install net-tools
[root@137 ~]# dnf -y install net-tools
[root@137 ~]# cat /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
[root@137 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@137 ~]# ifconfig lo:0 192.168.207.100/32 broadcast 192.168.207.100 up
[root@137 ~]# route add -host 192.168.207.100 dev lo:0
[root@137 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.207.2   0.0.0.0         UG    100    0        0 ens33
192.168.207.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.207.100 0.0.0.0         255.255.255.255 UH    0      0        0 lo
[root@137 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
    inet 192.168.207.100/0 brd 192.168.207.100 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:e1:7e:2b brd ff:ff:ff:ff:ff:ff
    inet 192.168.207.137/24 brd 192.168.207.255 scope global dynamic noprefixroute ens33
       valid_lft 1685sec preferred_lft 1685sec
    inet6 fe80::d179:e52e:1aae:e40e/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

[root@136 ~]# vim /etc/sysctl.conf 
[root@136 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@136 ~]# ifconfig lo:0 192.168.207.100/32 broadcast 192.168.207.100 up
[root@136 ~]# route add -host 192.168.207.100 dev lo:0
[root@136 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.207.2   0.0.0.0         UG    100    0        0 ens33
192.168.207.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.207.100 0.0.0.0         255.255.255.255 UH    0      0        0 lo
[root@136 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
    inet 192.168.207.100/0 brd 192.168.207.100 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d3:e3:05 brd ff:ff:ff:ff:ff:ff
    inet 192.168.207.136/24 brd 192.168.207.255 scope global dynamic noprefixroute ens33
       valid_lft 1568sec preferred_lft 1568sec
    inet6 fe80::6eb1:5c11:10e3:b7c8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever


在调度器上添加规则

[root@131 ~]# ipvsadm -A -t 192.168.207.100:80 -s rr
[root@131 ~]# ipvsadm -a -t 192.168.207.100:80 -r 192.168.207.5:80 -g
[root@131 ~]# ipvsadm -a -t 192.168.207.100:80 -r 192.168.207.6:80 -g
[root@131 ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@128 ~]# ipvsadm -A -t 192.168.207.100:80 -s rr
[root@128 ~]# ipvsadm -a -t 192.168.207.100:80 -r 192.168.207.5:80 -g
[root@128 ~]# ipvsadm -a -t 192.168.207.100:80 -r 192.168.207.6:80 -g
[root@128 ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm

配置keepalived主配置文件

配置主keepalived

[root@131 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678
    }
    virtual_ipaddress {
       192.168.207.100 
    }
}

virtual_server 192.168.207.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.207.5 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.207.6 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@131 ~]# systemctl start keepalived
[root@131 ~]# systemctl enable keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.


配置备keepalived

[root@128 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 100
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678
    }
    virtual_ipaddress {
       192.168.207.100
    }
}

virtual_server 192.168.207.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.207.5 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.207.6 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@128 ~]# systemctl start keepalived
[root@128 ~]# systemctl enable keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.

查看VIP

在MASTER上查看

[root@131 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:8f:ed:d0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.207.131/24 brd 192.168.207.255 scope global dynamic noprefixroute ens33
       valid_lft 1089sec preferred_lft 1089sec
    inet 192.168.207.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::825d:8cb3:9d2b:703f/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

在SLAVE上查看

[root@128 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:e0:3f:52 brd ff:ff:ff:ff:ff:ff
    inet 192.168.207.128/24 brd 192.168.207.255 scope global dynamic noprefixroute ens33
       valid_lft 1038sec preferred_lft 1038sec
    inet6 fe80::20c:29ff:fee0:3f52/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

让keepalived监控监控LVS的LB

在master上编写脚本

[root@131 ~]# mkdir /scripts
[root@131 ~]# vi /scripts/notify.sh 
[root@131 ~]# cat /scripts/notify.sh 
#!/bin/bash
case "$1" in
  master)
            ipvsadm -R < /etc/sysconfig/ipvsadm
  ;;
  backup)
            ipvsadm -C
  ;;
  *)
        echo "Usage:$0 master|backup"
  ;;
esac
[root@131 ~]# chmod +x /scripts/notify.sh 

在SLAVE上编写脚本

[root@128 ~]# mkdir /scripts
[root@128 ~]# vi /scripts/notify.sh 
[root@128 ~]# cat /scripts/notify.sh 
#!/bin/bash
case "$1" in
  master)
            ipvsadm -R < /etc/sysconfig/ipvsadm
  ;;
  backup)
            ipvsadm -C
  ;;
  *)
        echo "Usage:$0 master|backup"
  ;;
esac

[root@128 ~]# chmod +x /scripts/notify.sh

配置keepalived加入故障转移脚本

配置主keepalived

[root@131 ~]# vi /etc/keepalived/keepalived.conf
[root@131 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678
    }
    virtual_ipaddress {
       192.168.207.100 
    }
    notify_master "/scripts/notify.sh master"
    notify_backup "/scripts/notify.sh backup"
}

virtual_server 192.168.207.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.207.5 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.207.6 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@131 ~]# systemctl restart keepalived
[root@131 ~]# systemctl enable keepalived

配置备keepalived

[root@128 ~]# vi /etc/keepalived/keepalived.conf
[root@128 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 100
    priority 90
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678
    }
    virtual_ipaddress {
       192.168.207.100
    }
    notify_master "/scripts/notify.sh master"
    notify_master "/scripts/notify.sh master"
}

virtual_server 192.168.207.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.207.5 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.207.6 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@128 ~]# systemctl restart keepalived
[root@128 ~]# systemctl enable keepalived

测试验证

模拟主宕机

[root@131 ~]# poweroff
[root@128 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:e0:3f:52 brd ff:ff:ff:ff:ff:ff
    inet 192.168.207.128/24 brd 192.168.207.255 scope global dynamic noprefixroute ens33
       valid_lft 1308sec preferred_lft 1308sec
    inet 192.168.207.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fee0:3f52/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@137 ~]# curl 192.168.207.100
<h1>test1</h1>
[root@138 ~]# curl 192.168.207.100
<h1>test2</h1>

在这里插入图片描述

以上是关于keepalived的主要内容,如果未能解决你的问题,请参考以下文章

keepalived双主双从配置

Oracle Compute云快速搭建MySQL Keepalived高可用架构

keepalived高可用

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js