實驗lvs+dns+nfs+mysql+web


LVS優勢:工做在內核級別,不受socket文件數量的限制,併發數量對調度器自己來說能達到3百萬,缺點:工做在TCP層(IP+PORT),不具有對應用層報文調度,如網頁的動靜分離。php

服務器IP地址規劃(10.x模擬公網地址,192.x私網地址,172.x私網地址)

NFS:192.168.30.33
WEB1:192.168.30.17
WEB2:192.168.30.27
LVS:192.168.30.7 VIP:10.0.0.100
DNS:172.20.42.27
Mysql:192.168.30.30
Route:192.168.30.208, 10.0.0.200,172.20.42.200
Client: Windows IP 172.20.42.222

各服務器配置

WEB1

1. 網絡
    ifcfg-eth0
    DEVICE=eth0
    IPADDR=192.168.30.17
    PREFIX=24
    GATEWAY=192.168.30.208
    ifcfg-eth0:1
    DEVICE=eth0:1
    IPADDR=10.0.0.100
    PREFIX=8
2. 安裝相應的包
    yum install httpd php-fpm php-mysql -y
3. 在RS上修改內核參數以限制arp通告及應答級
    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

WEB2

參考WEB1,只是將IP更改成192.168.30.27

DNS

1. 安裝 yum install bind -y
2. 配置

vim /etc/named.rfc1912.zones
zone "blog.com" IN {
type master;
file "blog.com.zone";
};
vim /var/named/blog.com.zone
$TTL D
@ IN SOA master.blog.com admin.blog.com. (
0 ; serial
D ; refresh
H ; retry
W ; expire
H ) ; minimum
NS master
master A 172.20.42.27
www A 10.0.0.100html

3. 啓動systemctl restart named

Route路由配置

1. 網絡
    ifcfg-eth0
    DEVICE=eth0
    BOOTPROTO=none
    IPADDR=192.168.30.208
    PREFIX=24
    ifcfg-eth0:1
    DEVICE=eth0:1
    BOOTPROTO=none
    IPADDR=10.0.0.200
    PREFIX=8
    ifcfg-eth1
    DEVICE=eth1
    BOOTPROTO=none
    IPADDR=172.20.42.200
    PREFIX=16
2. 啓用ip_forward
    echo 1 > /prov/sys/net/ipv4/ip_forward
    sysctl -a |grep ip_forward: net.ipv4.ip_forward = 1

LVS配置

1. 網絡
ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
IPADDR=192.168.30.7
PREFIX=24
GATEWAY=192.168.30.208
ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=none
IPADDR=10.0.0.100
PREFIX=8
2. 運行以下的腳本(適當進行更改)
   [root@lvs ~]#cat lvs_dr_vs.sh 
    #!/bin/bash
    #Author:wangxiaochun
    #Date:2017-08-13
    vip='10.0.0.100'
    iface='eth0:1'
    mask='255.255.255.255'
    port='80'
    rs1='192.168.30.17'
    rs2='192.168.30.27'
    scheduler='wrr'
    type='-g'
    rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null

    case $1 in
start)
    ifconfig $iface $vip netmask $mask #broadcast $vip up
    iptables -F

    ipvsadm -A -t ${vip}:${port} -s $scheduler
    ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
    ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
    echo "The VS Server is Ready!"
    ;;
stop)
    ipvsadm -C
    ifconfig $iface down
    echo "The VS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac
    3. 查看ipvsadm -Ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  10.0.0.100:80 wrr
      -> 192.168.30.17:80             Route   1      0          0         
      -> 192.168.30.27:80             Route   1      0          0         
    4. 更改lvs-dr的調度策略
    ipvsadm -C
    ipvsadm -A -t 10.0.0.100:80 -s rr
    ipvsadm -a -t 10.0.0.100:80 -r 192.168.30.17
    ipvsadm -a -t 10.0.0.100:80 -r 192.168.30.27
    5. 測試新的策略
    在Linux客戶機上,配置網絡
    DEVICE=eth2
    BOOTPROTO=none
    IPADDR=172.20.0.7![](https://images2018.cnblogs.com/blog/1386066/201807/1386066-20180703114658714-1685106874.png)


    PREFIX=16
    GATEWAY=172.20.42.200
    DNS1=172.20.42.27

    [root@centos7 ~]# curl www.blog.com
    web1
    [root@centos7 ~]# curl www.blog.com
    web2
    [root@centos7 ~]# curl www.blog.com
    web1
    [root@centos7 ~]# curl www.blog.com
    web2
    測試結論:達到了roundrobin輪詢的效果
    6. 在windows PC上測試


在瀏覽器中訪問www.blog.com,能夠看出,不是每次刷新網站都會切換網站,是由於瀏覽器的緩存致使。

mysql

LVS高可用性

上述LVS的方案有以下缺點:
1. Director不可用,整個系統將不可用;SPoF Single Point of Failure
解決方案:高可用keepalived heartbeat/corosync
2. 某RS不可用時,Director依然會調度請求至此RS
解決方案: 由Director對各RS健康狀態進行檢查,失敗時禁用,成功時啓用keepalived heartbeat/corosync ldirectord
檢測方式:
(a) 網絡層檢測,icmp
(b) 傳輸層檢測,端口探測
(c) 應用層檢測,請求某關鍵資源
RS全不用時:backup server, sorry serverweb

ldirectord安裝配置過程

1.  yum install  ldirectord-3.9.6-0rc1.1.1.x86_64.rpm
2.  cp    /usr/share/doc/ldirectord-3.9.6/ldirectord.cf /etc/ha.d/
    更改配置文件
    checktimeout=3
    checkinterval=1
    fallback=127.0.0.1:80
    #fallback6=[::1]:80
    autoreload=yes
    logfile="/var/log/ldirectord.log"
    quiescent=no

    # Sample for an http virtual service
   virtual=10.0.0.100:80
        real=192.168.30.17:80 gate(gate表示LVS-DR)
        real=192.168.30.27:80 gate
        fallback=127.0.0.1:80 gate
        service=http
        scheduler=rr 
        protocol=tcp
        checktype=negotiate
        checkport=80
        request="test.html"
        receive="web"


    以下的配置文件表示有權重:
        real=192.168.30.17:80 gate 1
        real=192.168.30.27:80 gate 2
        fallback=127.0.0.1:80 gate
        service=http
        scheduler=wrr 
3. systemctl start ldirectord.service
4. 同時在兩個WEB服務器上的/var/www/html創建text.html,內容分別爲web1和web2
5. 測試
    在LVS服務器上檢查狀態:
        [root@lvs ha.d]# ipvsadm -Ln
        IP Virtual Server version 1.2.1 (size=4096)
        Prot LocalAddress:Port Scheduler Flags
          -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
        TCP  10.0.0.100:80 rr
          -> 192.168.30.17:80             Route   1      0          0
          -> 192.168.30.27:80             Route   1      0          0
    在WEB2服務器上,中止httpd服務systemctl stop httpd
    在LVS服務器上,ipvsadm -Ln,能夠看到WEB2已經不在LVS的集羣節點中:
          -> 192.168.30.17:80             Route   1      0          3
    中止WEB1服務器上的httpd,在LVS服務器上,能夠看到:
          -> 127.0.0.1:80                 Route   1      0          1
    同時在LVS上啓動httpd,主頁裏顯示Sorry,please be patient.,當如上兩個WEB服務器都不能提供服務時,終端訪問會獲得以下的顯示:
        [root@centos7 home]# curl www.blog.com
        Sorry,please be patient.
相關文章
相關標籤/搜索