随着網際網路資訊的爆炸性增長,負載均衡(load balance)已經不再是一個很陌生的話題,顧名思義,負載均衡即是将負載分攤到不同的服務單元,既保證服務的可用性,又保證響應足夠快,給使用者很好的體驗。快速增長的通路量和資料流量催生了各式各樣的負載均衡産品,很多專業的負載均衡硬體提供了很好的功能,但卻價格不菲,這使得負載均衡軟體大受歡迎,nginx就是其中的一個,在linux下有Nginx、LVS、Haproxy等等服務可以提供負載均衡服務,而且Nginx提供了幾種配置設定方式(政策),當然現在主流的公有雲(Windows Azure)提供的服務在均衡配置設定方式基本上都類似于Nginx的輪詢(round robin)配置設定方式和權重輪詢(Weight round robin)配置設定方式,對于其他公有雲産品沒有具體試驗過,是以我們今天主要介紹一下Nginx下的幾種配置設定方式,具體見下:
1、輪詢(預設)
每個請求按時間順序逐一配置設定到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。
2、weight
指定輪詢幾率,weight和通路比率成正比,用于後端伺服器性能不均的情況。 例如:
1
2
3
4
<code>upstream server_pool{ </code>
<code>server 192.168.5.21 weight=10; </code>
<code>server 192.168.5.22 weight=10; </code>
<code>}</code>
3、ip_hash
每個請求按通路ip的hash結果配置設定,這樣每個訪客固定通路一個後端伺服器,可以解決session的問題。 例如:
5
<code>ip_hash; </code>
<code>server 192.168.5.21:80; </code>
<code>server 192.168.5.22:80; </code>
4、fair(第三方)
按後端伺服器的響應時間來配置設定請求,響應時間短的優先配置設定。
<code>fair; </code>
注意:整個配置設定方式是通過修改定義負載均衡的server配置中添加的。
我們今天主要介紹我個人認為使用最多的幾種方式吧;前面四種。
我們後端使用兩台Apache服務作為WEB服務,具體安裝就不多介紹了。
192.168.5.21
<code>Yum </code><code>install</code> <code>httpd</code>
<a href="http://s3.51cto.com/wyfs02/M01/8C/49/wKiom1hojB_AqvmgAALl4sDDNxw999.png" target="_blank"></a>
檢視httpd 版本
<code>rpm -qa | </code><code>grep</code> <code>httpd</code>
<a href="http://s3.51cto.com/wyfs02/M01/8C/45/wKioL1hojCHQEEpdAAASJYrQr6o885.png" target="_blank"></a>
接下來我們首先要為apache定義一個 預設的頁面,友善區分;我們為了後面的資料統計是以頁面内容顯示的比較少
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<code>Vim </code><code>/var/www/httml/index</code><code>.html</code>
<code><</code><code>/html</code><code>></code>
<code><!DOCTYPE html></code>
<code><html></code>
<code><</code><code>head</code><code>></code>
<code><title>Welcome to Apache<</code><code>/title</code><code>></code>
<code><style></code>
<code>body {</code>
<code>35em;</code>
<code>margin: 0 auto;</code>
<code>font-family: Tahoma, Verdana, Arial, sans-serif;</code>
<code><</code><code>/style</code><code>></code>
<code><style </code><code>type</code><code>=</code><code>"text/css"</code><code>></code>
<code>h1{color:red}</code>
<code>h2{color:blue}</code>
<code>h3{color:green}</code>
<code>h4{color:yellow}</code>
<code><</code><code>/head</code><code>><body bgcolor=</code><code>'7D7DFF'</code><code>></code>
<code><h2>HostName:A-S ----->IP:192.168.5.21<</code><code>/h2</code><code>></code>
<code><</code><code>/body</code><code>></code>
儲存退出,啟動服務
<code>Systemctl start httpd</code>
<a href="http://s3.51cto.com/wyfs02/M00/8C/45/wKioL1hojCLDdm3pAABCQW-1KUk506.png" target="_blank"></a>
然後添加預設的防火牆端口8o
<code>Firewall-cmd --zone=public --add-port=</code><code>'80/tcp'</code> <code>--permanent</code>
<code>或者vim </code><code>/etc/firewalld/zone/public</code><code>.xml</code>
<code>添加一下格式</code>
<code><port portocal=</code><code>'tcp'</code> <code>port=</code><code>'80'</code><code>></code>
<a href="http://s3.51cto.com/wyfs02/M01/8C/45/wKioL1hojCPDfxRDAAAtyEOOolM124.png" target="_blank"></a>
我們測試通路
<a href="http://s3.51cto.com/wyfs02/M02/8C/49/wKiom1hojCbhvyCUAAEVkfD7x7U486.png" target="_blank"></a>
我們準備第二台WEB服務(192.168.5.22)我們按照第一台的方式進行配置,再次就跳過了。
第二台主機的配置:192.168.5.22 主機名 B-S
安裝好httpd後,我們将a-s上的index拷貝到b-s伺服器上
<code>scp</code> <code>index.html [email protected]:</code><code>/var/www/html/</code>
然後修改index.html檔案
<a href="http://s3.51cto.com/wyfs02/M00/8C/49/wKiom1hojCej9n38AABKdUlCaSA657.png" target="_blank"></a>
我們為了後面的測試,我們将兩台伺服器的顯示内容修改一下,為了通過服務進行批量測試。
将兩台Apache WEB服務的index.html檔案隻顯示伺服器的名稱及IP位址。
<a href="http://s3.51cto.com/wyfs02/M02/8C/45/wKioL1hojCiAwQf1AABsC7P2D6Y696.png" target="_blank"></a>
接下來我們就部署nginx,在Centos7上yum安裝需要定義yum源
<code>cd</code> <code>/etc/yum</code><code>.repo</code>
<code>vim epel.repo</code>
<code>添加以下内容</code>
<code>[epel] </code>
<code>name=aliyun epel </code>
<code>baseurl=http:</code><code>//mirrors</code><code>.aliyun.com</code><code>/epel/7Server/x86_64/</code>
<code>gpgcheck=0</code>
<a href="http://s3.51cto.com/wyfs02/M00/8C/45/wKioL1hojCuhy85tAAAYeR5lMkM821.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M01/8C/49/wKiom1hojCyzxHcHAACGN72uhdc860.png" target="_blank"></a>
<code>Yum </code><code>install</code> <code>nginx</code>
<code>nginx 192.168.5.20</code>
<a href="http://s3.51cto.com/wyfs02/M01/8C/45/wKioL1hojC3T-GMCAACFU_6hASw144.png" target="_blank"></a>
啟動服務
<code>systemctl start nginx</code>
<a href="http://s3.51cto.com/wyfs02/M02/8C/45/wKioL1hojDHRtisYAACWo3cRR_g935.png" target="_blank"></a>
我們接下來需要編輯nginx的預設配置檔案來修改負載的配置
我們首先檢視預設的配置檔案
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<code>#user nobody;</code>
<code>worker_processes 1;</code>
<code>#error_log logs/error.log;</code>
<code>#error_log logs/error.log notice;</code>
<code>#error_log logs/error.log info;</code>
<code>#pid logs/nginx.pid;</code>
<code>events {</code>
<code> </code><code>worker_connections 1024;</code>
<code>http {</code>
<code> </code><code>include mime.types;</code>
<code> </code><code>default_type application</code><code>/octet-stream</code><code>;</code>
<code> </code><code>#log_format main '$remote_addr - $remote_user [$time_local] "$request" '</code>
<code> </code><code># '$status $body_bytes_sent "$http_referer" '</code>
<code> </code><code># '"$http_user_agent" "$http_x_forwarded_for"';</code>
<code> </code><code>#access_log logs/access.log main;</code>
<code> </code><code>sendfile on;</code>
<code> </code><code>#tcp_nopush on;</code>
<code> </code><code>#keepalive_timeout 0;</code>
<code> </code><code>keepalive_timeout 65;</code>
<code> </code><code>#gzip on;</code>
<code> </code><code>server {</code>
<code> </code><code>listen 80;</code>
<code> </code><code>server_name localhost;</code>
<code> </code><code>#charset koi8-r;</code>
<code> </code><code>#access_log logs/host.access.log main;</code>
<code> </code><code>location / {</code>
<code> </code><code>root html;</code>
<code> </code><code>index index.html index.htm;</code>
<code> </code><code>}</code>
<code> </code><code>#error_page 404 /404.html;</code>
<code> </code><code># redirect server error pages to the static page /50x.html</code>
<code> </code><code>#</code>
<code> </code><code>error_page 500 502 503 504 </code><code>/50x</code><code>.html;</code>
<code> </code><code>location = </code><code>/50x</code><code>.html {</code>
<code> </code><code># proxy the PHP scripts to Apache listening on 127.0.0.1:80</code>
<code> </code><code>#location ~ \.php$ {</code>
<code> </code><code># proxy_pass http://127.0.0.1;</code>
<code> </code><code>#}</code>
<code> </code><code># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</code>
<code> </code><code># root html;</code>
<code> </code><code># fastcgi_pass 127.0.0.1:9000;</code>
<code> </code><code># fastcgi_index index.php;</code>
<code> </code><code># fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;</code>
<code> </code><code># include fastcgi_params;</code>
<code> </code><code># deny access to .htaccess files, if Apache's document root</code>
<code> </code><code># concurs with nginx's one</code>
<code> </code><code>#location ~ /\.ht {</code>
<code> </code><code># deny all;</code>
<code> </code><code>}</code>
<code> </code><code># another virtual host using mix of IP-, name-, and port-based configuration</code>
<code> </code><code>#</code>
<code> </code><code>#server {</code>
<code> </code><code># listen 8000;</code>
<code> </code><code># listen somename:8080;</code>
<code> </code><code># server_name somename alias another.alias;</code>
<code> </code><code># location / {</code>
<code> </code><code># root html;</code>
<code> </code><code># index index.html index.htm;</code>
<code> </code><code># }</code>
<code> </code><code>#}</code>
<code> </code><code># HTTPS server</code>
<code> </code><code># listen 443 ssl;</code>
<code> </code><code># server_name localhost;</code>
<code> </code><code># ssl_certificate cert.pem;</code>
<code> </code><code># ssl_certificate_key cert.key;</code>
<code> </code><code># ssl_session_cache shared:SSL:1m;</code>
<code> </code><code># ssl_session_timeout 5m;</code>
<code> </code><code># ssl_ciphers HIGH:!aNULL:!MD5;</code>
<code> </code><code># ssl_prefer_server_ciphers on;</code>
然後我們需要修改預設配置
<code>upstream nginx.ixmsoft.com {</code>
<code> </code><code>server 192.168.5.21:80;</code>
<code> </code><code>server 192.168.5.22:80;</code>
<a href="http://s3.51cto.com/wyfs02/M00/8C/45/wKioL1hojDKgU6gKAAATVzjwqKA256.png" target="_blank"></a>
我們首先配置輪詢的方式
<code># For more information on configuration, see: </code>
<code># * Official English Documentation: http://nginx.org/en/docs/ </code>
<code># * Official Russian Documentation: http://nginx.org/ru/docs/</code>
<code>user nginx; </code>
<code>worker_processes auto; </code>
<code>error_log </code><code>/var/log/nginx/error</code><code>.log; </code>
<code>pid </code><code>/run/nginx</code><code>.pid;</code>
<code># Load dynamic modules. See /usr/share/nginx/README.dynamic. </code>
<code>include </code><code>/usr/share/nginx/modules/</code><code>*.conf;</code>
<code>events { </code>
<code> </code><code>worker_connections 1024; </code>
<code>http { </code>
<code> </code><code>log_format main </code><code>'$remote_addr - $remote_user [$time_local] "$request" '</code>
<code> </code><code>'$status $body_bytes_sent "$http_referer" '</code>
<code> </code><code>'"$http_user_agent" "$http_x_forwarded_for"'</code><code>;</code>
<code> </code><code>access_log </code><code>/var/log/nginx/access</code><code>.log main;</code>
<code> </code><code>sendfile on; </code>
<code> </code><code>tcp_nopush on; </code>
<code> </code><code>tcp_nodelay on; </code>
<code> </code><code>keepalive_timeout 65; </code>
<code> </code><code>types_hash_max_size 2048;</code>
<code> </code><code>include </code><code>/etc/nginx/mime</code><code>.types; </code>
<code> </code><code>default_type application</code><code>/octet-stream</code><code>;</code>
<code>#增加後端伺服器的負載方式,我們預設使用輪詢</code>
<code> </code><code>upstream nginx.ixmsoft.com {</code>
<code> </code>
<code> </code><code># Load modular configuration files from the /etc/nginx/conf.d directory. </code>
<code> </code><code># See http://nginx.org/en/docs/ngx_core_module.html#include </code>
<code> </code><code># for more information. </code>
<code> </code><code>include </code><code>/etc/nginx/conf</code><code>.d/*.conf;</code>
<code> </code><code>server { </code>
<code> </code><code>#listen 80 default_server; </code>
<code> </code><code>#listen [::]:80 default_server; </code>
<code> </code><code>#server_name _; </code>
<code> </code><code>listen 80;</code>
<code> </code><code>server_name http:</code><code>//nginx</code><code>.ixmsoft.com;</code>
<code> </code><code>root </code><code>/usr/share/nginx/html</code><code>;</code>
<code> </code><code># Load configuration files for the default server block. </code>
<code> </code><code>include </code><code>/etc/nginx/default</code><code>.d/*.conf;</code>
<code>#增加的伺服器配置</code>
<code> </code><code>proxy_redirect off;</code>
<code> </code><code>proxy_set_header Host $host;</code>
<code> </code><code>proxy_set_header X-Real-IP $remote_addr;</code>
<code> </code><code>proxy_pass http:</code><code>//nginx</code><code>.ixmsoft.com;</code>
<code> </code><code>error_page 404 </code><code>/404</code><code>.html; </code>
<code> </code><code>location = </code><code>/40x</code><code>.html { </code>
<code> </code><code>error_page 500 502 503 504 </code><code>/50x</code><code>.html; </code>
<code> </code><code>location = </code><code>/50x</code><code>.html { </code>
<code> </code><code>} </code>
<code># Settings for a TLS enabled server. </code>
<code># </code>
<code># server { </code>
<code># listen 443 ssl http2 default_server; </code>
<code># listen [::]:443 ssl http2 default_server; </code>
<code># server_name _; </code>
<code># root /usr/share/nginx/html; </code>
<code># ssl_certificate "/etc/pki/nginx/server.crt"; </code>
<code># ssl_certificate_key "/etc/pki/nginx/private/server.key"; </code>
<code># ssl_session_cache shared:SSL:1m; </code>
<code># ssl_session_timeout 10m; </code>
<code># ssl_ciphers HIGH:!aNULL:!MD5; </code>
<code># ssl_prefer_server_ciphers on; </code>
<code># # Load configuration files for the default server block. </code>
<code># include /etc/nginx/default.d/*.conf; </code>
<code># location / { </code>
<code># } </code>
<code># error_page 404 /404.html; </code>
<code># location = /40x.html { </code>
<code># error_page 500 502 503 504 /50x.html; </code>
<code># location = /50x.html { </code>
<code># }</code>
儲存會重新開機服務
<code>Systemc restart nginx</code>
然後測試通路,通路結果基本上都是一個後端伺服器配置設定一次
<a href="http://s3.51cto.com/wyfs02/M01/8C/45/wKioL1hojDOy6DECAABCz7IBU5w966.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M02/8C/45/wKioL1hojDTADZc2AABLNXtG77I433.png" target="_blank"></a>
我們使用以下指令執行十次
<code>for</code> <code>i </code><code>in</code> <code>$(</code><code>seq</code> <code>10); </code><code>do</code> <code>curl http:</code><code>//192</code><code>.168.5.20 ; </code><code>done</code>
<code>[root@bogon nginx]</code><code># for i in $(seq 10); do curl http://192.168.5.20 ; done </code>
<code>HostName:A-S ----->IP:192.168.5.21</code>
<code>HostName:B-S ----->IP:192.168.5.22</code>
<code>HostName:A-S ----->IP:192.168.5.21 </code>
<a href="http://s3.51cto.com/wyfs02/M00/8C/45/wKioL1hojDXD2cxdAAFMDeUtTXE499.png" target="_blank"></a>
接下來我們配置權重輪詢配置設定方式
我們隻修改一下部分
<code> </code><code>server 192.168.5.21:80 weight=10 max_fails=2 fail_timeout=30s;</code>
<code> </code><code>server 192.168.5.22:80 weight=5 max_fails=2 fail_timeout=30s;</code>
<code># 供proxy_pass和fastcgi_pass指令中使用的代理伺服器</code>
<code># 背景如果有動态應用的時候,ip_hash指令可以通過hash算法</code>
<code># 将用戶端請求定位到同一台後端伺服器上,解決session共享,</code>
<code># 但建議用動态應用做session共享</code>
<code># server用于指定一個後端伺服器的名稱和參數</code>
<code># weight代表權,重預設為1,權重越高被配置設定的用戶端越多</code>
<code># max_fails 指定時間内對後端請求失敗的次數</code>
<code># fail_timeout 達到max_fails指定的失敗次數後暫停的時間</code>
<code># down參數用來标記為離線,不參與負載均衡.在ip_hash下使用</code>
<code># backup僅僅在非backup伺服器當機或繁忙的時候使用</code>
<a href="http://s3.51cto.com/wyfs02/M02/8C/49/wKiom1hojDagFDrYAAAeCJtnChc494.png" target="_blank"></a>
修改後的代碼如下:
<code> </code><code># Load modular configuration files from the /etc/nginx/conf.d directory. </code>
我們重新開機進行測試
<code>[root@bogon nginx]</code><code># for i in $(seq 10); do curl http://192.168.5.20 ; done </code>
<code>HostName:A-S ----->IP:192.168.5.21 </code>
<code>HostName:A-S ----->IP:192.168.5.21 </code>
<a href="http://s3.51cto.com/wyfs02/M01/8C/46/wKioL1hojDej2e0lAABTooCORR8754.png" target="_blank"></a>
接下來我們測試第三種,ip_hash;每個請求按通路ip的hash結果配置設定,這樣每個訪客固定通路一個後端伺服器,可以解決的問題。
我們隻修改server部分
<code> </code><code>upstream nginx.ixmsoft.com { </code>
<code> </code><code>ip_hash; </code>
<a href="http://s3.51cto.com/wyfs02/M02/8C/46/wKioL1hojDiQNjHPAAAU2ne_M5k749.png" target="_blank"></a>
修改後的整體代碼:
儲存退出後,我們測試
<a href="http://s3.51cto.com/wyfs02/M00/8C/49/wKiom1hojDmhjgxKAABQzAY3o8A768.png" target="_blank"></a>
最後我們介紹一下fairl
按後端伺服器的響應時間來配置設定請求,響應時間短的優先配置設定
<code>upstream nginx.ixmsoft.com { </code>
<code> </code><code>server 192.168.5.22:80; </code>
<code> </code><code>fair; </code>
修改後的整體代碼
本文轉自 高文龍 51CTO部落格,原文連結:http://blog.51cto.com/gaowenlong/1887997,如需轉載請自行聯系原作者