CDN之多邊緣節點負載均衡--學習筆記


一.劇情

  劇情是這樣的,本次的多邊緣節點負載均衡實驗,1個LVS四層負載均衡集群和1個Nginx為反向代理的七層負載均衡集群,由Bind dns解析作為 主負載均衡服務器,調度兩個集群,中間層有一台Web服務器。前端是一台客戶端。如下圖所示:

          

 

二.基礎環境

1.硬件信息

  4G內存、50G硬盤、2核

  8台虛擬機系統為Centos 6.5,1台client系統為 windows server 2008。

2.網絡信息:

  IP地址按上圖的標明。(其中每台機器的dns設置,可以在所有軟件聯網安裝完成后,修改成環境中的192.168.40.105

3.系統內環境配置,所有linux系統內都安裝了開發工具,並關閉每台機器的防火牆。主機名已更改為圖上所示:

1 1 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo    (替換yum源為 阿里源)
2 (備用源,如果阿里源不能使用,請更換為網易源:http://mirrors.163.com/.help/CentOS6-Base-163.repo)
3 2 yum -y groupinstall "development tools"
4 3 service iptables stop

 

 

三.軟件部署

  此次實驗,我們可以分為兩部分,先做通LVS+squid+web四層負載架構。再做Nginx+Bind+Squid+web七層負載。最后將兩個集群結合在一起。

LVS四層負載均衡:

  1.安裝LVS服務器

1 yum install ipvsadm

  2.由於我們采用DR的工作方式,DR工作方式要求真實服務器的IP地址和虛擬IP地址(VIP)必須屬於同一網段。具體工作原理可參考LVS三種實現技術。這一步我們修改內核參數 /etc/sysctl.conf,在文件最后追加以下三句內容,

1 vim /etc/sysctl.conf
2 net.ipv4.ip_forwarf = 0          (此句,默認是為0)
3 net.ipv4.conf.all.send_redirects = 1
4 net.ipv4.conf.default.send_redirects = 1
5 net.ipv4.conf.eth0.send_redirects = 1

  修改完成后,立即生效:

1 sysctl -p

  3.配置LVS服務器的VIP即虛擬IP,后續的的兩台squid也要配置這個虛擬IP。並增加路由記錄

1 ifconfig eth0:0 192.168.40.128 netmask 255.25.255.255 broadcast 192.168.40.128 up
2 route add -host 192.168.40.128 dev eth0:0

  創建負載均衡服務器的虛擬服務及相應端口,指令rr 參數表示應用的調度算法為輪詢算法(Round Robin)

1 ipvsadm -A -t 192.168.40.128:80 -s rr

  建立虛擬服務與后台真實服務器squid1和squid2提供的http服務的鏈接,並使用DR的工作方式

1 ipvsadm -a -t 192.168.40.128:80 -r 192.168.40.122 -g
2 ipvsadm -a -t 192.168.40.128:80 -r 192.168.40.110 -g

  LVS服務器的配置就到這,下一步去配置squid1和squid2緩存代理。

  4.在squid1安裝squid軟件(squid2做同樣的設置,除了visible_hostname例外。)

1 yum install squid

  安裝后,我們先去編輯一下squid的配置文件 /etc/squid/squid.conf

1 vim /etc/squid/squid.conf
2 
3 cache_dir ufs /var/spool/squid 100 16 256
4 visible_hostname        squid
5 http_port 80 accel vhost vport
6 cache_peer 192.168.40.115 parent 80 0
7 http_access allow all
8 cache_mem 32 MB

  簡單的講下各參數的意思,cache_dir 是緩存的目錄,后面 100 代表的是這個目錄最多能存放100MB數據,visible_hostname 必須得設置一個主機名,http_port 80 本機開放的80端口,cache_peer 是表示當我本機沒有客戶端請求的資源時,將去這個上一級去取,在這里即是40.115的80端口去獲取。最后一個是cache_mem 表示在內存的緩存為32MB.

  改完上述文件后,我們用命令檢查一下squid

1 squid -z         初始化后啟動
2 squid -k parse    
3 squid -k reconfigure       修改配置后,重新加載

  接着,我們去配置squid的內核參數,使其不再轉發IP數據包和處理ARP協議包

1 vim /etc/sysctl.conf
2 net.ipv4.conf.lo.arp_ignore = 1
3 net.ipv4.conf.lo.arp_announce = 2
4 net.ipv4.conf.all.arp_ignore = 1
5 net.ipv4.conf.all.arp_announce = 2

  更行內核參數,執行以下命令:

1 sysctl -p

  配置虛擬IP(VIP),和LVS服務器配置同一個IP

1 ifconfig lo:0 192.168.40.128 netmask 255.255.255.255 broadcast 192.168.40.128 up
2 route add -host 192.168.40.128 dev lo:0

  OK,四層負載均衡服務器將VIP(192.168.40.128)提供給用戶訪問,並將用戶的訪問請求以輪詢的方式分發給后台的真實服務器,而由真實的服務器去響應客戶端,此時的返回給客戶這一步,將不再經過lvs。

  5.web服務器的安裝,使用apache來提供web服務,按照架構圖所示,我們登陸40.115這台機器,安裝LAMP環境,並創建兩個虛擬主機。

1 yum -y install httpd mysql mysql-server mysql-devel
2 yum -y install php php-mysql
3 cd /var/www/html/
4 mkdir -pv {baidu.test.com,sougo.test.com}
5 cd baidu.test.com
6 wget www.baidu.com
7 cd ../sougo.test.com
8 wget sogou.com

  軟件安裝完成后,我們去配置apache,編輯/etc/httpd/conf/httpd.conf 文件,修改或者追加以下內容。

1 vim /etc/httpd/conf/httpd.conf
2 
3 Listen 80
4 ServerName 192.168.40.115
5 NameVirtualHost 192.168.40.115:80
6 <VirtualHost 192.168.40.115:80>
7     ServerName baidu.test.com
8     DocumentRoot /var/www/html/baidu.test.com
9 </VirtualHost>                              (其實單做這個實驗時,無須使用虛擬主機,因為整體環境需要,所以這里提前修改好了。

  修改完成,檢查配置有無錯誤后,重新啟動httpd

1 apachectl -t
2 service httpd restart

  驗證,客戶端40.114去訪問192.168.40.128(四層負載的VIP地址。)打開瀏覽器,輸入http://192.168.40.128

1 [root@squid2 ~]# tail -f /var/log/squid/access.log 
2 1443079520.674     94 192.168.40.141 TCP_MISS/404 579 GET http://192.168.40.128/content-search.xml - FIRST_UP_PARENT/192.168.40.115 text/html
3 1443079520.776     99 192.168.40.141 TCP_MISS/404 572 GET http://192.168.40.128/favicon.ico - FIRST_UP_PARENT/192.168.40.115 text/html

  通過squid1的日志,我們可以看到,客戶端的請求,被分配到squid2這台代理服務器上。說明我們的四層負載均衡已經配置成功。如果實驗中出現打不開網址現象,或者其它異常的問題,首先去檢查一下各個防火牆有無關閉。(對防火牆熟悉的人可以單獨打開各服務端口)

 

Nginx七層負載均衡:

  1.七層負載均衡中的環境搭建,利用之前已經安裝好的apache的網站服務器,再有兩台Squid代理緩存服務器(主機名為squid3和squid4),一台windows 2008 的主機,一台Nginx服務器(豬腳),還有一台Bind域名服務器用於網站的解析指向nginx(sougo.test.com,對應為nginx的ip:192.168.40.107)。

  2.安裝Bind dns 軟件

1 yum -y install bind*

  關於bind軟件安裝完成后的配置,這里說一下,默認安裝后,各配置文件都存放在 /var/named/chroot/目錄下的子目錄中。該目錄下的etc/named.conf 是主配置文件,etc/named.rfc1912.zones 文件是添加域的。var/named/目錄下 存放的是域名正向解析文件和反向解析文件存放的位置。

  先cp一些文件到/var/named/chroot/var/named/目錄下,(后續會用的到,不然啟動會報錯。)

1 cp /var/named/named.*

  編輯named.rfc1912.zones文件,添加test.com.域(只添加標紅的)

 1 [root@bind ~]# vim /var/named/chroot/etc/named.rfc1912.zones
 2 // named.rfc1912.zones:
 3 //
 4 // Provided by Red Hat caching-nameserver package
 5 //
 6 // ISC BIND named zone configuration for zones recommended by
 7 // RFC 1912 section 4.1 : localhost TLDs and address zones
 8 // and http://www.ietf.org/internet-drafts/draft-ietf-dnsop-default-local-zones-02.txt
 9 // (c)2007 R W Franks
10 //
11 // See /usr/share/doc/bind*/sample/ for example named configuration files.
12 //
13 
14 zone "localhost.localdomain" IN {
15         type master;
16         file "named.localhost";
17         allow-update { none; };
18 };
19 
20 zone "localhost" IN {
21         type master;
22         file "named.localhost";
23         allow-update { none; };
24 };
25 
26 zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" IN {
27         type master;
28         file "named.loopback";
29         allow-update { none; };
30 };
31 
32 zone "1.0.0.127.in-addr.arpa" IN {
33         type master;
34         file "named.loopback";
35         allow-update { none; };
36 };
37 
38 zone "0.in-addr.arpa" IN {
39         type master;
40         file "named.empty";
41         allow-update { none; };
42 };
43 
44 zone "test.com" IN {                //正向解析
45         type master;
46         file "named.test.com";
47 };
48 
49 
50 zone "40.168.192.in-addr.arpa" IN {        //反向解析
51         type master;
52         file "named.192.168.40";
53 };

  添加域后,我們去編輯正向解析文件 named.test.com,內容為下:

 1 [root@bind ~]# vim /var/named/chroot/var/named/named.test.com 
 2 $TTL 1D
 3 @       IN SOA  @ root.test.com. (
 4                                         0       ; serial
 5                                         1D      ; refresh
 6                                         1H      ; retry
 7                                         1W      ; expire
 8                                         3H )    ; minimum
 9                 NS      @
10                 A       127.0.0.1
11                 AAAA    ::1
12 bind    IN      A       192.168.40.105
13 www     IN      A       192.168.40.115
14 sougo   IN      A       192.168.40.107

  編輯反向解析文件named.192.168.40(反向解析在整個實驗中,不是必須的需要。只是為了更完整一些)

 1 [root@bind ~]# vim /var/named/chroot/var/named/named.192.168.40
 2 $TTL 1D
 3 @       IN SOA  40.168.192.in-addr.arpa. root.test.com. (
 4                                         0       ; serial
 5                                         1D      ; refresh
 6                                         1H      ; retry
 7                                         1W      ; expire
 8                                         3H )    ; minimum
 9 @       IN      NS      bind.test.com.
10 107     IN      PTR     sougo.test.com.
11 115     IN      PTR     www.test.com.

  編輯完成后,我們檢查一下配置文件

1 [root@bind ~]# named-checkconf
2 [root@bind ~]# named-checkzone 

  檢查無誤后,重啟named守護進程。

1 [root@bind ~]# service named restart

  可以在客戶端40.114上,修改網絡配置,將dns修改成40.115

  測試一下。cmd內執行nslookup 解析一下,如下圖所示,BINd服務器已經提供了域名解析服務,如果無法解析,請檢查bind 主機的iptables是否關閉。

  3.Nginx服務器的搭建,在40.107上安裝nginx軟件。

1 yum install nginx

  修改nginx配置文件,有的需要追加進去,有的內容可能已經存在,這點需要自己判斷一下,進行修改。

 1 [root@nginx ~]# vim /etc/nginx/conf.d/default.conf
 2 
 3 upstream webservers {          #此處是后台squid代理服務器集群的地址,端口是80,后面我們將對squid3 和squid4 服務器進行修改內核參數及squid服務配置
 4         server 192.168.40.112:80;   # upstream將按照默認的輪詢方式
 5         server 192.168.40.113:80;
 6 }
 7 
 8 server {
 9     listen       80;
10     server_name  sougo.test.com;
11 
12     #charset koi8-r;
13     #access_log  /var/log/nginx/log/host.access.log  main;
14 
15     location ~/ {
16         proxy_pass      http://webservers;
17         proxy_redirect  off;
18         proxy_set_header Host   $host;
19         proxy_set_header X-Real-IP      $remote_addr;
20         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
21     }
22 
23     #error_page  404              /404.html;
24 
25     # redirect server error pages to the static page /50x.html
26     #
27     error_page   500 502 503 504  /50x.html;
28     location = /50x.html {
29         root   /usr/share/nginx/html;
30     }
31 
32     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
33     #
34     #location ~ \.php$ {
35     #    proxy_pass   http://127.0.0.1;
36     #}
37 
38     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
39     #
40     #location ~ \.php$ {
41     #    root           html;
42     #    fastcgi_pass   127.0.0.1:9000;
43     #    fastcgi_index  index.php;
44     #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
45     #    include        fastcgi_params;
46     #}
47 
48     # deny access to .htaccess files, if Apache's document root
49     # concurs with nginx's one
50     #
51     #location ~ /\.ht {
52     #    deny  all;
53     #}
54 }

  編輯主配置文件/etc/nginx,添加或者修改以下參數:

1 [root@nginx ~]# vim /etc/nginx/nginx.conf
2 
3 events {
4     worker_connections  1024;
5     use epoll;
6 }

  修改以上兩個文件后,測試檢查並重啟nginx服務器

1 [root@nginx ~]# nginx -t
2 [root@nginx ~]# /etc/init.d/nginx restart

   4.后台Squid兩台服務器的配置(squid3 和 squid4 配置一樣。)此處,以squid3為例:

1 [root@squid3 ~]# vim /etc/squid/squid.conf
2 
3 cache_dir ufs /var/spool/squid 100 16 256
4 visible_hostname squid3
5 http_port 80 accel vhost vport
6 cache_peer 192.168.40.115 parent 80 0
7 http_access allow all
8 cache_mem 32 MB

  上述的配置就可以將squid3 和squid4 配置為 web站點 (sougo.test.com)代理緩存服務器。配置完成后記得重啟服務器

[root@squid3 ~]# /etc/init.d/squid restart

  現在dns解析 sougo.test.com 為nginx服務器ip 40.107后,我們去客戶端驗證一下。如下圖所示。

  去squid3上查看一下日志,本機沒有資源,去上一級即40.115上去下載資源。然后由squid響應給客戶端

[root@squid3 ~]# tail -f /var/log/squid/access.log 
1443020309.684      1 192.168.40.107 TCP_MISS/404 579 GET http://sougo.test.com/web/js/addbanner.min.js - FIRST_UP_PARENT/192.168.40.115 text/html
1443020309.893      1 192.168.40.107 TCP_MISS/404 578 GET http://sougo.test.com/images/index/spy_h.png - FIRST_UP_PARENT/192.168.40.115 text/html
1443020309.907      1 192.168.40.107 TCP_MISS/404 567 GET http://sougo.test.com/favicon.ico - FIRST_UP_PARENT/192.168.40.115 text/html
1443077070.478    673 192.168.40.107 TCP_MISS/404 567 GET http://sougo.test.com/favicon.ico - FIRST_UP_PARENT/192.168.40.115 text/html
1443080793.374     88 192.168.40.107 TCP_MISS/404 567 GET http://sougo.test.com/favicon.ico - FIRST_UP_PARENT/192.168.40.115 text/html
1443091064.977      6 192.168.40.107 TCP_MEM_HIT/200 12904 GET http://sougo.test.com/ - NONE/- text/html

 

  現在,七層負載均衡的實驗也得到了成功響應。那么下一步,我們就將兩個負載集群融合在一塊。。。

 

多邊緣節點負載均衡:

  1.這次,我們將兩個集群變成多邊緣。先說web,apache 有兩個 虛擬主機,分別是 baidu.test.com(對應端口 192.168.40.115:80) 和 sougo.test.com(對應端口 192.168.40.115:8080),通過Bind DNS的解析,當用戶訪問 baidu.test.com域名時,dns解析的地址是192.168.40.128(VIP地址),跳轉到四層負載集群;當用戶訪問sougo.test.com域名時,dns解析到192.168.40.107(nginx),跳轉到七層負載集群。在此,Bind 擔任的是主負載均衡服務器。下面先進行dns的配置:

  2.Bind 的配置,其實只需添加一條主機記錄,編輯 /var/named/chroot/var/named/named.test.com 正向解析文件

 1 [root@bind ~]# vim /var/named/chroot/var/named/named.test.com
 2 
 3 $TTL 1D
 4 @       IN SOA  @ root.test.com. (
 5                                         0       ; serial
 6                                         1D      ; refresh
 7                                         1H      ; retry
 8                                         1W      ; expire
 9                                         3H )    ; minimum
10                 NS      @
11                 A       127.0.0.1
12                 AAAA    ::1
13 bind    IN      A       192.168.40.105
14 www     IN      A       192.168.40.115
15 baidu   IN      A       192.168.40.128        #(baidu.test.com 與邊緣節點1的LVS服務器VIP綁定)
16 sougo   IN      A       192.168.40.107        #(sougo.test.com 與邊緣節點2的nginx服務器綁定)

  編輯后,重啟域名named服務

1 [root@bind ~]# service named restart

  3.Web服務器的配置,編輯 /etc/httpd/conf/httpd.conf,在最后添加或者修改成下面這樣。

 1 [root@www ~]# vim /etc/httpd/conf/httpd.conf
 2 
 3 Listen 80
 4 Listen 8080
 5 NameVirtualHost 192.168.40.115:80
 6 NameVirtualHost 192.168.40.115:8080
 7 
 8 <VirtualHost 192.168.40.115:80>
 9     ServerName baidu.test.com
10     DocumentRoot /var/www/html/baidu.test.com        (虛擬主機baidu 網頁存放路徑)
11 </VirtualHost>
12 
13 <VirtualHost 192.168.40.115:8080>
14     ServerName sougo.test.com
15     DocumentRoot /var/www/html/sougo.test.com        (虛擬主機sougo網頁存放路徑)
16 </VirtualHost>

  開啟兩個端口,80提供給baidu.test.com,8080提供給sougo.test.com。  編輯完成后,測試並重啟httpd服務:

1 [root@www ~]# apachectl -t 
2 [root@www ~]# /etc/init.d/httpd restart

  4.現在修改七層負載集群中的代理服務器,因為它們要代理訪問的是sogou.test.com,所以現在修改squid3 和 squid4 服務器。(兩台都要同樣設置)以squid3為例:

1 [root@squid3 ~]# vim /etc/squid/squid.conf
2 
3 visible_hostname squid3
4 http_port 80 accel vhost vport
5 cache_peer 192.168.40.115 parent 8080 0          (主要修改這個端口為8080即可,其它的,可按上個實驗環境中的配置。)
6 http_access allow all
7 cache_mem 32 MB

  修改cache_peer 字段,當squid服務器的本地緩存內容不能滿足80端口上的http內容請求時,將向其的上一級節點 192.168.40.115的8080端口上去請求並接收數據。從而建立了web站點8080端口的連接,及緩存關系。

  修改完成后,重新啟動squid服務

[root@squid3 ~]# service squid restart

  5.四層的兩台squid服務器集群,由於squid.conf 文件中默認80指向的是baidu.test.com,所以我們保持默認即可。至此,8台linux服務器的配置都已ok,現在就是在客戶端的測試了。下面看下訪問的效果。

  當訪問baidu.test.com域名時:

  運行ipvsadm -l 命令,可以看到,在沒訪問之前,運行時,連接時為0 的,但我們訪問后,在運行命令,便變成了1,此時,可以多次刷新網頁,查看調度情況

  同樣的道理,當用戶訪問sougo.test.com域名時,請求將被Bind全局負載解析調度到Nginx服務器上,再由七層中的集群緩存代理處理網頁瀏覽請求。

 

三.總結

  本次記錄,主要利用了Bind作為全局負載,調度以LVS四層負載 + Nginx七層負載的集群,去訪問web。在整個環節中,注意的幾點:修改內核參數后,立即生效命令:sysctl -p 會打開防火牆,所以在每個環節中如果遇到問題,先排查一下iptables是否關閉。還有一點在dns配置時,也需要謹慎,因為正向或者反向,有時少一個. 可能就會造成解析失敗。至於其中利用的技術原理,可以去百度補補。有的我也不是特別的完全明白。\(•ω•`)o


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM