keepalived + LVS DR 构建高可用负载均衡

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived + LVS DR 构建高可用负载均衡相关的知识,希望对你有一定的参考价值。

本文参考博客: https://blog.csdn.net/Ki8Qzvka6Gz4n450m/article/details/79119665
原博客很详细并正确,重新记录只为方便查找和搭建

一、环境简述
real server1: 192.168.50.207 -----------httpd服务器
real server2: 192.168.50.235 -----------httpd服务器
director server1:192.168.50.232--------lvs dr服务器+ keepalived服务器
director server2:192.168.50.231 -----lvs dr服务器+ keepalived服务器
测试服务器: 192.168.50.208

二、搭建
1 real server1和real server2服务器设置:
分别执行以下脚本
[root@localhost lvs_dir]#cat lvs_dr_rs.sh
#!/bin/bash
vip=192.168.50.252
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
注: httpd的安装和设置省略,参考前面的博客

  1. director server1服务器设置:
    [root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived

global_defs {
notification_email {br/>root@localhost.localdomain
}
notification_email_from keepalived@232
smtp_server 192.168.50.232
smtp_connect_timeout 30
router_id 232
! vrrp_skip_check_adv_addr
! vrrp_strict
! vrrp_garp_interval 0
! vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 111
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.50.252
}
}

virtual_server 192.168.50.252 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP

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

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

}

[root@localhost keepalived]# echo 1 > /proc/sys/net/ipv4/ip_forward

  1. director server2的搭建类似
    只需要将keepalived的配置文件中的内容稍作修改
    state MASTER -> state BACKUP
    priority 100 -> priority 90

  2. 启动keepalved服务(director server1和director server2)
    [root@localhost keepalived]# systemctl start keepalived

三、测试
1 测试负载均衡(208上):
[root@localhost ~]# curl 192.168.50.252:80
235
[root@localhost ~]# curl 192.168.50.252:80
207
[root@localhost ~]# curl 192.168.50.252:80
235
[root@localhost ~]# curl 192.168.50.252:80
207
2.测试real server的单点故障问题:
关掉235上的httpd服务,然后在208上测试:
[root@localhost ~]# curl 192.168.50.252:80
207
[root@localhost ~]# curl 192.168.50.252:80
207
3.测试director的高可用性
关掉231服务器的keepalived,或者关掉网卡等,再在208上测试
[root@localhost ~]# curl 192.168.50.252:80
235
[root@localhost ~]# curl 192.168.50.252:80
207
[root@localhost ~]# curl 192.168.50.252:80
235
[root@localhost ~]# curl 192.168.50.252:80
207

以上是关于keepalived + LVS DR 构建高可用负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

搭建LVS(DR模式)+Keepalived高可用群集,可跟做!!!

[Linux]-构建LVS-DR模式+Keepalived高可用群集

LVS+Keepalived构建高可用群集

keepalived + LVS DR 构建高可用负载均衡

Keepalived实现LVS-DR集群高可用

lvs集群高可用之DR+keepalived