Linux架構之Nginx 七層負載均衡

第50章 Nginx七層負載均衡

1、Nginx負載均衡基本概述

1)爲何要使用負載均衡

當咱們的Web服務器直接面向用戶,每每要承載大量併發請求,單臺服務器難以負荷。使用多臺Web服務器組成集羣,前端使用Nginx負載均衡,將請求分散地打到後端服務器集羣中,實現負載的分發。能夠大大提高系統的吞吐率、請求性能、高容災能力。html

 

每每咱們接觸的最多的是SLB(Server Load Balance)負載均衡,實現最多的也是SLB,那麼SLB它的調度節點和服務節點一般是在一個地域裏面。它在這個小的邏輯地域裏面決定了他對部分服務的實時性、響應性是很是好的。前端

所以,當海量用戶請求過來之後,它一樣是請求調度節點,調度節點將用戶的請求轉發給後端對應的服務節點,服務節點處理完請求後再轉發給調度節點,調度節點最後響應給用戶節點。這樣就能實現一個均衡的做用。Nginx就是一個典型的SLBnode

2)負載均衡的叫法

2.1)負載均衡的叫法有不少:

負載均衡linux

負載nginx

Load Balancec++

LBgit

 

2.2)公有云中叫法有:

阿里雲負載均衡——SLBgithub

青雲負載均衡——QLBweb

騰訊雲負載均衡——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頭改寫等等,這些是四層負載均衡沒法實現的。

注意:四層負載均衡不識別域名,七層負載均衡識別域名。

 

2、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掉,可是返回錯誤異常碼了,如:50四、50二、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;
  }
}

 

3、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;
}

 

 

4、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;
}

 

5、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;
  }
}
相關文章
相關標籤/搜索