1)為什么要使用負載均衡
當我們的Web服務器直接面向用戶,往往要承載大量並發請求,單台服務器難以負荷。使用多台Web服務器組成集群,前端使用Nginx負載均衡,將請求分散地打到后端服務器集群中,實現負載的分發。可以大大提升系統的吞吐率、請求性能、高容災能力。
往往我們接觸的最多的是SLB(Server Load Balance)
負載均衡,實現最多的也是SLB
,那么SLB
它的調度節點和服務節點通常是在一個地域里面。它在這個小的邏輯地域里面決定了他對部分服務的實時性、響應性是非常好的。
因此,當海量用戶請求過來以后,它同樣是請求調度節點,調度節點將用戶的請求轉發給后端對應的服務節點,服務節點處理完請求后再轉發給調度節點,調度節點最后響應給用戶節點。這樣就能實現一個均衡的作用。Nginx
就是一個典型的SLB
。
2)負載均衡的叫法
2.1)負載均衡的叫法有很多:
負載均衡
負載
Load Balance
LB
2.2)公有雲中叫法有:
阿里雲負載均衡——SLB
青雲負載均衡——QLB
騰訊雲負載均衡——CLB
ucloud負載均衡——ULB
2.3)常見的負載均衡的軟件有:
Nginx、Haproxy、LVS
2.4)負載均衡能實現的應用場景一: 四層負載均衡
所謂四層負載均衡指的是OSI
七層模型中的傳輸層,那么傳輸層Nginx
已經能支持TCP/IP
的控制,所以只需要對客戶端的請求進行TCP/IP
協議的包轉發就可以實現負載均衡,那么它的好處是性能非常快、只需要底層進行應用處理,而不需要進行一些復雜的邏輯。
2.5)負載均衡能實現的應用場景二:七層負載均衡
七層負載均衡是在應用層,它可以完成很多應用方面的協議請求。比如我們說的http
應用的負載均衡,它可以實現http
信息的改寫、頭信息的改寫、安全應用規則控制、URL
匹配規則控制、以及轉發、rewrite
等等的規則,所以在應用層的服務里面,我們可以做的內容就更多,那么Nginx
則是一個典型的七層負載均衡SLB。
2.6)四層負載均衡與七層負載均衡區別
四層負載均衡數據包在底層就進行了分發,而七層負載均衡數據包則是在最頂層進行分發,由此可以看出,七層負載均衡效率沒有四負載均衡高。
但七層負載均衡更貼近於服務,如:http
協議就是七層協議,我們可以用Nginx
可以做會話保持,URL
路徑規則匹配、head
頭改寫等等,這些是四層負載均衡無法實現的。
注意:四層負載均衡不識別域名,七層負載均衡識別域名。
二、Nginx負載均衡配置場景
Nginx
要實現負載均衡需要用到proxy_pass
代理模塊配置。
Nginx
負載均衡與Nginx
代理不同地方在於,Nginx
的一個location
僅能代理一台服務器,而Nginx
負載均衡則是將客戶端請求代理轉發至一組upstream
虛擬服務池。
1)Nginx upstream虛擬配置語法
Syntax: upstream name { ... }
Default: -
Context: http
# upstream例
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
2)Nginx
負載均衡示例
2.1)環境准備
角色 | 外網IP(NAT) | 內網IP(LAN) | 主機名 |
---|---|---|---|
lb01 | eth0:10.0.0.5 | eth1:172.16.1.5 | lb01 |
Web01 | eth0:10.0.0.7 | eth1:172.16.1.7 | web01 |
Web02 | eth0:10.0.0.8 | eth1:172.16.1.8 | web02 |
2.2)Web01
服務器上配置nginx
[root@web01 images]# cd ~
[root@web01 ~]# cd /etc/nginx/conf.d/
#配置nginx
[root@web01 conf.d]# vim node.conf
server{
listen 80;
server_name node.drz.com;
location / {
root /node;
index index.html;
}
}
[root@web01 ~]# cd /node
-bash: cd: /node: No such file or directory
#創建node目錄
[root@web01 ~]# mkdir /node
#新建index.html
[root@web01 ~]# echo "Web01..." > /node/index.html
#重啟nginx
[root@web01 ~]# systemctl restart nginx
=========================================================
2.3)Web02
服務器上配置nginx
[root@web02 ROOT]# cd ~
[root@web02 ~]# cd /etc/nginx/conf.d/
#配置nginx
[root@web02 conf.d]# vim node.conf
server {
listen 80;
server_name node.drz.com;
location / {
root /node;
index index.html;
}
}
[root@web02 conf.d]# cd ~
#創建node目錄
[root@web02 ~]# mkdir /node
#新建index.html
[root@web02 ~]# echo "Web02..." > /node/index.html
#重啟nginx
[root@web02 ~]# systemctl restart nginx
=================================================
2.4)配置Nginx
負載均衡
[root@lb01 code]# cd /etc/nginx/conf.d/
#配置nginx負載均衡
[root@lb01 conf.d]# vim node_proxy.conf
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name node.drz.com;
location / {
proxy_pass http://node;
include proxy_params;
}
}
#准備Nginx負載均衡調度使用的proxy_params
[root@lb01 conf.d]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
window鍵+R——drivers——etc——hosts, 修改內容為 10.0.0.5 node.drz.com
#重啟nginx
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl restart nginx
打開瀏覽器,輸入 node.drz.com
2.5)負載均衡常見典型故障
如果后台服務連接超時,Nginx
是本身是有機制的,如果出現一個節點down掉的時候,Nginx
會更據你具體負載均衡的設置,將請求轉移到其他的節點上,但是,如果后台服務連接沒有down掉,但是返回錯誤異常碼了,如:504、502、500,此時你需要加一個負載均衡的設置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;
意思是,當其中一台返回錯誤碼404,500...等錯誤時,可以分配到下一台服務器程序繼續處理,提高平台訪問成功率。
[root@lb01 conf.d]# vim node_proxy.conf
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name node.drz.com;
location / {
proxy_pass http://node;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
include proxy_params;
}
}
三、Nginx負載均衡調度算法
調度算法 | 概述 |
---|---|
輪詢 | 按時間順序逐一分配到不同的后端服務器(默認) |
weight | 加權輪詢,weight值越大,分配到的訪問幾率越高 |
ip_hash | 每個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個后端服務器 |
url_hash | 按照訪問URL的hash結果來分配請求,是每個URL定向到同一個后端服務器 |
least_conn | 最少鏈接數,那個機器鏈接數少就分發 |
Nginx
負載均衡[rr]權重輪詢具體配置
upstream load_pass {
server 10.0.0.7:80;
server 10.0.0.8:80;
}
=========================================
Nginx
負載均衡[wrr]權重輪詢具體配置
upstream load_pass {
server 10.0.0.7:80 weight=5;
server 10.0.0.8:80;
}
==========================================
Nginx
負載均衡ip_hash
具體配置不能和weight一起使用。
#如果客戶端都走相同代理, 會導致某一台服務器連接過多
upstream load_pass {
ip_hash;
server 10.0.0.7:80 weight=5;
server 10.0.0.8:80;
}
四、Nginx負載均衡后端狀態
后端Web服務器在前端Nginx負載均衡調度中的狀態
狀態 | 概述 |
---|---|
down | 當前的server暫時不參與負載均衡 |
backup | 預留的備份服務器 |
max_fails | 允許請求失敗的次數 |
fail_timeout | 經過max_fails失敗后,服務暫停時間 |
max_conns | 限制最大的接收連接數 |
測試down狀態測試該Server不參與負載均衡的調度
upstream load_pass {
#不參與任何調度, 一般用於停機維護
server 10.0.0.7:80 down;
}
===================================================
測試backup以及down狀態
upstream load_pass {
server 10.0.0.7:80 down;
server 10.0.0.8:80 backup;
server 10.0.0.9:80 max_fails=1 fail_timeout=10s;
}
location / {
proxy_pass http://load_pass;
include proxy_params;
}
====================================================
測試max_fails失敗次數和fail_timeout多少時間內失敗多少次則標記down
upstream load_pass {
server 10.0.0.7:80;
server 10.0.0.8:80 max_fails=2 fail_timeout=10s;
}
=====================================================
測試max_conns最大TCP連接數
upstream load_pass {
server 10.0.0.7:80;
server 10.0.0.8:80 max_conns=1;
}
五、Nginx負載均衡健康檢查
在Nginx官方模塊提供的模塊中,沒有對負載均衡后端節點的健康檢查模塊,但可以使用第三方模塊。 nginx_upstream_check_module來檢測后端服務的健康狀態。
1)安裝依賴包
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2)下載nginx源碼包以及nginx_upstream_check模塊第三方模塊
[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
3)解壓nginx源碼包以及第三方模塊
[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb02 ~]# unzip master.zip
4)進入nginx目錄,打補丁(nginx的版本是1.14補丁就選擇1.14的,p1代表在nginx目錄,p0是不在nginx目錄)
[root@lb02 ~]# cd nginx-1.14.2/
[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@lb02 nginx-1.14.2]# make && make install
5)在已有的負載均衡上增加健康檢查的功能
[root@lb01 conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
#interval 檢測間隔時間,單位為毫秒
#rise 表示請求2次正常,標記此后端的狀態為up
#fall 表示請求3次失敗,標記此后端的狀態為down
#type 類型為tcp
#timeout 超時時間,單位為毫秒
}
server {
listen 80;
server_name web.drz.com;
location / {
proxy_pass http://web;
include proxy_params;
}
location /upstream_check {
check_status;
}
}