nginx應用:使用nginx進行負載平衡

不言
發布: 2023-03-29 08:40:02
原創
1837 人瀏覽過

這篇文章主要介紹了關於nginx應用:使用nginx進行負載平衡,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

nginx應用:使用nginx進行負載平衡
nginx一般可以用於七層的負載平衡,這篇文章將介紹一些負載平衡的基本知識以及使用nginx進行負載平衡的簡單的例子。

四層負載平衡vs 七層負載平衡

常常會說七層負載平衡還是四層負載平衡,其實是根據ISO的OSI網路模型的所在層的叫法而決定的,nginx因為在使用http協定在應用層進行負載平衡的操作,所以稱為七層負載平衡。而諸如LVS在TCP層進行負載平衡操作的則稱為四層負載平衡。一般來說,有以下層的負載平衡分類:

類別 #OSI模型層 說明
#二層負載平衡 MAC層 根據MAC位址回應
三層負載均衡 IP層 根據IP位址回應
#四層負載平衡 TCP層 在IP位址的基礎上結合連接埠號碼進行回應
七層負載平衡 HTTP層 在四層的基礎上,可繼續根據URL/瀏覽器類別等七層的資訊進行進一步的回應

#常見軟體的支援

##輕量級實作支援http和mail,效能與haproxy相近haproxy-支援七層負載平衡LVS支援四層負載平衡,實作較重-F5硬體實現,成本高-
軟體 四層負載平衡 七層負載平衡
#nginx
常見的負載平衡演算法

負載平衡常見有下列幾種演算法:

負載平衡演算法負載平衡演算法(E)nginx支援與否說明#適用場景普通輪詢Round Robin支援權重相同的輪詢適用於外部服務請求和內部伺服器都相對均衡的場景權重輪詢Weighted Round Robin支援(weight) 可以設定不同權重進行輪詢伺服器的處理能力不同,或則希望進行流量的控制,例如Canary Release隨機均衡Random-隨機分配給伺服器外部和內部都非常均衡的場合,或者需要隨機的分配的需求較強權重隨機Weighted Random-結合權重隨機分配給伺服器可結合權重調節隨機策略,更好地適應現實中分佈狀況響應速度Response Time支持(fair)根據伺服器的回應速度進行分配伺服器效能和伺服器目前運作狀況的結合,此種策略能動態的調整狀態,避免能者已經不能的情況下仍然被大量分配作業最少連接Least Connection根據連接的數量進行分配輪詢做的是分配任務,由於實際情況中無法控制輪訓分配的任務,但無法確認任務完成的速度,會導致反映真實伺服器負荷的連線數產生不同,適合於長時間提供長連線服務的業務,例如網路上的客服的WebSocket的實現,或是FTP/SFTP等服務。 DNS回應Flash DNS-根據最快傳回的DNS解析結果來繼續請求服務,忽略其他DNS回傳的IP位址適用於具有全域負載平衡的情況下,例如CDN

負載平衡演示實例:普通輪詢

接下來使用nginx來示範如何進行普通輪詢:

負載平衡演算法#負載平衡演算法(E)nginx支援與否說明適用場景普通輪詢Round Robin支援權重相同的輪詢適用於外部服務請求和內部伺服器都相對均衡的場景

事前準備

事前在7001/7002兩個連接埠分別啟動兩個服務,用於顯示不同訊息,為了演示方便,使用tornado做了一個鏡像,透過docker容器啟動時傳遞的參數不同用於顯示服務的不同。

[root@kong ~]# docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7001"ddba0abd24524d270a782c3fab907f6a35c0ce514eec3159357bded09022ee57
[root@kong ~]# docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7002"95deadd795e19f675891bfcd44e5ea622c95615a95655d1fd346351eca707951
[root@kong ~]# [root@kong ~]# curl http://192.168.163.117:7001Hello, Service :User Service 1: 7001[root@kong ~]# [root@kong ~]# curl http://192.168.163.117:7002Hello, Service :User Service 1: 7002[root@kong ~]#
登入後複製

啟動nginx

[root@kong ~]# docker run -p 9080:80 --name nginx-lb -d nginx 9d53c7e9a45ef93e7848eb3f4e51c2652a49681e83bda6337c89a3cf2f379c74
[root@kong ~]# docker ps |grep nginx-lb9d53c7e9a45e        nginx                      "nginx -g 'daemon ..."   11 seconds ago      Up 10 seconds       0.0.0.0:9080->80/tcp                                                 nginx-lb
[root@kong ~]#
登入後複製

nginx程式碼段

#準備如下nginx程式碼段將其新增至nginx的/etc/nginx/conf.d/default.conf

http {
upstream nginx_lb {    server 192.168.163.117:7001;    server 192.168.163.117:7002;
}server {
    listen       80;
    server_name  www.liumiao.cn 192.168.163.117;
    location / {
        proxy_pass http://nginx_lb;
    }

}
登入後複製

修改default.conf的方法

可以透過在容器中安裝vim達到效果,也可以在本地修改然後透過docker cp傳入,或者直接sed修改都可以。若在容器中安裝vim,使用下列方式即可

[root@kong ~]# docker exec -it nginx-lb sh# apt-get update...省略# apt-get install vim...省略
登入後複製

修改前

# cat default.confserver {
    listen       80;
    server_name  localhost;    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;        index  index.html index.htm;
    }    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}}#
登入後複製

已修改後

# cat default.confupstream nginx_lb {    server 192.168.163.117:7001;    server 192.168.163.117:7002;
}server {
    listen       80;
    server_name  www.liumiao.cn 192.168.163.117;    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
        proxy_pass http://nginx_lb;
    }    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}}#
登入後複製

重啟nginx容器

[root@kong ~]# docker restart nginx-lbnginx-lb
[root@kong ~]#
登入後複製
登入後複製

#確認結果

可以清楚地看到按照順序,進行輪詢:

[root@kong ~]# curl http://localhost:9080Hello, Service :User Service 1: 7001[root@kong ~]# curl http://localhost:9080Hello, Service :User Service 1: 7002[root@kong ~]# curl http://localhost:9080Hello, Service :User Service 1: 7001[root@kong ~]# curl http://localhost:9080Hello, Service :User Service 1: 7002[root@kong ~]#
登入後複製

負載平衡示範實例:權重輪詢

而在此基礎上,進行權重輪詢只需要加上weight即可

負載平衡演算法負載平衡演算法(E)nginx支援與否說明適用場景
權重輪詢Weighted Round Robin支援(weight) 可以設定不同權重進行輪詢伺服器的處理能力不同,或則希望進行流量的控制,例如Canary Release

修改default.conf

依照以下修改default.conf

# cp default.conf default.conf.org
# vi default.conf
# diff default.conf default.conf.org
2,3c2,3
<     server 192.168.163.117:7001 weight=100;<     server 192.168.163.117:7002 weight=200;
--->     server 192.168.163.117:7001;
>     server 192.168.163.117:7002;
#
登入後複製

重啟nginx容器

[root@kong ~]# docker restart nginx-lbnginx-lb
[root@kong ~]#
登入後複製
登入後複製

確認結果

可以看到輪詢結果依照1/3和2/3的比重在進行了:

[root@kong ~]# curl http://localhost:9080Hello, Service :User Service 1: 7001[root@kong ~]# curl http://localhost:9080Hello, Service :User Service 1: 7002[root@kong ~]# curl http://localhost:9080Hello, Service :User Service 1: 7002[root@kong ~]#
登入後複製

相關建議:

nginx管理設定最佳化

Nginx反向代理websocket配置實例​​

以上是nginx應用:使用nginx進行負載平衡的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!