LVS負(fù)載均衡高可用集群及Sorry Server

實(shí)驗(yàn)環(huán)境

1. host1 host2 host3 host4 都是 CentOS7.3 系統(tǒng);
2. host3 host4 作為 Real Server,搭建Web服務(wù);
3. host1 host2 作為LVS負(fù)載均衡高可用集群;
4. HA Cluster的配置前提:
    (1) 各節(jié)點(diǎn)時(shí)間必須同步;
            ntp, chrony
    (2) 確保iptables及selinux不會(huì)成為阻礙;
    (3) 各節(jié)點(diǎn)之間可通過主機(jī)名互相通信(對(duì)KA并非必須);
            建議使用/etc/hosts文件實(shí)現(xiàn); 
    (4) 確保各節(jié)點(diǎn)的用于集群服務(wù)的接口支持MULTICAST(多播)通信;
            D類:224-239

配置Real Server

  • 安裝Nginx并修改測(cè)試頁:
# RS1:
[root@host3 ~]#yum -y install nginx
[root@host3 ~]#echo "<h1>RS1:host3</h1>" > /usr/share/nginx/html/index.html 
[root@host3 ~]#cat /usr/share/nginx/html/index.html                             
<h1>RS1:host3</h1>
[root@host3 ~]#systemctl start nginx
[root@host3 ~]#ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port
LISTEN     0      128                                                        *:80
LISTEN     0      128                                                        *:22
LISTEN     0      100                                                127.0.0.1:25 
LISTEN     0      128                                                       :::80
LISTEN     0      128                                                       :::22
LISTEN     0      100                                                      ::1:25 
[root@host3 ~]#

------------------------------------------------------------------------------------------------------
# RS2:
[root@host4 ~]#yum -y install nginx 
[root@host4 ~]#echo "<h1>RS2:host4</h1>" > /usr/share/nginx/html/index.html 
[root@host4 ~]#cat /usr/share/nginx/html/index.html 
<h1>RS2:host4</h1>
[root@host4 ~]#systemctl start nginx             
[root@host4 ~]#ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port              
LISTEN     0      128                                                        *:80                  
LISTEN     0      128                                                        *:22                  
LISTEN     0      100                                                127.0.0.1:25                 
LISTEN     0      128                                                       :::80                  
LISTEN     0      128                                                       :::22                  
LISTEN     0      100                                                      ::1:25                  
[root@host4 ~]#
------------------------------------------------------------------------------------------------------
# 測(cè)試:
>.host1:
[root@host1 ~]#curl http://192.168.10.13
<h1>RS1:host3</h1>
[root@host1 ~]#curl http://192.168.10.14
<h1>RS2:host4</h1>
[root@host1 ~]#
>.host2:
[root@host2 ~]#curl http://192.168.10.13
<h1>RS1:host3</h1>
[root@host2 ~]#curl http://192.168.10.14
<h1>RS2:host4</h1>
[root@host2 ~]#
  • 修改RS的內(nèi)核參數(shù)并配置VIP
RS1
RS1上給權(quán)限
RS1上執(zhí)行腳本并查看lo:0的IP地址
RS2
RS2上給權(quán)限
RS2上執(zhí)行腳本并查看lo:0的IP地址

搭建LVS集群

給兩臺(tái)VS服務(wù)器 host1 host2上 安裝 ipvsadmkeepalived

# VS1:
[root@host1 ~]#yum -y install ipvsadm keepalived

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

# VS2:
[root@host2 ~]#yum -y install ipvsadm keepalived

配置LVS高可用集群服務(wù)器

  • 配置VS服務(wù)器host1的keepalived服務(wù)并啟動(dòng):
[root@host1 keepalived]#cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     ngt@mgl.com
   }
   notification_email_from grh_ngt@mgl.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id host1
   vrrp_mcast_group4 224.89.51.18
}

vrrp_instance VI_1 {
    state MASTER                       <-- 配置 host1 為 MASTER
    interface ens33
    virtual_router_id 89
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass nQyVIaY1
    }
    virtual_ipaddress {
        192.168.10.88                  <-- 虛擬 IP 為 VIP
    }
}

virtual_server 192.168.10.88 80 {      <-- 配置 VS 服務(wù)器 host1
    delay_loop 6
    lb_algo rr                         <-- LVS調(diào)度算法
    lb_kind DR                         <--LVS工作模式
    nat_mask 255.255.255.255
    protocol TCP

    real_server 192.168.10.13 80 {      <-- 添加第一臺(tái) RS服務(wù)器 host3
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200 
            }
            connect_timeout 2
            nb_get_retry 3
            delay_before_retry 1
        }
        }
    real_server 192.168.10.14 80 {       <--添加第二臺(tái)RS服務(wù)器 host4
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200 
            }
            connect_timeout 2
            nb_get_retry 3
            delay_before_retry 1
        }
    }
}
[root@host1 keepalived]#systemctl start keepalived
  • 配置VS服務(wù)器host2的keepalived服務(wù)并啟動(dòng):

只需把vrrp_instance VI_1state改為BACKUP即可,其他與VS1保持一致

[root@host1 keepalived]#cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     ngt@mgl.com
   }
   notification_email_from grh_ngt@mgl.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id host1
   vrrp_mcast_group4 224.89.51.18
}

vrrp_instance VI_1 {
    state BACKUP                        <-- 配置 host2 為 BACKUP
    interface ens33
    virtual_router_id 89
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass nQyVIaY1
    }
    virtual_ipaddress {
        192.168.10.88                  <-- 虛擬 IP 為 VIP
    }
}

virtual_server 192.168.10.88 80 {      <-- 配置 VS 服務(wù)器 host2
    delay_loop 6
    lb_algo rr                         <-- LVS調(diào)度算法
    lb_kind DR                         <--LVS工作模式
    nat_mask 255.255.255.255
    protocol TCP

    real_server 192.168.10.13 80 {      <-- 添加第一臺(tái) RS服務(wù)器 host3
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200 
            }
            connect_timeout 2
            nb_get_retry 3
            delay_before_retry 1
        }
        }
    real_server 192.168.10.14 80 {       <--添加第二臺(tái)RS服務(wù)器 host4
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200 
            }
            connect_timeout 2
            nb_get_retry 3
            delay_before_retry 1
        }
    }
}
[root@host2 keepalived]#systemctl start keepalived

測(cè)試:

如何測(cè)試的詳細(xì)步驟我就不細(xì)寫了,貼個(gè)圖意思意思下:


Sorry Server

在兩臺(tái)VS上也安裝Nginx,設(shè)置頁面內(nèi)容為Say sorry的message!

  • VS1上:
[root@host1 keepalived]#yum -y install nginx
[root@host1 keepalived]#echo "<h1>Sorry from Director 1</h1>" > /usr/share/nginx/html/index.html
[root@host1 keepalived]#systemctl start nginx
  • VS2上:
[root@host2 keepalived]#yum -y install nginx
[root@host2 keepalived]#echo "<h1>Sorry from Director 2</h1>" > /usr/share/nginx/html/index.html
[root@host2 keepalived]#systemctl start nginx
  • 編輯配置文件:

很簡(jiǎn)單,只需在兩臺(tái)VS的配置文件keepalived.conf的virtual_server里加一行sorry_server 127.0.0.1 80 即可!

  • 測(cè)試Sorry Server 的效果:
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容