keepalive 之 配置介绍

2019-12-13 00:31栏目:操作系统
TAG:

简介

keepalived是HA Cluster(High Availability Cluster,高可用集群)的二个服务软件,用来幸免单点故障。

keepalived采纳V奥迪Q5RP(virtual router redundancy protocol,虚构路由冗余公约),以软件的款型实现服务器热备功效。平常状态下是将两台linux服务器组成二个热备组(master-backup),同时热备组内独有大器晚成台主服务器(master)提供劳动,同不时候master会设想出贰个共用IP地址(VIP),这些VIP只存在master上并对外提供服务;若是keepalived检查实验到master宕机或劳务故障,备用服务器(backup)会自动接管VIP成为master,keepalived并将master从热备组移除,当master复苏后,会自动步向到热备组,暗许再抢占成为master,起到故障转移职能。

高可用集群首要有二种实现格局:主备形式和主主形式:
主备形式:三个或三个VIP,意气风发台主机对外提供劳动,其余朝气蓬勃台做备用机,当主服务器现身难点,备用服务器接管IP继续提供劳动;
主主情势:三个或多个VIP,两台主机都对外提供服务,能够是同三个劳务,也能够是差别的劳动,这种格局升高了硬件的投入,也对负荷均衡起到自然的法力。

keepalived主要有八个模块,分别是ipvs wrapper、checkers、vrrp stack。ipvs wrapper模块为keepalived的骨干,担负主进度的起步、维护以致全局配置文件的加载和解析;checkers担当健检,包涵广大的各样检查措施;vrrp stack模块是来贯彻VKoleosRP合同的。

1  概述

风华正茂、高可用集群

铺排文件

keepalived的暗中认可配置文件为/etc/keepalived/keepalived.conf,其主程序文件为/usr/sbin/keepalived。keepalived的布局文件的构成首要分为三局地,而各样部分下边又包涵着各自的子段,其重新整合及陈设如下:

正文首要介绍keepalive 的有关安排

(意气风发)提高系统高可用性的减轻方案:冗余(redundant卡塔尔(قطر‎

  • 做事方式

    • active/passive:主备
    • active/active:双主
  • 以心跳情势通报

    • active --> HEARTBEAT --> passive
    • active <--> HEARTBEAT <--> active
  • 故障管理

    • failover:故障切换,即某财富的主节点故障时,将财富转移至其余节点的操作
    • failback:故障移回,即某能源的主节点故障后重新修正上线后,将以前已转移至其余节点的财富重复切回的进度

1、GLOBAL CONFIGURATION:全局配置段,包罗 Global definitions、Static routes/address/rules

2  keepalived安装配备

(二)HA Cluster完结方案

  • ais:应用接口标准完善复杂的HA集群
    GL450HCS:Red Hat Cluster Suite红帽集群套件
    heartbeat
    corosync

  • vrrp合同落到实处:设想路由冗余构和
    keepalived

Global definitions:用于定义全局设置,常用的参数及示范如下:

global_defs {
    notification_email {  #指定报警邮件发往的邮箱地址
        root@localhost
    }
    notification_email_from keepalived@localhost  #指定报警邮件的发件人
    smtp_server 127.0.0.1  #指定邮件服务器的地址
    smtp_connect_timeout 30  #指定邮件服务器的连接超时时长
    router_id node1  #设置路由器的标识
    vrrp_mcast_group4 224.0.100.19  #设置vrrp的广播地址,在同一个HA Cluster中要确保其广播地址一致,才能接收到对应的vrrp报文
    vrrp_strict  #执行严格的vrrp协议检查,下列情况将会阻止启动Keepalived:1. 没有VIP地址。2. 单播邻居。3. 在VRRP版本2中有IPv6地址。
}

CentOS 6.4以上的版本都合併在Base源里

二、KeepAlived基本介绍

Static routes/address/rules:用于配置keepalived中不会被vrrp移除的静态地址、路由大概法则,基本不用。

.程序处境:

(大器晚成)VQX56RP(Virtual Router Redundancy Protocol卡塔尔(英语:State of Qatar)左券术语

  • 编造路由器:Virtual Router,几个概略路由器对外以七个IP地址提供劳务,有如生机勃勃台路由器

    • 假造路由器标记:VLANDID(0-255卡塔尔(英语:State of Qatar),唯风流倜傥标志设想路由器
    • VIP:Virtual IP,虚拟IP
    • VMAC:Virutal MAC (00-00-5e-00-01-VRID),虚拟MAC
  • 大要路由器
    master:主设备
    backup:备用设备
    priority:优先级

2、VENVISIONRPD CONFIGURATION:vrrp相关安顿段

.主配置文件:/etc/keepalived/keepalived.conf

(二)KeepAlived的劳作特色

  • 通告:心跳,优先级等;周期性

  • 专门的学业方法:抢占式,非抢占式

  • 康宁认证:

    • 无认证
    • 简易字符认证:预分享密钥
    • MD5
  • 做事方式:

    • 主/备:单设想路由器
    • 主/主:主/备(虚拟路由器1),备/主(设想路由器2)

VRRP instance(s):定义vrrp同步组。

vrrp_instance VIP_1 {    #定义vrrp实例,VIP_1为自定义实例名
    state MASTER|BACKUP    #指定此虚拟路由器在vrrp组的角色
    interface eno16777736    #绑定物理接口
    virtual_router_id 14    #唯一标识id,用于区分vrrp实例,范围为0-255
    priority 100    #设定优先级,范围为1-254
    advert_int 1    #发送vrrp通告的时间间隔
    nopreempt|preempt    #设定工作模式为非抢占或抢占模式
    preempt_delay 300    #在抢占式模式下,节点上线后触发新选举的延迟时长
    authentication {    #设置vrrp实例协商的方式及密码
        auth_type PASS    #定义认证类型为简单密码认证
        auth_pass 571f97b2    #定义密码串,最长不超过8个字符
    }
    virtual_ipaddress {    #在绑定的物理接口上添加虚拟ip地址
        #<IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPE> label <LABEL>
        10.1.0.91/16 dev eno16777736
    }
    track_interface {    #配置需要监控的网络接口,一旦接口出现故障该vrrp实例转为FAULT状态
      eth0
      eth1
      ...
     }
    track_script {    #调用在vrrp_script中定义的脚本,根据脚本进行监控调整
       <SCRIPT_NAME>
       <SCRIPT_NAME> weight <-254..254>
    }
    notify_master <STRING>|<QUOTED-STRING>    #当前节点成为主节点时触发的通知脚本
    notify_backup <STRING>|<QUOTED-STRING>    #当前节点转为备节点时触发的通知脚本
    notify_fault <STRING>|<QUOTED-STRING>    #当前节点转为fault状态时触发的通知脚本
    notify_stop <STRING>|<QUOTED-STRING>     #当前节点停止时所触发的通知脚本
}

.主程序文件:/usr/sbin/keepalived

(三)KeepAlived的功能

  • vrrp合同完结地点流动

  • 为vip地址所在的节点生成ipvs准绳(在配置文件中先行定义卡塔尔(英语:State of Qatar)

  • 为ipvs集群的各奥迪Q5S做健康情形检验

  • 基于脚本调用接口通过试行脚本实现脚本中定义的意义,从而影响集群事务,以此补助nginx, haproxy等服务

VRRP script(s卡塔尔国:定义周期性试行的台本,用于检查相应的劳务或ip状态。

vrrp_script <SCRIPT_NAME> {    #定义周期执行的脚本,vrrp instances会根据脚本的退出码来调整优先级
    script <STRING>|<QUOTED-STRING>    #定义执行脚本的存放路径
    interval INT     #定义调用执行脚本的周期,默认为1s
    timeout <INTEGER>    #脚本执行超时时间,脚本执行超时后,则被认为失败
    rise <INTEGER>        #定义脚本检查成功多少次,才认可当前的状态为正常
    fall <INTEGER>        #定义检查失败多少次,才认为当前状态为失败
}

.Unit File:/usr/lib/systemd/system/keepalived.service

三、KeepAlived的配置

3、LVS CONFIGURATION:LVS配置段

.Unit File的条件布署文件:/etc/sysconfig/keepalived

(风流倜傥)HA Cluster配置酌量:

  • 各节点时间必得一同:ntp服务(CentOS 6), chrony(CentOS 7)

    // 由于ntp/chrony服务不能同步差距过大的时间,需要先使用ntpdate命令同步一次,再开启服务
    ntpdate ntp_server_ip
    // 开启chronyd服务(CentOS 7)
    vim /etc/chrony.conf
    server 172.18.0.1 iburst
    systemctl enable chronyd
    systemctl start chronyd
    // 开启ntp服务(CentOS 6)
    vim /etc/ntp.conf
    server 172.18.0.1 iburst
    chkconfig ntpd on
    service ntpd start
    
  • 管教iptables及selinux不会化为阻碍

  • 各节点之间可通过主机名相互像信(对KA并不是必得),提出选用/etc/hosts文件落到实处

  • 各节点之间的root客商能够凭借密钥认证的ssh服务到位互相近信(对KA并不是必得)

    ssh-keygen
    ssh-copy-id destination_ip
    

Virtual server(s卡塔尔:用于定义设想服务器的装置,设想服务器能够用ip port、fwmark、virtual server group(s卡塔尔来定义。

virtual_server IP port | virtual_server fwmark <int>  |virtual_server group string
{
    delay_loop <INT>    #健康检查的时间间隔
    lb_algo rr|wrr|lc|wlc|lblc|sh|dh    #lvs调度方法
    lb_kind NAT|DR|TUN    #集群类型
    persistence_timeout <INT>    #持久连接时长
    protocol TCP|UDP|SCTP    #服务协议
    sorry_server <IPADDR> <PORT>    #备用服务器
    real_server <IPADDR> <PORT>{
        weight <INT>    #指定权重,默认是1
        notify_up <STRING>|<QUOTED-STRING>    #服务器健康检查成功时执行的脚本
        notify_down <STRING>|<QUOTED-STRING>    #服务器健康检查失败时执行的脚本
        HTTP_GET|SSL_GET {    #应用层检测
            url {
                path <URL_PATH>    #定义要监控的URL
                status_code <INT>    #健康状态的响应码
                digest <STRING>    #健康状态的响应的内容的校验码
            }
            nb_get_retry <INT>    #重试次数
            delay_before_retry <INT>    #重试之前的延迟时长
            connect_ip <IP ADDRESS>    #向哪个IP地址发起健康状态检测请求,默认是real server的ip地址
            connect_port <PORT>    #向哪个PORT发起健康状态检测请求,默认是real server的端口
            bindto <IP ADDRESS>    #发起连接的接口的ip地址
            bind_port <PORT>    #发起连接的接口的地址端口
            connect_timeout <INTEGER>    #连接请求的超时时长
        }
        TCP_CHECK {
            connect_ip <IP ADDRESS>    #向哪个IP地址发起健康状态检测请求,默认是real server的ip地址
            connect_port <PORT>    #向哪个PORT发起健康状态检测请求,默认是real server的端口
            bindto <IP ADDRESS>    #发起连接的接口的ip地址
            bind_port <PORT>    #发起连接的接口的地址端口
            connect_timeout <INTEGER>    #连接请求的超时时长
        }
    }
}

3配备文件组件部分

(二)KeepAlived的程序情形

  • 主配置文件:/etc/keepalived/keepalived.conf

  • 主程序文件:/usr/sbin/keepalived

  • Unit File:/usr/lib/systemd/system/keepalived.service

  • Unit File的景况陈设文件:/etc/sysconfig/keepalived

 配置实例:主主方式

#一台主机配置VIP_1、VIP_2互为主备
global_defs {
    notification_email {
        root@localhost
    }
    notification_email_from keepalived@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id node1
    vrrp_mcast_group4 224.0.100.19
}

vrrp_instance VIP_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 14
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 571f97b2
    }
    virtual_ipaddress {
        10.1.0.91/16 dev eno16777736
    }
}

vrrp_instance VIP_2 {
    state BACKUP
    interface eno16777736
    virtual_router_id 15
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 578f07b2
    }
    virtual_ipaddress {
        10.1.0.92/16 dev eno16777736
    }
}
#一台主机配置VIP_1、VIP_2互为主备
global_defs {
    notification_email {
        root@localhost
    }
    notification_email_from keepalived@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id node2
    vrrp_mcast_group4 224.0.100.19
}

vrrp_instance VIP_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 16
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 571f97b2
    }
    virtual_ipaddress {
        10.1.0.91/16 dev eno16777736
    }
}

vrrp_instance VIP_2 {
    state MASTER
    interface eno16777736
    virtual_router_id 17
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 578f07b2
    }
    virtual_ipaddress {
        10.1.0.92/16 dev eno16777736
    }
}

 

2.1  组配置文件

(三)KeepAlived的布署文件布局

  • GLOBAL CONFIGURATION:全局设置
    Global definitions
    Static routes/addresses

  • VRRPD CONFIGURATION:VRRP设置
    VRRP synchronization group(s):vrrp同步组
    VSportageRP instance(s卡塔尔(قطر‎:即一个vrrp设想路由器

  • LVS CONFIGURATION:LVS设置
    Virtual server group(s)
    Virtual server(s):ipvs集群的vs和rs

有三段配置

(四)配置虚构路由器

  • 语法:

    vrrp_instance <STRING> {
    ....
    }
    
  • 专项使用参数:

    • state MASTER | BACKUP
      当下节点在这设想路由器上的发端状态;只可以有叁个是MASTETiggo,余下的都应为BACKUP
    • interface IFACE_NAME
      绑定为近年来虚构路由器使用的情理接口
    • virtual_router_id VRID
      这段日子设想路由器惟大器晚成标志,范围是0-255
    • priority 100
      脚下大要节点在那虚构路由器中的优先级;范围1-254
    • advert_int 1
      vrrp通告的时刻间距,暗中同意1s
    • authentication:认证机制
    authentication {
    auth_type AH|PASS
    auth_pass <PASSWORD> 仅前8位有效
    }
    
    • virtual_ipaddress:虚拟IP
    virtual_ipaddress { 
    <IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPE> label <LABEL>
    }
    
    • track_interface:配置监察和控制互连网接口,风姿罗曼蒂克旦现身故障,则转为FAULT状态完结地方转移
    track_interface {
    eth0
    eth1
    …
    }
    
    • nopreempt:定义职业方式为非抢占格局
    • preempt_delay 300:抢占式格局,节点上线后触发新选出操作的推移时间长度,暗许情势
    • 概念通告脚本:
      notify_master <STRING> | <QUOTED-STRING>:
      时下节点成为主节点时触发的剧本
      notify_backup <STRING> | <QUOTED-STRING>:
      日前节点转为备节点时接触的台本
      notify_fault <STRING> | <QUOTED-STRING>:
      时下节点转为“失利”状态时触发的脚本
      notify <STRING> | <QUOTED-STRING>:
      通用格式的通报触发机制,多个剧本可实现上述二种意况的调换时的照拂
  • 尝试1:达成主/备设想路由器

    • 尝试碰到:
      物理路由器1:ip: 192.168.136.230, 主机名: node1, MASTETiguan
      物理路由器2:ip: 192.168.136.130, 主机名: node2, BACKUP
      VIP:192.168.136.100
    // 配置物理路由器1
    vim /etc/keepalived/keepalived.conf
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from node1@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id node1                  // vrrp中的路由器主机名
       vrrp_mcast_group4 224.0.0.58     // 设置组播ip地址
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface ens37
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass dd73f9d6          // openssl rand -hex 4 生成8位16进制密码
        }
        virtual_ipaddress {
            192.168.136.100/24
        }
    }
    
    systemctl start keepalived
    
    // 配置物理路由器2
    vim /etc/keepalived/keepalived.conf
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from node2@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id node2
       vrrp_mcast_group4 224.0.0.58
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface ens37
        virtual_router_id 51
        priority 90                    //作为BACKUP优先级比MASTER要低
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass dd73f9d6         // 密码与node1相同
        }
        virtual_ipaddress {
           192.168.136.100/24
        }
    }
    
    systemctl start keepalived
    
    • 测试
      node1的ip地址早已现身VIP

    图片 1

    监听组播地址的tcp连接tcpdump -i ens37 -nn host 224.0.0.58,当时闭馆node1的keepalived服务systemctl stop keepalived,自动由node2接管并早先申明本人兼顾设想路由器的IP

    图片 2

    VIP那时早就被node2接管

    图片 3

  • 实验2:实现keepalived日志
vim /etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -S 3"    // -D:详细日志,-S 3: 设置日志facility为local3
vim /etc/rsyslog.conf 
local3.*               /var/log/keepalived.log    // 设置日志存储路径
systemctl restart rsyslog
systemctl restart keepalived
tail -f  /var/log/keepalived.log

图片 4

  • 实行3:完结主/主虚构路由器,何况当节点发生变化时主动发送邮件

    • 实验境况
      物理路由器1:ip: 192.168.136.230, 主机名: node1
      物理路由器2:ip: 192.168.136.130, 主机名: node2
      设想路由器1:MASTE奥迪Q7: node1, BACKUP: node2, VIP: 192.168.136.100
      设想路由器2:MASTE宝马7系: node2, BACKUP: node1, VIP: 192.168.136.200
    // 配置物理路由器1(虚拟路由器1的MASTER,虚拟路由器2的BACKUP)
    vim /etc/keepalived/keepalived.conf
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id node1
       vrrp_mcast_group4 224.0.0.58
    }
    // 虚拟路由器1的设置
    vrrp_instance VI_1 {
        state MASTER
        interface ens37
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass dd73f9d6
        }
        virtual_ipaddress {
            192.168.136.100/24
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    }
    // 虚拟路由器2的设置
    vrrp_instance VI_2 {
        state BACKUP
        interface ens37
        virtual_router_id 61
        priority 80
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass a56c19be
        }
        virtual_ipaddress {
            192.168.136.200/24
       }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    }
    systemctl restart keepalived
    
    // 配置物理路由器2(虚拟路由器1的BACKUP,虚拟路由器2的MASTER)
    vim /etc/keepalived/keepalived.conf
    global_defs {
       notification_email {
       root@localhost
       }
       notification_email_from node2@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id node2
       vrrp_mcast_group4 224.0.0.58
    }
    // 虚拟路由器1的设置
    vrrp_instance VI_1 {
        state BACKUP
        interface ens37
        virtual_router_id 51
        priority 90
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass dd73f9d6
        }
        virtual_ipaddress {
           192.168.136.100/24
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    }
    // 虚拟路由器2的设置
    vrrp_instance VI_2 {
        state MASTER
        interface ens37
        virtual_router_id 61
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass a56c19be
        }
        virtual_ipaddress {
            192.168.136.200/24
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    }
    
    // 在物理路由器1,2上添加脚本文件
    vim /etc/keepalived/notify.sh
    #! /bin/bash
    
    contact='root@localhost'
    notify() {
            mailsubject="$(hostname) to be $1, vip floating"
            mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
            echo "$mailbody" | mail -s "$mailsubject" $contact
    }
    
    case $1 in
    master)
            notify master
            ;;
    backup)
            notify backup
            ;;
    fault)
            notify fault
            ;;
    *)
            echo "Usage: $(basename $0) {master|backup|fault}"
            exit 1
            ;;
    esac
    chmod +x /etc/keepalived/notify.sh
    
    • 测试
      监听组播地址的tcp连接tcpdump -i ens37 -nn host 224.0.0.58,能够看出node1, node2分别注解具备设想路由器1(vrid 51卡塔尔、虚构路由器2(vrid61卡塔尔(قطر‎的IP地址

    图片 5

    分别查看node1和node2的网卡IP地址,进一层确定上述结果

    图片 6

    图片 7

    此刻,断开node1的网络连接
    虚构路由器1的VIP立即由node2的网卡接管

    图片 8

    图片 9

    过来node1的网络连接,在node1和node2上都得以见见相应的邮件文告:
    node1上通报出错,不慢文告本身被切换为BACKUP,复苏网络连接后公告作者重新产生MASTELAND;

    图片 10

    node2上通报笔者切换为MASTE奔驰M级,复苏互联网连接后公告自身切换为BACKUP

    图片 11

GLOBAL CONFIGURATION

(五)Keepalived支持IPVS

  • 语法:
virtual_server {IP port | fwmark int}
{
    ...
    real_server{
        ...
    }
    ...
}
  • virtual_server常用参数

    • delay_loop <INT>
      自笔者商酌后端服务器的光阴世距
    • lb_algo rr|wrr|lc|wlc|lblc|sh|dh
      概念调节措施
    • lb_kind NAT|DR|TUN
      集群的类别
    • persistence_timeout <INT>
      有头有尾连接时间长度
    • protocol TCP
      服务左券,仅辅助TCP
    • sorry_server<IPADDR> <PORT>
      具有PRADOS故障时,备用服务器地址
  • real_server <IPADD牧马人> <PORT>常用参数

    • weight <INT>
      RS权重
    • notify_up <STRING>|<QUOTED-STRING>
      HighlanderS上线文告脚本
    • notify_down <STRING>|<QUOTED-STRING>
      TucsonS下线通告脚本
    • HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK { ... }
      概念当前主机的健康情形检查评定方法
  • HTTP_GET|SSL_GET:应用层健康意况检查实验

    HTTP_GET|SSL_GET {
    url {
    path <URL_PATH>               // 定义要监控的URL
    status_code <INT>             // 判断上述检测机制为健康状态的响应码
    digest <STRING>               // 判断为健康状态的响应的内容的校验码
    }
    connect_timeout <INTEGER>     // 连接请求的超时时长
    nb_get_retry <INT>            // 重试次数
    delay_before_retry <INT>      // 重试之前的延迟时长
    connect_ip <IP ADDRESS>       // 向当前RS哪个IP地址发起健康状态检测请求
    connect_port <PORT>           // 向当前RS的哪个PORT发起健康状态检测请求
    bindto <IP ADDRESS>           // 发出健康状态检测请求时使用的源地址
    bind_port <PORT>              // 发出健康状态检测请求时使用的源端口
    }
    
  • TCP_CHECK参数

    • connect_ip <IP ADDRESS>
      向当前LacrosseS的哪位IP地址发起健康状态检测须要
    • connect_port <PORT>
      向当前RubiconS的哪位PORT发起健康情况检查测验诉求
    • bindto <IP ADDRESS>
      暴发健康意况检查测量试验诉求时选拔的源地址
    • bind_port <PORT>
      发出健康状态检查评定央求时接纳的源端口
    • connect_timeout <INTEGER>
      接连诉求的逾期时间长度
  • 实验4:完成主/备模型的IPVS集群

    • 实验情形:
      LB1(master)/VS:IP: 192.168.136.230
      LB2(backup)/VS:IP: 192.168.136.130
      VIP:192.168.136.100
      RS1:IP: 192.168.136.229
      RS2:IP: 192.168.136.129
    // 配置LB1的keepalived设置
    vim /etc/keepalived/keepalived.conf
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from node1@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id node1
       vrrp_mcast_group4 224.0.0.58
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface ens37
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass dd73f9d6
        }
        virtual_ipaddress {
            192.168.136.100/24
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    }
    
    virtual_server 192.168.136.100 80{
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        protocol TCP
        sorry_server 127.0.0.1 80
        real_server 192.168.136.229 80{
            weight 2
            HTTP_GET {
                url {
                  path /
                  status_code 200
                }
                connect_timeout 1
                nb_get_retry 3
                delay_before_retry 1
            }
        }
        real_server 192.168.136.129 80{
            weight 1
            HTTP_GET {
                url {
                  path /
                  status_code 200
                }
                connect_timeout 1
                nb_get_retry 3
                delay_before_retry 1
            }
        }
    }
    
    // 配置LB2的keepalived设置
    vim /etc/keepalived/keepalived.conf
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from node2@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id node2
       vrrp_mcast_group4 224.0.0.58
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface ens37
        virtual_router_id 51
        priority 90
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass dd73f9d6
        }
        virtual_ipaddress {
           192.168.136.100/24
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    }
    
    virtual_server 192.168.136.100 80{
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        protocol TCP
        sorry_server 127.0.0.1 80
        real_server 192.168.136.229 80{
            weight 2
            HTTP_GET {
                url {
                  path /
                  status_code 200
                }
                connect_timeout 1
                nb_get_retry 3
                delay_before_retry 1
            }
        }
        real_server 192.168.136.129 80{
            weight 1
            HTTP_GET {
                url {
                  path /
                  status_code 200
                }
                connect_timeout 1
                nb_get_retry 3
                delay_before_retry 1
            }
        }
    }
    
    // 配置LB1, LB2的sorry server服务
    echo sorry on LB1 > /var/www/html/index.html     // LB1上操作
    echo sorry on LB2 > /var/www/html/index.html     // LB2上操作
    systemctl start httpd
    
    // 配置RS1, RS2的Web服务
    echo RS1 homepage > /var/www/html/index.html     // RS1上操作
    echo RS2 homepage > /var/www/html/index.html     // RS2上操作
    systemctl start httpd
    
    // 编辑脚本实现:禁止RS响应ARP请求,并将网卡绑定VIP
    vim lvs_dr_rs.sh
    #! /bin/bash
    vip='192.168.136.100'
    mask='255.255.255.255'
    dev=lo:1
    rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
    service httpd start &> /dev/null && echo "The httpd Server is Ready!"
    
    case $1 in
    start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ifconfig $dev $vip netmask $mask broadcast $vip up
        echo "The RS Server is Ready!"
        ;;
    stop)
        ifconfig $dev down
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo "The RS Server is Canceled!"
        ;;
    *)
        echo "Usage: $(basename $0) start|stop"
        exit 1
        ;;
    esac
    
    chmod +x lvs_dr_rs.sh
    bash lvs_dr_rs.sh start
    
    // LB1, LB2启动KeepAlived服务,进行测试
    systemctl start keepalived
    

    拜候VIP(192.168.136.100卡塔尔国的Web服务,不奇怪干活

    图片 12

    停下OdysseyS2的Web服务,自动进行健检,全体调治至LacrosseS1

    图片 13

    甘休凯雷德S1的Web服务,自动进行健检,调整至LB1的sorry server

    图片 14

    停下LB1的KeepAlived服务,自动切换至LB2

    图片 15

  • 实践5:实现主/主模型的IPVS集群

    • 实践情况:
      LB1/VS1:IP: 192.168.136.230,后端RS: RS1, RS2
      LB2/VS2:IP: 192.168.136.130,后端RS: RS3, RS4
      LB1 VIP:192.168.136.100
      LB2 VIP:192.168.136.200
      RS1:IP: 192.168.136.229
      RS2:IP: 192.168.136.129
      RS3:IP: 192.168.136.240
      RS4:IP: 192.168.136.250
      LB之间互为MASTE揽胜极光与BACKUP的涉嫌
      MASTER:LB1,BACKUP:LB2
      MASTER:LB2,BACKUP:LB1
    // 配置LB1, LB2的keepalived设置
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from node1@localhost     // LB1上操作
       notification_email_from node1@localhost     // LB2上操作
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id node1                             // LB1上操作
       router_id node2                             // LB2上操作
       vrrp_mcast_group4 224.0.0.58
    }
    vrrp_instance VI_1 {
        state MASTER                               // LB1上操作
        state BACKUP                               // LB2上操作
        interface ens37
        virtual_router_id 51
        priority 100                               // LB1上操作
        priority 90                                // LB2上操作
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass dd73f9d6
        }
        virtual_ipaddress {
            192.168.136.100/24
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    }
    vrrp_instance VI_2 {
        state BACKUP                               // LB1上操作
        state MASTER                               // LB2上操作
        interface ens37
        virtual_router_id 61
        priority 80                                // LB1上操作
        priority 100                               // LB2上操作
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass a56c19be
        }
        virtual_ipaddress {
            192.168.136.200/24
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    
    }
    virtual_server 192.168.136.100 80{
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        protocol TCP
        sorry_server 127.0.0.1 80
        real_server 192.168.136.229 80{
            weight 2
            HTTP_GET {
                url {
                  path /
                  status_code 200
                }
                connect_timeout 1
                nb_get_retry 3
                delay_before_retry 1
            }
        }
    real_server 192.168.136.129 80{
            weight 1
            HTTP_GET {
                url {
                  path /
                  status_code 200
                }
                connect_timeout 1
                nb_get_retry 3
                delay_before_retry 1
            }
        }
    }
    virtual_server 192.168.136.200 80{
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        protocol TCP
        sorry_server 127.0.0.1 80
        real_server 192.168.136.240 80{
            weight 2
            HTTP_GET {
                url {
                  path /
                  status_code 200
                }
                connect_timeout 1
                nb_get_retry 3
                delay_before_retry 1
            }
        }
        real_server 192.168.136.250 80{
            weight 1
            HTTP_GET {
                url {
                  path /
                  status_code 200
                }
                connect_timeout 1
                nb_get_retry 3
                delay_before_retry 1
           }
       }
    }
    
    // 配置LB1, LB2的sorry server服务
    echo sorry on LB1 > /var/www/html/index.html     // LB1上操作
    echo sorry on LB2 > /var/www/html/index.html     // LB2上操作
    systemctl start httpd
    
    // 配置RS1, RS2, RS3, RS4的Web服务
    echo RS1 homepage > /var/www/html/index.html     // RS1上操作
    echo RS2 homepage > /var/www/html/index.html     // RS2上操作
    echo RS3 homepage > /var/www/html/index.html     // RS3上操作
    echo RS4 homepage > /var/www/html/index.html     // RS4上操作
    systemctl start httpd
    
    // 编辑脚本实现:禁止RS响应ARP请求,并将网卡绑定VIP
    vim lvs_dr_rs.sh
    #! /bin/bash
    vip='192.168.136.100'                            // RS1, RS2上操作
    vip='192.168.136.200'                            // RS3, RS4上操作
    mask='255.255.255.255'
    dev=lo:1
    rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
    service httpd start &> /dev/null && echo "The httpd Server is Ready!"
    
    case $1 in
    start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ifconfig $dev $vip netmask $mask broadcast $vip up
        echo "The RS Server is Ready!"
        ;;
    stop)
        ifconfig $dev down
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo "The RS Server is Canceled!"
        ;;
    *)
        echo "Usage: $(basename $0) start|stop"
        exit 1
        ;;
    esac
    
    chmod +x lvs_dr_rs.sh
    bash lvs_dr_rs.sh start
    
    // LB1, LB2启动KeepAlived服务,进行测试
    systemctl start keepalived
    

使用ipvsadm -Ln一声令下查看ipvs调解计谋,与KeepAlived的安排切合

图片 16

做客VIP1, VIP2(192.168.136.100, 192.168.136.200卡塔尔的Web服务,平常办事

图片 17

悬停传祺S1的Web服务,自动实行健检,全体调节至卡宴S2

图片 18

停下本田CR-VS2的Web服务,自动举办健检,调节至LB1的sorry server

图片 19

结束LB1的KeepAlived服务,自动切换至LB2

图片 20

终止TucsonS3的Web服务,自动实行健检,全体调解至昂CoraS4

图片 21

悬停瑞虎S4的Web服务,自动举行健检,调节至LB2的sorry server

图片 22

Global definitions

(六)Keepalived调用脚本举办能源监察

  • keepalived调用外部的拉扯脚本进行财富监察,并依靠监察的结果意况完成优先动态调解

  • vrrp_script:自定义能源监察脚本,vrrp实例依据脚本重临值,公共定义,可被三个实例调用,定义在vrrp实例之外

  • track_script:调用vrrp_script定义的脚本去监察和控制财富,定义在实例之内,调用事情发生从前定义的vrrp_script

    • 分两步:(1卡塔尔(قطر‎ 先定义一个本子;(2卡塔尔(英语:State of Qatar) 调用此脚本
      格式:
    // 定义脚本,定义在实例外
    vrrp_script <SCRIPT_NAME> {
        script ""     // 引号内为脚本命令
        interval INT
        weight -INT
    }
    // 调用脚本,定义在实例内
    track_script {
        SCRIPT_NAME_1
        SCRIPT_NAME_2
    }
    
  • 尝试6:完毕主/主模型的高可用Nginx反向代理

    • 试市价况:
      LB1/VS1:IP: 192.168.136.230,后端RS: RS1, RS2
      LB2/VS2:IP: 192.168.136.130,后端RS: RS3, RS4
      LB1 VIP:192.168.136.100
      LB2 VIP:192.168.136.200
      RS1:IP: 192.168.136.229
      RS2:IP: 192.168.136.129
      RS3:IP: 192.168.136.240
      RS4:IP: 192.168.136.250
      LB之间互为MASTE福睿斯与BACKUP的涉嫌
      MASTER:LB1,BACKUP:LB2
      MASTER:LB2,BACKUP:LB1
    // 配置LB1, LB2的KeepAlived设置
    vim /etc/keepalived/keepalived.conf
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from node1@localhost     // LB1上操作
       notification_email_from node2@localhost     // LB2上操作
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id node1                             // LB1上操作
       router_id node2                             // LB2上操作
       vrrp_mcast_group4 224.0.0.58
    }
    
    vrrp_script chk_nginx {
            script "killall -0 nginx && exit 0 || exit 1;"
            interval 1
            weight -20
            fall 3
            rise 3
    }
    vrrp_instance VI_1 {
        state MASTER                               // LB1上操作
        state BACKUP                               // LB2上操作
        interface ens37
        virtual_router_id 51
        priority 100                               // LB1上操作
        priority 90                                // LB2上操作
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass dd73f9d6
        }
        virtual_ipaddress {
            192.168.136.100/24
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    // 下面的脚本引用仅在LB1的配置文件出现
        track_script {
            chk_nginx
        }
    }
    vrrp_instance VI_2 {
        state BACKUP                               // LB1上操作
        state MASTER                               // LB2上操作
        interface ens37
        virtual_router_id 61
        priority 90                                // LB1上操作
        priority 100                               // LB2上操作
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass a56c19be
        }
        virtual_ipaddress {
            192.168.136.200/24
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_backup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    // 下面的脚本引用仅在LB2的配置文件出现
        track_script {
            chk_nginx
        }
    }
    
    // 配置LB1,LB2的nginx反向代理
    vim /etc/nginx/nginx.conf
    http {
        upstream websrvs1 {
            server 192.168.136.229:80 weight=2;
            server 192.168.136.129:80 weight=1;
        }
        upstream websrvs2 {
            server 192.168.136.240:80 weight=2;
            server 192.168.136.250:80 weight=1;
        }
        server {
            listen  192.168.136.100:80;
            location / {
                    proxy_pass http://websrvs1;
            }
        }
        server {
            listen  192.168.136.200:80;
            location / {
                    proxy_pass http://websrvs2;
            }
        }
    }
    nginx -t
    systemctl start nginx
    
    // 配置RS1, RS2, RS3, RS4的Web服务
    echo RS1 homepage > /var/www/html/index.html     // RS1上操作
    echo RS2 homepage > /var/www/html/index.html     // RS2上操作
    echo RS3 homepage > /var/www/html/index.html     // RS3上操作
    echo RS4 homepage > /var/www/html/index.html     // RS4上操作
    systemctl start httpd
    
    // LB1, LB2启动KeepAlived服务,进行测试
    systemctl start keepalived
    

    报到192.168.136.100和192.168.136.200的web服务,确实依据设置供给调治

    图片 23

    终止CRUISERS2的httpd服务,全体调解至凯雷德S1

    图片 24

    停下智跑S3的httpd服务,整体调节至安德拉S4

    图片 25

    关闭LB2的nginx反向代理服务,通过tcpdump -i ens37 -nn host 224.0.0.58翻看组播处境。多个红框依次表明:
    (1)未关门nginx前的组播状态
    (2)关闭nginx后,LB2的vrid 61权重减去20变作80,而LB1vrid 61的权重为90
    (3)由于LB1的权重高,VIP2的全部权被LB1接管

    图片 26

    关闭LB1的nginx反向代理服务,通过tcpdump -i ens37 -nn host 224.0.0.58查阅组播情形。八个红框依次表明:
    (1)未关门nginx前的组播状态
    (2)关闭nginx后,LB1的vrid 51权重减去20变作80,而LB2的vrid 51权重为90
    (3)由于LB2的权重高,VIP1的全体权被LB2接管

    图片 27

    是因为那时候五个nginx反向代理均关门,故访谈192.168.136.100和192.168.136.200的web服务整个诉讼失败

    图片 28

    开拓LB2的nginx反向代理服务,通过tcpdump -i ens37 -nn host 224.0.0.58查阅组播情况。四个红框依次表达:
    (1)未张开nginx前的组播状态
    (2)展开nginx后,LB2的vrid 61权重扩大20变作100,而LB1的vrid 61权重为90
    (3)由于LB2的权重高,VIP2的全体权被LB2接管

    图片 29

    那时VIP1和VIP2均由LB2上的nginx服务器举办反向代理,192.168.136.100和192.168.136.200的web服务整个回复

Static routes/addresses

(七)Keepalived同步组

  • LVS NAT模型VIP和DIP供给一同,要求同步组

  • 格式:

    vrrp_sync_group VG_1 {
      group {
          VI_1 # name of vrrp_instance(below)
          VI_2 # One for each moveable IP.
      }
    }
    vrrp_instance VI_1 {
      eth0
      vip
    }
    vrrp_instance VI_2 {
      eth1
      dip
    }
    

VRRPD CONFIGURATION

VRRP synchronization group(s):vrrp同步组

VGL450RP instance(s卡塔尔(英语:State of Qatar):即贰个vrrp虚构路由器

LVS CONFIGURATION

Virtual server group(s)

Virtual server(s):ipvs集群的vs和rs

版权声明:本文由bob体育app发布于操作系统,转载请注明出处:keepalive 之 配置介绍