LVS负载均衡群集——keepalived+DR模式(实战!)

Posted

tags:

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

keepalived实现原理

keepalived采用vrrp热备份协议,实现Linux服务器的多机热备功能
vrrp,虚拟路由冗余协议,是针对路由器的一种备份解决方案

keepalivd案列讲解

keepalived可实现多机热备,每个热备组有多台服务器,最常用的就是双机热备
双机热备的故障切换是由虚拟IP地址的漂移来实现,适用于各种应用服务器

DR模式原理

技术图片

实验环境

CentOS7系统:
                 DR1 主: 192.168.100.2
                 DR2 备: 192.168.100.20
                 虚拟IP:192.168.100.10
                 web1:192.168.100.221
                 web2:192.168.100.222
win10系统: 
                  client:192.168.100.100

1、分别在四台Linux虚拟机上安装实验所需软件包

DR1主:
[root@localhost ~]# yum install ipvsadm keepalived -y

DR2从:
[root@localhost ~]# yum install ipvsadm keepalived -y

web1:
[root@localhost ~]# yum install httpd -y

web2:
[root@localhost ~]# yum install httpd -y

2、分别将五台虚拟机的网络模式改为仅主机模式(五台操作相同)

技术图片

3、分别修改四台Linux虚拟机的IP地址

DR1主:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static     #dhcp改为static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=3ceed540-b04c-48d6-a4f7-79951f09ea1d
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.201     #IP地址
NETMASK=255.255.255.0     #子网掩码
GATEWAY=192.168.100.1     #网关
[root@localhost ~]# systemctl restart network     #重启网络服务
[root@localhost ~]# ifconfig      #查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.201  netmask 255.255.255.0  broadcast 192.168.100.255     #IP地址配置成功
        inet6 fe80::3e1d:31ba:f66a:6f80  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:95:9b:1b  txqueuelen 1000  (Ethernet)
        RX packets 10373  bytes 13584878 (12.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4285  bytes 277473 (270.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
DR2从:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static     #dhcp改为static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=3ceed540-b04c-48d6-a4f7-79951f09ea1d
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.202     #IP地址
NETMASK=255.255.255.0     #子网掩码
GATEWAY=192.168.100.1     #网关
[root@localhost ~]# systemctl restart network     #重启网络服务
[root@localhost ~]# ifconfig       #查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.202  netmask 255.255.255.0  broadcast 192.168.100.255     #IP地址配置成功
        inet6 fe80::3e1d:31ba:f66a:6f80  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::2558:848e:ba08:4183  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:27:1c:3f  txqueuelen 1000  (Ethernet)
        RX packets 10470  bytes 13593710 (12.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4240  bytes 273794 (267.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
web1:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static     #dhcp改为static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=8ecd53ce-afdb-46f8-b7ff-b2f428a3bc8f
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.221     #IP地址
NETMASK=255.255.255.0     #子网掩码
GATEWAY=192.168.100.1     #网关
[root@localhost ~]# systemctl restart network     #重启网络服务
[root@localhost ~]# ifconfig       #查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.221  netmask 255.255.255.0  broadcast 192.168.100.255     #IP地址配置成功
        inet6 fe80::c776:9d00:618:88f2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:eb:34:07  txqueuelen 1000  (Ethernet)
        RX packets 11508  bytes 14937643 (14.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3932  bytes 254623 (248.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
web2:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static     #dhcp改为static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=09073596-a7cf-404c-a098-28c3ff58c44b
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.222     #IP地址
NETMASK=255.255.255.0     #子网掩码
GATEWAY=192.168.100.1     #网关
[root@localhost ~]# systemctl restart network     #重启网络服务
[root@localhost ~]# ifconfig       #查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.222  netmask 255.255.255.0  broadcast 192.168.100.255     #IP地址配置成功
        inet6 fe80::5134:22f5:842b:5201  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:0d:f1:75  txqueuelen 1000  (Ethernet)
        RX packets 11425  bytes 14931323 (14.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4454  bytes 286016 (279.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

4、修改win10系统的IP地址,并关闭防火墙

技术图片

技术图片

DR1主服务器配置

1、配置路由转发功能

[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1     #配置路由转发功能
#proc响应关闭重定向功能
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.send_redirects=0
net.ipv4.conf.ens33.send_redirects=0
[root@localhost ~]# sysctl -p     #加载生效
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# 

2、配置虚拟网卡

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0    #复制虚拟网卡的配置文件
[root@localhost network-scripts]# vim ifcfg-ens33:0
#添加配置文件信息,原有的信息删除
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
[root@localhost network-scripts]# systemctl restart network     #重启网络服务
[root@localhost network-scripts]# ifconfig      #查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.201  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::3e1d:31ba:f66a:6f80  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:95:9b:1b  txqueuelen 1000  (Ethernet)
        RX packets 11455  bytes 13700437 (13.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4713  bytes 330899 (323.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.10  netmask 255.255.255.0  broadcast 192.168.100.255     #虚拟网卡配置成功
        ether 00:0c:29:95:9b:1b  txqueuelen 1000  (Ethernet)

3、创建脚本便于service管理

[root@localhost network-scripts]# cd /etc/init.d/
[root@localhost init.d]# vim dr.sh
#!/bin/bash
GW=192.168.100.1   #网关
VIP=192.168.100.10    #虚拟ip
RIP1=192.168.100.221   #真实web1服务器ip
RIP2=192.168.100.222   #真实web2服务器ip
case "$1" in
        start)
                /sbin/ipvsadm --save > /etc/sysconfig/ipvsadm    #保存配置
                systemctl start ipvsadm    #启动服务
                /sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
                                #设置ens33:0地址,广播地址,子网掩码,并开启
                /sbin/route add -host $VIP dev ens33:0   #添加路由网段信息
                /sbin/ipvsadm -A -t $VIP:80 -s rr   #指定虚拟服务访问入口,指定轮询算法
                /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g    #指定真实服务器web1,dr模式
                /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g    #指定真实服务器web2,dr模式
                echo "ipvsadm starting ---------------------[ok]"
                ;;
        stop)
                /sbin/ipvsadm -C    #清空缓存
                systemctl stop ipvsadm    #关闭服务
                ifconfig ens33:0 down    #关闭接口
                route del $VIP     #删除路由信息
                echo "ipvsadm stoped--------------------[ok]"
                ;;
        status)
                if [ ! -e /var/lock/subsys/ipvsadm ];then    #判断文件存在与否决定状态
                        echo "ipvsadm stoped--------------"
                        exit 1
                else
                        echo "ipvsadm Runing-----------[ok]"
                fi
                ;;
        *)
                echo "Usage: $0 {start|stop|status}"
                exit 1
        esac
        exit 0
[root@localhost init.d]# chmod +x dr.sh     #给执行权限
[root@localhost init.d]# service dr.sh start     #开启服务
ipvsadm starting ---------------------[ok]
[root@localhost init.d]# systemctl stop firewalld.service     #关闭防火墙
[root@localhost init.d]# setenforce 0     #关闭增强型安全功能
[root@localhost init.d]# 

4、配置DR1服务器keepalived部署

[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# ls
keepalived.conf
[root@localhost keepalived]# vim keepalived.conf 
smtp_server 127.0.0.1           #指向本地
router_id LVS_01   #指定名称,备份服务器不同名称
vrrp_instance VI_1 {
    state MASTER         #主服务器,备份服务器是BACKUP
    interface ens33
    virtual_router_id 10     #组号相同
    priority 100      #优先级,主服务器优先级应大于备份服务器
    advert_int 1
    authentication {
        auth_type PASS    #验证类型
        auth_pass 1111     #验证密码
    }
    virtual_ipaddress {
        192.168.100.10     #虚拟IP地址
    }
}
virtual_server 192.168.100.10 80 {     #虚拟服务器
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

   real_server 192.168.100.221 80 {     #真实服务器
        weight 1
        TCP_CHECK {
            connect_port 80        #连接端口
            connect_timeout 3    #连接超时
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.100.222 80 {     #真实服务器
        weight 1
        TCP_CHECK {
            connect_port 80        #连接端口
            connect_timeout 3    #连接超时
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@localhost keepalived]# systemctl start keepalived.service     #开启服务
[root@localhost keepalived]# 

DR2从服务器配置

1、配置路由转发功能

[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1     #配置路由转发功能
#proc响应关闭重定向功能
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.send_redirects=0
net.ipv4.conf.ens33.send_redirects=0
[root@localhost ~]# sysctl -p     #加载生效
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# 

2、配置虚拟网卡

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0    #复制虚拟网卡的配置文件
[root@localhost network-scripts]# vim ifcfg-ens33:0
#添加配置文件信息,原有的信息删除
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
[root@localhost network-scripts]# systemctl restart network     #重启网络服务
[root@localhost network-scripts]# ifconfig      #查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.202  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::3e1d:31ba:f66a:6f80  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::2558:848e:ba08:4183  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:27:1c:3f  txqueuelen 1000  (Ethernet)
        RX packets 15720  bytes 14706442 (14.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6289  bytes 1113172 (1.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.10  netmask 255.255.255.0  broadcast 192.168.100.255     #虚拟网卡配置成功
        ether 00:0c:29:27:1c:3f  txqueuelen 1000  (Ethernet)

3、创建脚本便于service管理

[root@localhost network-scripts]# cd /etc/init.d/
[root@localhost init.d]# vim dr.sh
#!/bin/bash
GW=192.168.100.1   #网关
VIP=192.168.100.10    #虚拟ip
RIP1=192.168.100.221   #真实web1服务器ip
RIP2=192.168.100.222   #真实web2服务器ip
case "$1" in
        start)
                /sbin/ipvsadm --save > /etc/sysconfig/ipvsadm    #保存配置
                systemctl start ipvsadm    #启动服务
                /sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
                                #设置ens33:0地址,广播地址,子网掩码,并开启
                /sbin/route add -host $VIP dev ens33:0   #添加路由网段信息
                /sbin/ipvsadm -A -t $VIP:80 -s rr   #指定虚拟服务访问入口,指定轮询算法
                /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g    #指定真实服务器web1,dr模式
                /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g    #指定真实服务器web2,dr模式
                echo "ipvsadm starting ---------------------[ok]"
                ;;
        stop)
                /sbin/ipvsadm -C    #清空缓存
                systemctl stop ipvsadm    #关闭服务
                ifconfig ens33:0 down    #关闭接口
                route del $VIP     #删除路由信息
                echo "ipvsadm stoped--------------------[ok]"
                ;;
        status)
                if [ ! -e /var/lock/subsys/ipvsadm ];then    #判断文件存在与否决定状态
                        echo "ipvsadm stoped--------------"
                        exit 1
                else
                        echo "ipvsadm Runing-----------[ok]"
                fi
                ;;
        *)
                echo "Usage: $0 {start|stop|status}"
                exit 1
        esac
        exit 0
[root@localhost init.d]# chmod +x dr.sh     #给执行权限
[root@localhost init.d]# service dr.sh start     #开启服务
ipvsadm starting ---------------------[ok]
[root@localhost init.d]# systemctl stop firewalld.service     #关闭防火墙
[root@localhost init.d]# setenforce 0     #关闭增强型安全功能
[root@localhost init.d]# 

4、配置DR1服务器keepalived部署

[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# ls
keepalived.conf
[root@localhost keepalived]# vim keepalived.conf 
smtp_server 127.0.0.1           #指向本地
router_id LVS_02   #指定名称,备份服务器不同名称
vrrp_instance VI_1 {
    state BACKUP         #备份服务器BACKUP,主服务器是MASTER
    interface ens33
    virtual_router_id 10     #组号相同
    priority 90      #优先级,备份服务器应小于主服务器优先级
    advert_int 1
    authentication {
        auth_type PASS    #验证类型
        auth_pass 1111     #验证密码
    }
    virtual_ipaddress {
        192.168.100.10     #虚拟IP地址
    }
}
virtual_server 192.168.100.10 80 {     #虚拟服务器
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

   real_server 192.168.100.221 80 {     #真实服务器
        weight 1
        TCP_CHECK {
            connect_port 80        #连接端口
            connect_timeout 3    #连接超时
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.100.222 80 {     #真实服务器
        weight 1
        TCP_CHECK {
            connect_port 80        #连接端口
            connect_timeout 3    #连接超时
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@localhost keepalived]# systemctl start keepalived.service     #开启服务
[root@localhost keepalived]# 

配置web1服务器

1、开启并配置httpd服务

[root@localhost ~]# systemctl stop firewalld.service    #关闭防火墙
[root@localhost ~]# setenforce 0   #关闭增强型安全功能
[root@localhost ~]# systemctl start httpd.service      #开启httpd服务
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is abc web" > index.html    #创建网站页面
[root@localhost html]# 

2、修改回环网卡的虚拟网卡

[root@localhost html]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0    #复制回环网卡配置文件
[root@localhost network-scripts]# vim ifcfg-lo:0
#添加配置文件信息,原有的信息删除
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.0
ONBOOT=yes

3、添加脚本便于service管理

[root@localhost network-scripts]# cd /etc/init.d/
[root@localhost init.d]# vim web.sh
#!/bin/bash
VIP=192.168.100.10
        case "$1" in
        start)
                ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
                /sbin/route add -host $VIP dev 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/lo/arp_ignore
        echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
        sysctl -p > /dev/null 2>&1
        echo "RealServer Start OK "
        ;;
        stop)
                ifconfig lo:0 down
                route del $VIP /dev/null 2>&1
                echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
                echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
                echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
                echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
    echo "RealServer Stoped"
                ;;
        *)
                echo "Usage: $0 {start|stop}"
                exit 1
        esac
        exit 0
[root@localhost init.d]# chmod +x web.sh     #添加执行权限
[root@localhost init.d]# service web.sh start       #开启服务
RealServer Start OK 
[root@localhost init.d]# ifup lo:0    #开启网卡
[root@localhost init.d]#ifconfig     #查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.221  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::c776:9d00:618:88f2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:eb:34:07  txqueuelen 1000  (Ethernet)
        RX packets 13293  bytes 15122368 (14.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4692  bytes 360484 (352.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 192.168.100.10  netmask 255.255.255.255
        loop  txqueuelen 1  (Local Loopback)

配置web2服务器

1、开启并配置httpd服务

[root@localhost ~]# systemctl stop firewalld.service    #关闭防火墙
[root@localhost ~]# setenforce 0   #关闭增强型安全功能
[root@localhost ~]# systemctl start httpd.service      #开启httpd服务
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is xyz web" > index.html    #创建网站页面
[root@localhost html]# 

2、修改回环网卡的虚拟网卡

[root@localhost html]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0    #复制回环网卡配置文件
[root@localhost network-scripts]# vim ifcfg-lo:0
#添加配置文件信息,原有的信息删除
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.0
ONBOOT=yes

3、添加脚本便于service管理

[root@localhost network-scripts]# cd /etc/init.d/
[root@localhost init.d]# vim web.sh
#!/bin/bash
VIP=192.168.100.10
        case "$1" in
        start)
                ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
                /sbin/route add -host $VIP dev 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/lo/arp_ignore
        echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
        sysctl -p > /dev/null 2>&1
        echo "RealServer Start OK "
        ;;
        stop)
                ifconfig lo:0 down
                route del $VIP /dev/null 2>&1
                echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
                echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
                echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
                echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
    echo "RealServer Stoped"
                ;;
        *)
                echo "Usage: $0 {start|stop}"
                exit 1
        esac
        exit 0
[root@localhost init.d]# chmod +x web.sh     #添加执行权限
[root@localhost init.d]# service web.sh start       #开启服务
RealServer Start OK 
[root@localhost init.d]# ifup lo:0    #开启网卡
[root@localhost init.d]#ifconfig     #查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.222  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::5134:22f5:842b:5201  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:0d:f1:75  txqueuelen 1000  (Ethernet)
        RX packets 16771  bytes 15510870 (14.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7516  bytes 783039 (764.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 192.168.100.10  netmask 255.255.255.255
        loop  txqueuelen 1  (Local Loopback)

测试群集是否搭建成功

1、用win10测试主机访问虚拟IP地址:192.168.100.10

技术图片

再次刷新页面,可以测试轮询机制

技术图片

2、模拟DR1主服务器故障,然后再次用win10测试主机访问虚拟IP地址:192.168.100.10

[root@localhost keepalived]# ifdown ens33:0
#在DR1主服务器上,关闭虚拟网卡,模拟故障

技术图片

再次刷新页面,可以测试轮询机制

技术图片

测试成功!!!

以上是关于LVS负载均衡群集——keepalived+DR模式(实战!)的主要内容,如果未能解决你的问题,请参考以下文章

LVS负载均衡群集再相遇之DR模式+Keepalived

LVS负载均衡群集再相遇之DR模式+Keepalived

LVS负载均衡群集之DR模式+keepalived群集机构(理论+实践篇)

LVS负载均衡群集——keepalived+DR模式(实战!)

DR+keepalived实现web群集的负载均衡和高可用性

LVS负载均衡群集架构:DR模式群集部署