Nginx-負載均衡

Nginx負載均衡:

Nginx核心功能之一可以當做負載均衡器使用!
ngx_http_upstream_module模塊 

基本用法:
upstream name { 
    server1 
    server2
} 
官網說明:
Syntax: upstream name { ... }
Default:    —
Context:http

location ~* \.(jsp|do)$ {
    proxy_pass http://name;
}

nginx主機:192.168.23.48
後端主機:
RS1:192.168.23.36
RS2:192.168.23.37

具體實現:
http語句塊中設置:
upstream apache {
    server 192.168.23.36:80;
    server 192.168.23.37:80;
}

虛擬主機調用:
server {
    server_name www.a.com;
    listen 80;
    root /web/a.com;
    index index.html;
    server_tokens off;
    access_log /web/a.com/a.com.log test;
    location / {
        proxy_pass http://apache;
    }
}

測試結果:
[root@www21:45:46nginx]#for i in {1..1000};do curl www.a.com ; sleep 1;done
<h1>test RS1 server</h1>
<h1>test RS2 server</h1>

屬性設置:  
weight=number  權重,默認爲1  
max_conns    連接後端報務器最大併發活動連接數,1.11.5後支持  
max_fails=number 失敗嘗試最大次數;超出此處指定的次數時
server將被標 記爲不可用,默認爲1  
fail_timeout=time  後端服務器標記爲不可用狀態的連接超時時長,默認10s  
backup 將服務器標記爲"備用",即所有服務器均不可用時才啓用  
down  標記爲"不可用",實現灰度發佈 

測試屬性使用:
upstream apache {
    server 192.168.23.36:80 down;
    server 192.168.23.37:80 max_conns=1000;
    server 127.0.0.1:80 backup;
}

測試結果:
1 198.168.23.36的頁面不會出現:
[root@www21:51:31nginx]#for i in {1..1000};do curl www.a.com ; sleep 1;done
<h1>test RS2 server</h1>
<h1>test RS2 server</h1>
<h1>test RS2 server</h1>
2 所有主機都宕機之後,自動自用backup主機:
3 自帶健康性檢查,後端主機重新上線會立刻停止使用
backup
4 當併發量超過1000時,訪問會失敗!
root@www21:54:56nginx]#ab -c 1001 -n 10000 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.a.com (be patient)
Completed 1000 requests
apr_socket_recv: Connection reset by peer (104)
Total of 1843 requests completed

調度算法:
wrr 加權重的輪詢
ip_hash 源地址hash調度方法
least_conn 最少連接調度算法
hash key [consistent] 基於指定的key的hash表來實現對請求的調度
作用:將請求分類,同一類請求將發往同一個upstream server
使用 consistent參數將使用ketama一致性hash算法適用於後端是Cache服務器 
[如varnish]時使用  
hash $request_uri consistent;  
一致性哈希算法:
hash(ip1)/2^32取模
會有哈希環偏移問題

需要使用權重儘量避免[權重要儘量調大]
權重大hash(ip1+[權重個]隨機數)

hash $remote_addr;

keepalive 連接數N
爲每個worker進程保留的空閒的長連接數量
可節約nginx端口,並減少連接管理的消耗 

測試一致性哈希算法:
配置:
hash $request_uri consistent;
測試:
[root@www22:00:26nginx]#for i in {1..1000};do curl www.a.com/test1.html ; sleep 1;done
Test RS1 page1
Test RS1 page1
^C
[root@www22:00:45nginx]#for i in {1..1000};do curl www.a.com/test2.html ; sleep 1;done
Test RS1 page2
Test RS1 page2
^C
[root@www22:01:04nginx]#for i in {1..1000};do curl www.a.com/test3.html ; sleep 1;done
Test RS1 page3
Test RS1 page3
^C
[root@www22:01:09nginx]#for i in {1..1000};do curl www.a.com/test4.html ; sleep 1;done
Test RS1 page4
Test RS1 page4
^C
[root@www22:01:14nginx]#for i in {1..1000};do curl www.a.com/test5.html ; sleep 1;done
Test RS2 page5
Test RS2 page5
結果證明使用同一個url訪問會調度到同一個後端主機

相關企業版參數:

health_check [parameters];  
健康狀態檢測機制;只能用於location上下文[企業版專用]  

常用參數:  
interval=time檢測的頻率,默認爲5秒  
fails=number:判定服務器不可用的失敗檢測次數;默認爲1次  
passes=number:判定服務器可用的失敗檢測次數;默認爲1次  
uri=uri:做健康狀態檢測測試的目標uri;默認爲/  
match=NAME:健康狀態檢測的結果評估調用此處指定的match配置塊 

match name { 
    ... 
}  
對backend server做健康狀態檢測時,定義其結果判斷機制;

只能用於http 上下文 

常用的參數:[企業版專用]  
status  code[  code ...]: 期望的響應狀態碼  
header  HEADER[operator  value]:期望存在響應首部,
也可對期望的響 應首部的值基於比較操作符和值進行比較  
body:期望響應報文的主體部分應該有的內容 

Nginx模擬4層調度:

ngx_stream_core_module模塊 
模擬反代基於tcp或udp的服務連接,即工作於傳輸層的反代或調度器
官網說明:
Syntax: stream { ... }
Default:—
Context:main

具體使用:
stream {  
    upstream mysqlsrvs {   
        server 192.168.23.36:3306;    
        server 192.168.23.37:3306;    
    }  
    server {   
        listen 3306;   
        proxy_pass mysqlsrvs;  
    } 

}

測試結果:
第一次訪問:
[root@www22:27:01nginx]#mysql -uyl -p[密碼] -h 172.20.23.48
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linux2             |--區別看這裏
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

第二次訪問:
[root@www22:28:17nginx]#mysql -uyl -p[密碼] -h 172.20.23.48
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linux1             |--區別看這裏
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章