溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

LVS負(fù)載均衡群集——keepalived+DR模式(實戰(zhàn)!

發(fā)布時間:2020-02-14 11:10:07 來源:網(wǎng)絡(luò) 閱讀:2148 作者:wx5d2c2d660c282 欄目:建站服務(wù)器

keepalived實現(xiàn)原理

keepalived采用vrrp熱備份協(xié)議,實現(xiàn)Linux服務(wù)器的多機熱備功能
vrrp,虛擬路由冗余協(xié)議,是針對路由器的一種備份解決方案

keepalivd案列講解

keepalived可實現(xiàn)多機熱備,每個熱備組有多臺服務(wù)器,最常用的就是雙機熱備
雙機熱備的故障切換是由虛擬IP地址的漂移來實現(xiàn),適用于各種應(yīng)用服務(wù)器

DR模式原理

LVS負(fù)載均衡群集——keepalived+DR模式(實戰(zhàn)!

實驗環(huán)境

CentOS7系統(tǒng):
                 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系統(tǒng): 
                  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、分別將五臺虛擬機的網(wǎng)絡(luò)模式改為僅主機模式(五臺操作相同)

LVS負(fù)載均衡群集——keepalived+DR模式(實戰(zhàn)!

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     #子網(wǎng)掩碼
GATEWAY=192.168.100.1     #網(wǎng)關(guān)
[root@localhost ~]# systemctl restart network     #重啟網(wǎng)絡(luò)服務(wù)
[root@localhost ~]# ifconfig      #查看網(wǎng)卡信息
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     #子網(wǎng)掩碼
GATEWAY=192.168.100.1     #網(wǎng)關(guān)
[root@localhost ~]# systemctl restart network     #重啟網(wǎng)絡(luò)服務(wù)
[root@localhost ~]# ifconfig       #查看網(wǎng)卡信息
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     #子網(wǎng)掩碼
GATEWAY=192.168.100.1     #網(wǎng)關(guān)
[root@localhost ~]# systemctl restart network     #重啟網(wǎng)絡(luò)服務(wù)
[root@localhost ~]# ifconfig       #查看網(wǎng)卡信息
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     #子網(wǎng)掩碼
GATEWAY=192.168.100.1     #網(wǎng)關(guān)
[root@localhost ~]# systemctl restart network     #重啟網(wǎng)絡(luò)服務(wù)
[root@localhost ~]# ifconfig       #查看網(wǎng)卡信息
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系統(tǒng)的IP地址,并關(guān)閉防火墻

LVS負(fù)載均衡群集——keepalived+DR模式(實戰(zhàn)!

LVS負(fù)載均衡群集——keepalived+DR模式(實戰(zhàn)!

DR1主服務(wù)器配置

1、配置路由轉(zhuǎn)發(fā)功能

[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1     #配置路由轉(zhuǎn)發(fā)功能
#proc響應(yīng)關(guān)閉重定向功能
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、配置虛擬網(wǎng)卡

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0    #復(fù)制虛擬網(wǎng)卡的配置文件
[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     #重啟網(wǎng)絡(luò)服務(wù)
[root@localhost network-scripts]# ifconfig      #查看網(wǎng)卡信息
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     #虛擬網(wǎng)卡配置成功
        ether 00:0c:29:95:9b:1b  txqueuelen 1000  (Ethernet)

3、創(chuàng)建腳本便于service管理

[root@localhost network-scripts]# cd /etc/init.d/
[root@localhost init.d]# vim dr.sh
#!/bin/bash
GW=192.168.100.1   #網(wǎng)關(guān)
VIP=192.168.100.10    #虛擬ip
RIP1=192.168.100.221   #真實web1服務(wù)器ip
RIP2=192.168.100.222   #真實web2服務(wù)器ip
case "$1" in
        start)
                /sbin/ipvsadm --save > /etc/sysconfig/ipvsadm    #保存配置
                systemctl start ipvsadm    #啟動服務(wù)
                /sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
                                #設(shè)置ens33:0地址,廣播地址,子網(wǎng)掩碼,并開啟
                /sbin/route add -host $VIP dev ens33:0   #添加路由網(wǎng)段信息
                /sbin/ipvsadm -A -t $VIP:80 -s rr   #指定虛擬服務(wù)訪問入口,指定輪詢算法
                /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g    #指定真實服務(wù)器web1,dr模式
                /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g    #指定真實服務(wù)器web2,dr模式
                echo "ipvsadm starting ---------------------[ok]"
                ;;
        stop)
                /sbin/ipvsadm -C    #清空緩存
                systemctl stop ipvsadm    #關(guān)閉服務(wù)
                ifconfig ens33:0 down    #關(guān)閉接口
                route del $VIP     #刪除路由信息
                echo "ipvsadm stoped--------------------[ok]"
                ;;
        status)
                if [ ! -e /var/lock/subsys/ipvsadm ];then    #判斷文件存在與否決定狀態(tài)
                        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     #給執(zhí)行權(quán)限
[root@localhost init.d]# service dr.sh start     #開啟服務(wù)
ipvsadm starting ---------------------[ok]
[root@localhost init.d]# systemctl stop firewalld.service     #關(guān)閉防火墻
[root@localhost init.d]# setenforce 0     #關(guān)閉增強型安全功能
[root@localhost init.d]# 

4、配置DR1服務(wù)器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   #指定名稱,備份服務(wù)器不同名稱
vrrp_instance VI_1 {
    state MASTER         #主服務(wù)器,備份服務(wù)器是BACKUP
    interface ens33
    virtual_router_id 10     #組號相同
    priority 100      #優(yōu)先級,主服務(wù)器優(yōu)先級應(yīng)大于備份服務(wù)器
    advert_int 1
    authentication {
        auth_type PASS    #驗證類型
        auth_pass 1111     #驗證密碼
    }
    virtual_ipaddress {
        192.168.100.10     #虛擬IP地址
    }
}
virtual_server 192.168.100.10 80 {     #虛擬服務(wù)器
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

   real_server 192.168.100.221 80 {     #真實服務(wù)器
        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 {     #真實服務(wù)器
        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     #開啟服務(wù)
[root@localhost keepalived]# 

DR2從服務(wù)器配置

1、配置路由轉(zhuǎn)發(fā)功能

[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1     #配置路由轉(zhuǎn)發(fā)功能
#proc響應(yīng)關(guān)閉重定向功能
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、配置虛擬網(wǎng)卡

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0    #復(fù)制虛擬網(wǎng)卡的配置文件
[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     #重啟網(wǎng)絡(luò)服務(wù)
[root@localhost network-scripts]# ifconfig      #查看網(wǎng)卡信息
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     #虛擬網(wǎng)卡配置成功
        ether 00:0c:29:27:1c:3f  txqueuelen 1000  (Ethernet)

3、創(chuàng)建腳本便于service管理

[root@localhost network-scripts]# cd /etc/init.d/
[root@localhost init.d]# vim dr.sh
#!/bin/bash
GW=192.168.100.1   #網(wǎng)關(guān)
VIP=192.168.100.10    #虛擬ip
RIP1=192.168.100.221   #真實web1服務(wù)器ip
RIP2=192.168.100.222   #真實web2服務(wù)器ip
case "$1" in
        start)
                /sbin/ipvsadm --save > /etc/sysconfig/ipvsadm    #保存配置
                systemctl start ipvsadm    #啟動服務(wù)
                /sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
                                #設(shè)置ens33:0地址,廣播地址,子網(wǎng)掩碼,并開啟
                /sbin/route add -host $VIP dev ens33:0   #添加路由網(wǎng)段信息
                /sbin/ipvsadm -A -t $VIP:80 -s rr   #指定虛擬服務(wù)訪問入口,指定輪詢算法
                /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g    #指定真實服務(wù)器web1,dr模式
                /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g    #指定真實服務(wù)器web2,dr模式
                echo "ipvsadm starting ---------------------[ok]"
                ;;
        stop)
                /sbin/ipvsadm -C    #清空緩存
                systemctl stop ipvsadm    #關(guān)閉服務(wù)
                ifconfig ens33:0 down    #關(guān)閉接口
                route del $VIP     #刪除路由信息
                echo "ipvsadm stoped--------------------[ok]"
                ;;
        status)
                if [ ! -e /var/lock/subsys/ipvsadm ];then    #判斷文件存在與否決定狀態(tài)
                        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     #給執(zhí)行權(quán)限
[root@localhost init.d]# service dr.sh start     #開啟服務(wù)
ipvsadm starting ---------------------[ok]
[root@localhost init.d]# systemctl stop firewalld.service     #關(guān)閉防火墻
[root@localhost init.d]# setenforce 0     #關(guān)閉增強型安全功能
[root@localhost init.d]# 

4、配置DR1服務(wù)器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   #指定名稱,備份服務(wù)器不同名稱
vrrp_instance VI_1 {
    state BACKUP         #備份服務(wù)器BACKUP,主服務(wù)器是MASTER
    interface ens33
    virtual_router_id 10     #組號相同
    priority 90      #優(yōu)先級,備份服務(wù)器應(yīng)小于主服務(wù)器優(yōu)先級
    advert_int 1
    authentication {
        auth_type PASS    #驗證類型
        auth_pass 1111     #驗證密碼
    }
    virtual_ipaddress {
        192.168.100.10     #虛擬IP地址
    }
}
virtual_server 192.168.100.10 80 {     #虛擬服務(wù)器
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

   real_server 192.168.100.221 80 {     #真實服務(wù)器
        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 {     #真實服務(wù)器
        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     #開啟服務(wù)
[root@localhost keepalived]# 

配置web1服務(wù)器

1、開啟并配置httpd服務(wù)

[root@localhost ~]# systemctl stop firewalld.service    #關(guān)閉防火墻
[root@localhost ~]# setenforce 0   #關(guān)閉增強型安全功能
[root@localhost ~]# systemctl start httpd.service      #開啟httpd服務(wù)
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is abc web" > index.html    #創(chuàng)建網(wǎng)站頁面
[root@localhost html]# 

2、修改回環(huán)網(wǎng)卡的虛擬網(wǎng)卡

[root@localhost html]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0    #復(fù)制回環(huán)網(wǎng)卡配置文件
[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     #添加執(zhí)行權(quán)限
[root@localhost init.d]# service web.sh start       #開啟服務(wù)
RealServer Start OK 
[root@localhost init.d]# ifup lo:0    #開啟網(wǎng)卡
[root@localhost init.d]#ifconfig     #查看網(wǎng)卡信息
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服務(wù)器

1、開啟并配置httpd服務(wù)

[root@localhost ~]# systemctl stop firewalld.service    #關(guān)閉防火墻
[root@localhost ~]# setenforce 0   #關(guān)閉增強型安全功能
[root@localhost ~]# systemctl start httpd.service      #開啟httpd服務(wù)
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is xyz web" > index.html    #創(chuàng)建網(wǎng)站頁面
[root@localhost html]# 

2、修改回環(huán)網(wǎng)卡的虛擬網(wǎng)卡

[root@localhost html]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0    #復(fù)制回環(huán)網(wǎng)卡配置文件
[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     #添加執(zhí)行權(quán)限
[root@localhost init.d]# service web.sh start       #開啟服務(wù)
RealServer Start OK 
[root@localhost init.d]# ifup lo:0    #開啟網(wǎng)卡
[root@localhost init.d]#ifconfig     #查看網(wǎng)卡信息
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

LVS負(fù)載均衡群集——keepalived+DR模式(實戰(zhàn)!

再次刷新頁面,可以測試輪詢機制

LVS負(fù)載均衡群集——keepalived+DR模式(實戰(zhàn)!

2、模擬DR1主服務(wù)器故障,然后再次用win10測試主機訪問虛擬IP地址:192.168.100.10

[root@localhost keepalived]# ifdown ens33:0
#在DR1主服務(wù)器上,關(guān)閉虛擬網(wǎng)卡,模擬故障

LVS負(fù)載均衡群集——keepalived+DR模式(實戰(zhàn)!

再次刷新頁面,可以測試輪詢機制

LVS負(fù)載均衡群集——keepalived+DR模式(實戰(zhàn)!

測試成功?。?!

向AI問一下細(xì)節(jié)
AI