青蛙學Linux—Keepalived+LVS構建負載均衡集羣(DR模式)

一、實驗環境

  • 主機A:操做系統CentOS 7.6.1810,IP地址192.168.0.86,Keepalived版本2.0.2,ipvsadm版本1.27
  • 主機B:操做系統CentOS 7.6.1810,IP地址192.168.0.88,運行Apache 2.4.37提供Web服務
  • 主機C:操做系統CentOS 7.6.1810,IP地址192.168.0.110,運行Apache 2.4.37提供Web服務
  • 負載均衡算法/機制/健康檢查:rr/DR/TCP_CHECK
  • VIP:192.168.0.5

主機A的角色爲LB,主機BC角色爲後端RS。這裏僅實現負載均衡,而不對LB作高可用集羣。html

二、在主機A上配置Keepalived

這裏不使用Keepalived郵件發送和VRRP功能,因此全局配置使用默認,而VRRP不進行配置,僅配置LVS部分。Keepalived配置以下:算法

! Configuration File for keepalived

global_defs {
   notification_email {
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

virtual_server 192.168.0.5 80 {
    delay_loop 5
    lb_algo rr
    lb_kind DR
    persistence_timeout 100
    protocol TCP
    real_server 192.168.0.88 80 {
        inhibit_on_failure
        TCP_CHECK {
            connect_timeout 5
            nb_get_retry 3
            delay_before_retry 5
        }
    }
    real_server 192.168.0.110 80 {
        inhibit_on_failure
        TCP_CHECK {
            connect_timeout 5
            nb_get_retry 3
            delay_before_retry 5
        }
    }
}

由於在Keepalived中沒有配置VRRP功能,因此必須手動爲主機A的ens160網卡接口綁定VIP:shell

[root@localhost keepalived]# ip addr add 192.168.0.5/24 dev ens160

啓動Keepalived:後端

[root@localhost keepalived]# systemctl start keepalived

Keepalived啓動後能夠經過ipvsadm查看當前的RS組:bash

[root@localhost keepalived]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.0.5:80 rr persistent 100
  -> 192.168.0.88:80              Route   1      0          0         
  -> 192.168.0.110:80             Route   1      0          0

三、在主機BC上綁定VIP並配置ARP抑制

經過如下腳本爲lo綁定VIP並配置ARP抑制:負載均衡

#!/bin/bash
VIP=192.168.0.5
/usr/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
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
sysctl -p

注意:腳本中調用了ifconfig命令,請確保該命令已經安裝。oop

編寫完腳本後,經過chmod命令爲腳本賦予可執行權限,而後執行該腳本。操作系統

四、驗證

首先直接訪問主機B跟主機C:3d

http://192.168.0.88code

1

http://192.168.0.110

2

主機B與主機C均正常提供Web服務。此時經過VIP192.168.0.5進行訪問:

3

4

因爲沒有設置後端節點的權重,因此這裏訪問會平均分配給兩個後端RS。經過在主機A上使用ipvsadm也能夠查看到當前各個後端RS的負載狀況:

[root@localhost keepalived]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.0.5:80 rr
  -> 192.168.0.88:80              Route   1      1          0         
  -> 192.168.0.110:80             Route   1      1          0

此時,將主機B上的Web服務中止,查看集羣狀況:

[root@localhost keepalived]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.0.5:80 rr
  -> 192.168.0.88:80              Route   0      0          0         
  -> 192.168.0.110:80             Route   1      0          0

主機B的權重已經被調整爲0了,說明Keepalived已經檢測到主機B出現故障,接下去的請求就不會再轉發給主機B了。

相關文章
相關標籤/搜索