LVS+Keepalived双主模型的实现

Posted

tags:

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

拓扑结构:

Client LVS1 LVS2 RS1 RS2
192.168.2.1 DIP 192.168.2.128 VIP 192.168.2.198 DIP 192.168.2.129 VIP 192.168.2.199 192.168.2.130 192.168.2.131

实现双主模型我们需要配置两个虚拟路由器组,也就是每台主机需要配置两段 vrrp_instance,每个虚拟接口配置虚拟IP,LVS1与LVS2的同一组virtual_router_id内互为主备,这里如果不明白可以查看VRRP的实现原理或者稍后看配置信息,RS1与RS2在双主模式下需要配置两组路由,当收到来自VIP1的请求交给lo:0的网卡处理,收到来自VIP2的请求交给lo:1的网卡处理

上配置:

###########################           LVS1配置          ######################### 

yum install nginx -y
echo "对不起,服务器正在维护..‘ > /usr/share/nginx/html/index.html
systemctl start nginx

! Configuration File for keepalived

global_defs {           ##对于邮件报警,先简单配置为本地的邮箱,而且这里的邮件报警也比较鸡肋,后面我们借助keepalive调用脚本的能力再开发报警或者借助zabbix这种专业级程序
   notification_email {
        [email protected]       
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1                   ##设置为主机名,唯一
   vrrp_mcast_group4 224.0.0.112     ##组播地址
}

###  配置虚拟IP配置区域 

vrrp_instance VI_1 {
    state MASTER                    #状态分为MASTER | BACKUP
    interface eno16777736           ##浮动ip绑定在哪一个物理接口
    virtual_router_id 31            ##虚拟路由器id,和另一台设置为一致
    priority 100                    ##优先级
    advert_int 1                    ##心跳检测频率,默认1s
#    nopreempt                       ##非抢占模式
    authentication {
        auth_type PASS
        auth_pass f1GDsVH6      ##VRRP组播,和同一组虚拟vip保持一致
    }
    virtual_ipaddress {
        192.168.2.198/24 dev eno16777736 label eno16777736:1    ##设置vip地址
    }

    notify_master "/etc/keepalived/scripts/notify.sh master"      ##状态变更为master时执行脚本
    notify_backup "/etc/keepalived/scripts/notify.sh backup"     ##状态变更为backup时执行脚本
    notify_fault "/etc/keepalived/scripts/notify.sh fault"     ##状态发生故障时执行脚本
}

vrrp_instance VI_2 {
    state BACKUP
    interface eno16777736
    virtual_router_id 32
    priority 98
    advert_int 1
#    nopreempt
    authentication {
        auth_type PASS
        auth_pass f1GDsV78
    }
    virtual_ipaddress {
        192.168.2.199/24 dev eno16777736 label eno16777736:2
    }

    notify_master "/etc/keepalived/scripts/notify.sh master"      ##状态变更为master时执行脚本
    notify_backup "/etc/keepalived/scripts/notify.sh backup"     ##状态变更为backup时执行脚本
    notify_fault "/etc/keepalived/scripts/notify.sh fault"     ##状态发生故障时执行脚本

}

###  LVS规则配置区域
##由于是双主模型,我们需要配置两组VIP的集群,当LVS2宕机时,LVS2虚拟IP漂移到本机,我们需要LVS1上有192.168.2.199的集群配置,反之LVS2也要配置192.168.2.198的集群配置

virtual_server 192.168.2.198 80 {
    delay_loop 1
    lb_algo wlc
    lb_kind DR
#   persistence_timeout 300
    protocol TCP
    sorry_server 127.0.0.1 80
  real_server 192.168.2.130 80{
    weight 1
    TCP_CHECK {
            connect_port 80
        connect_timeout 1
            nb_get_retry 2
        delay_before_retry 1
    }
  }

  real_server 192.168.2.131 80{
    weight 1
    TCP_CHECK {
            connect_port 80
        connect_timeout 1
            nb_get_retry 2
        delay_before_retry 1
    }
  }

}

virtual_server 192.168.2.199 80 {
    delay_loop 1
    lb_algo wlc
    lb_kind DR
#   persistence_timeout 300
    protocol TCP
    sorry_server 127.0.0.1 80
  real_server 192.168.2.130 80{
    weight 1
    TCP_CHECK {
            connect_port 80
        connect_timeout 1
            nb_get_retry 2
        delay_before_retry 1
    }
  }

  real_server 192.168.2.131 80{
    weight 1
    TCP_CHECK {
            connect_port 80
        connect_timeout 1
            nb_get_retry 2
        delay_before_retry 1
    }
  }
}

###########################           LVS2配置          #########################
yum install nginx -y
echo "对不起,服务器正在维护..‘ > /usr/share/nginx/html/index.html
systemctl start nginx

! Configuration File for keepalived

global_defs {           ##对于邮件报警,先简单配置为本地的邮箱,而且这里的邮件报警也比较鸡肋,后面我们借助keepalive调用脚本的能力再开发报警或者借助zabbix这种专业级程序
   notification_email {
        [email protected]       
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node2                   ##设置为主机名,唯一
   vrrp_mcast_group4 224.0.0.112     ##组播地址
}

vrrp_instance VI_1 {
    state BACKUP                    #状态分为MASTER | BACKUP
    interface eno16777736           ##浮动ip绑定在哪一个物理接口
    virtual_router_id 31            ##虚拟路由器id,和另一台设置为一致
    priority 98                     ##优先级
    advert_int 1                    ##心跳检测频率,默认1s
#    nopreempt                       ##非抢占模式
    authentication {
        auth_type PASS
        auth_pass f1GDsVH6      ##VRRP组播,和同一组虚拟vip保持一致
    }
    virtual_ipaddress {
        192.168.2.198/24 dev eno16777736 label eno16777736:1    ##设置vip地址
    }

    notify_master "/etc/keepalived/scripts/notify.sh master"      ##状态变更为master时执行脚本
    notify_backup "/etc/keepalived/scripts/notify.sh backup"     ##状态变更为backup时执行脚本
    notify_fault "/etc/keepalived/scripts/notify.sh fault"     ##状态发生故障时执行脚本
}

vrrp_instance VI_2 {
    state MASTER
    interface eno16777736
    virtual_router_id 32
    priority 100
    advert_int 1
#    nopreempt
    authentication {
        auth_type PASS
        auth_pass f1GDsV78
    }
    virtual_ipaddress {
        192.168.2.199/24 dev eno16777736 label eno16777736:2
    }

    notify_master "/etc/keepalived/scripts/notify.sh master"      ##状态变更为master时执行脚本
    notify_backup "/etc/keepalived/scripts/notify.sh backup"     ##状态变更为backup时执行脚本
    notify_fault "/etc/keepalived/scripts/notify.sh fault"     ##状态发生故障时执行脚本
}

virtual_server 192.168.2.198 80 {
    delay_loop 1
    lb_algo wlc
    lb_kind DR
#   persistence_timeout 300
    protocol TCP
    sorry_server 127.0.0.1 80
  real_server 192.168.2.130 80{
    weight 1
    TCP_CHECK {
            connect_port 80
        connect_timeout 1
            nb_get_retry 2
        delay_before_retry 1
    }
  }

  real_server 192.168.2.131 80{
    weight 1
    TCP_CHECK {
            connect_port 80
        connect_timeout 1
            nb_get_retry 2
        delay_before_retry 1
    }
  }

}

virtual_server 192.168.2.199 80 {
    delay_loop 1
    lb_algo wlc
    lb_kind DR
#   persistence_timeout 300
    protocol TCP
    sorry_server 127.0.0.1 80
  real_server 192.168.2.130 80{
    weight 1
    TCP_CHECK {
            connect_port 80
        connect_timeout 1
            nb_get_retry 2
        delay_before_retry 1
    }
  }

  real_server 192.168.2.131 80{
    weight 1
    TCP_CHECK {
            connect_port 80
        connect_timeout 1
            nb_get_retry 2
        delay_before_retry 1
    }
  }
}

########################   RS1配置   ########################
yum install nginx -y
echo "192.168.2.130‘ > /usr/share/nginx/html/index.html
systemctl start nginx
## 执行脚本 set_lvs_rs.sh
bash set_lvs_rs.sh start

########################   RS2配置   ########################
yum install nginx -y
echo "192.168.2.131‘ > /usr/share/nginx/html/index.html
systemctl start nginx
## 执行脚本 set_lvs_rs.sh
bash set_lvs_rs.sh start

到此LVS+keepalived的双主模型已经完成,通过测试我们可以得到:
1、DNS轮巡,当LVS1与LVS2无异常时同时正常工作。
2、当LVS1或任意其中一台宕机时,浮动IP飘逸至另一台主机,两个VIP都仍然正常工作
3、当NGINX任意一台TCP 80端口4层检测不正常,keepalived自动将其从规则删除,反之自动添加
4、当NGINX同时全部宕机,keepalived临时提供sorry server
5、当vip发生变更keepalived将自动发送邮件通知管理员

set_lvs_rs.sh 脚本内容:

#!/bin/bash
#
vip1=‘192.168.2.198‘
vip2=‘192.168.2.199‘
mask=‘255.255.255.255‘

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 lo:0 $vip1 netmask $mask broadcast $vip1 up
    ifconfig lo:1 $vip2 netmask $mask broadcast $vip2 up
    route add -host $vip1 dev lo:0
    route add -host $vip2 dev lo:1
    ;;
stop)
    ifconfig lo:0 down
    ifconfig lo:1 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 "Usage $(basename $0) start|stop"
    exit 1
    ;;
esac

notify.sh脚本内容:

[[email protected] scripts]# cat notify.sh 
#!/bin/bash
#
contact=‘[email protected]‘

notify() {
        local mailsubject="$(hostname) to be $1, vip floating"
        local 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

以上是关于LVS+Keepalived双主模型的实现的主要内容,如果未能解决你的问题,请参考以下文章

基于Keepalived实现LVS双主高可用集群

LVS基础及LVS+Keepalived实现双主高可用负载均衡

keepalived高可用nginx/htttpd,双主模型

LVS/NAT双主 + keepalived负载均衡实现

LVS/NAT双主 + keepalived负载均衡实现

mysql主从之LVS+keepalived+双主MySQL 负载均衡