文章目錄
- Nginx負載均衡
- 一、為什么要用負載均衡
- 二、常見的負載均衡
- 三、負載均衡的常用軟體
- 四、負載均衡型別
- 五、Nginx負載均衡配置
- 1、負載均衡配置語法 ngx_http_upstream_module
- 2、環境準備
- 3、準備站點
- 4、負載均衡組態檔
- 5、優化組態檔
- 6、nginx負載均衡常見問題
- 六、nginx負載均衡調度演算法
- 1.輪詢配置方法
- 2.加權輪詢配置方法
- 3.ip_hash的配置方法
- 七、nginx負載均衡狀態
- 1.down狀態配置測驗
- 2.backup狀態配置測驗
- 3.訪問錯誤狀態
- 4.max_conns限制最大連接數狀態配置
- 八、負載均衡nginx健康檢測模塊
Nginx負載均衡
#注意:代理只能代理一臺機器
nginx做代理,一個location可以做一個代理
一、為什么要用負載均衡
當我們的Web服務器直接面向用戶,往往要承載大量并發請求,單臺服務器難以負荷,我使用多臺Web服務器組成集群,前端使用Nginx負載均衡,將請求分散的打到我們的后端服務器集群中,實作負載的分發,那么會大大提升系統的吞吐率、請求性能、高容災
往往我們接觸的最多的是SLB(Server Load Balance)負載均衡,實作最多的也是SLB、那么SLB它的調度節點和服務節點通常是在一個地域里面,那么它在這個小的邏輯地域里面決定了他對部分服務的實時性、回應性是非常好的,
所以說當海量用戶請求過來以后,它同樣是請求調度節點,調度節點將用戶的請求轉發給后端對應的服務節點,服務節點處理完請求后在轉發給調度節點,調度節點最后回應給用戶節點,這樣也能實作一個均衡的作用,那么Nginx則是一個典型的SLB
二、常見的負載均衡
SLB 阿里云負載均衡
LB 青云負載均衡
CLB 騰訊云負載均衡
ULB ucloud負載均衡
#負載均衡的稱呼
負載均衡
負載
Load Balance
LB
三、負載均衡的常用軟體
Nginx #在1.9版本之前只能做七層,1.9版本之后既能做七層,也能做四層負載均衡
Haproxy #既能做四層,也能做七層負載均衡
LVS #只能做四層負載均衡
#LVS是最快的負載均衡軟體,其他兩個軟體需要將請求發送到服務再轉發到后端,LVS不用到服務,它相當于將服務器變成了負載均衡,直接轉發請求
四、負載均衡型別
#四層負載均衡
所謂四層負載均衡指的是OSI七層模型中的傳輸層,那么傳輸層Nginx已經能支持TCP/IP的控制,所以只需要對客戶端的請求進行TCP/IP協議的包轉發就可以實作負載均衡,那么它的好處是性能非常快、只需要底層進行應用處理,而不需要進行一些復雜的邏輯,
#七層負載均衡
七層負載均衡它是在應用層,那么它可以完成很多應用方面的協議請求,比如我們說的http應用的負載均衡,它可以實作http資訊的改寫、頭資訊的改寫、安全應用規則控制、URL匹配規則控制、以及轉發、rewrite等等的規則,所以在應用層的服務里面,我們可以做的內容就更多,那么Nginx則是一個典型的七層負載均衡SLB
#四層負載與七層負載區別
四層負載均衡資料包在底層就進行了分發,而七層負載均衡資料包則是在最頂層進行分發、由此可以看出,七層負載均衡效率沒有四負載均衡高,
但七層負載均衡更貼近于服務,如:http協議就是七層協議,我們可以用Nginx可以作會話保持,URL路徑規則匹配、head頭改寫等等,這些是四層負載均衡無法實作的,
注意:四層負載均衡不識別域名,七層負載均衡識別域名
五、Nginx負載均衡配置
#Nginx要實作負載均衡需要用到proxy_pass代理模塊配置.
Nginx負載均衡與Nginx代理不同地方在于,Nginx的一個location僅能代理一臺服務器,而Nginx負載均衡則是將客戶端請求代理轉發至一組upstream虛擬服務池.
1、負載均衡配置語法 ngx_http_upstream_module
Syntax: upstream name { ... }
Default: —
Context: http
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name www.linux.com
location / {
proxy_pass http://backend;
}
}
2、環境準備
| 主機 | 外網ip | 內網ip | 身份 |
|---|---|---|---|
| lb01 | 10.0.0.4 | 172.16.1.4 | 負載均衡 |
| web01 | 10.0.0.7 | 172.16.1.7 | web |
| web02 | 10.0.0.8 | 172.16.1.8 | web |
3、準備站點
#web01準備
[root@web01 conf.d]# vim node.conf
server {
listen 80;
server_name node.linux.com;
location / {
root /code/node;
inde index.html;
}
}
[root@web01 conf.d]# mkdir /code/node
[root@web01 conf.d]# echo "我是web01......" > /code/node/index.html
[root@web01 conf.d]# systemctl restart nginx
#web02準備
[root@web02 conf.d]# vim node.conf
server {
charset 'utf-8';
listen 80;
server_name node.linux.com;
location / {
root /code/node;
index index.html;
}
}
[root@web02 conf.d]# mkdir /code/node
[root@web02 conf.d]# echo "我是web02......" > /code/node/index.html
[root@web02 conf.d]# systemctl restart nginx
4、負載均衡組態檔
[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.linux.com;
location / {
proxy_pass http://node;
include proxy_params;
}
}
#檢查include檔案是否存在
[root@lb01 conf.d]# ll /etc/nginx/proxy_params
-rw-r--r-- 1 root root 275 Feb 28 15:24 /etc/nginx/proxy_params
[root@lb01 conf.d]# systemctl restart nginx
5、優化組態檔
[root@Nginx ~]# 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;
6、nginx負載均衡常見問題
如果后臺服務連接超時,Nginx是本身是有機制的,如果出現一個節點down掉的時候,Nginx會更據你具體負載均衡的設定,將請求轉移到其他的節點上,但是,如果后臺服務連接沒有down掉,但是回傳錯誤例外碼了如:504、502、500,這個時候你需要加一個負載均衡的設定,
#如下:
proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;
#當其中一臺回傳錯誤碼404,500...等錯誤時,可以分配到下一臺服務器程式繼續處理,提高平臺訪問成功率
#組態檔
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
}
server {
listen 80;
server_name linux.slb.cluster.local.com;
location / {
proxy_pass http://test;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
}
}
六、nginx負載均衡調度演算法
| 調度演算法 | 概述 |
|---|---|
| 輪詢 | 按時間順序逐一分配到不同的后端服務器(默認) |
| weight | 加權輪詢,weight值越大,分配到的訪問幾率越高 |
| ip_hash | 每個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個后端服務器 |
| url_hash | 按照訪問URL的hash結果來分配請求,是每個URL定向到同一個后端服務器 |
| least_conn | 最少鏈接數,那個機器鏈接數少就分發 |
# 加權,權值越小,訪問概率越低,反之則越高
server 172.16.1.7:80 weight=1;
server 172.16.1.8:80 weight=5;
server 172.16.1.9:80 weight=100;
1.輪詢配置方法
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
2.加權輪詢配置方法
#訪問根據配置的權重比例進行分配
upstream node {
server 172.16.1.7:80 weight=5;
server 172.16.1.8:80 weight=1;
}
3.ip_hash的配置方法
#根據訪問的來源IP分配至同一臺服務器
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
ip_hash;
}
#經常使用這種方式進行會話保持
七、nginx負載均衡狀態
| 狀態 | 概述 |
|---|---|
| down | 當前的server暫時不參與負載均衡 |
| backup | 預留的備份服務器 |
| max_fails | 允許請求失敗的次數 |
| fail_timeout | 經過max_fails失敗后, 服務暫停時間 |
| max_conns | 限制最大的接收連接數 |
1.down狀態配置測驗
upstream node {
#不參與負載均衡任何調度,一般停機維護或者上線的時候使用
server 172.16.1.7:80 down;
server 172.16.1.8:80;
}
2.backup狀態配置測驗
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80 backup;
}
3.訪問錯誤狀態
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80 max_fails=3 fail_timeout 10s;
}
4.max_conns限制最大連接數狀態配置
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80 max_conns=120;
}
八、負載均衡nginx健康檢測模塊
#安裝需要的軟體
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
#下載源件包
[root@lb02 ~]#wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@lb02~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@lb02~]# tar xf nginx-1.16.1.tar.gz
[root@lb02~]# unzip master.zip
[root@lb02~]# cd nginx-1.16.1/
# p1:在nginx目錄內 p0不在nginx目錄內
[root@lb02 nginx-1.16.1]# patch -p1 <../nginx_upstream_check_module-master/check_1.16.1+.patch
[root@lb02 nginx-1.16.1]#./configure --prefix=/data/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/data/nginx/nginx.conf --error-log-path=/data/log/nginx/error.log --http-log-path=/data/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.16.1]# make -j && make install
[root@lb02 nginx-1.16.1]# vim /etc/nginx/conf.d/proxy.conf
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
check interval=3000 rise=1 fall=2 timeout=1000;
}
server {
listen 80;
server_name linux.slb.cluster.local.com;
location / {
proxy_pass http://test;
# proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
include proxy_params;
}
location /upstream_check {
check_status;
}
}
interval:檢測間隔
rise:重試測驗
fall:錯誤次數
timeout:超時時間
[root@lb02 nginx]# cat /etc/nginx/conf.d/test.conf
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
check interval=3000 rise=1 fall=2 timeout=1000;
}
server {
listen 80;
server_name linux.slb.cluster.local.com;
location / {
proxy_pass http://test;
# proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
include proxy_params;
}
location /upstream_check {
check_status;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/282301.html
標籤:其他
上一篇:AdminLTE快速入門和使用(網頁模板快速入門使用)
下一篇:微服務架構
