天天看點

Linux下玩轉nginx系列(八)---如何使用upsync子產品實作動态負載均衡

一、HTTP動态負載均衡

動态負載均衡

動态負載均衡政策類似于權重輪詢政策,可以通過對于後端伺服器叢集的狀态監測,量化不同伺服器的性能差異,來周期性調整伺服器的比重來實作權重的動态調整。

在nginx中傳統的負載均衡,如果Upstream參數發生變化,每次都需要重新加載nginx.conf檔案,是以擴充性不是很高,是以我們可以采用動态負載均衡,實作Upstream可配置化、動态化,無需人工重新加載nginx.conf。這類似分布式的配置中心。

動态負載均衡實作方案

  • Consul+Consul-template 每次發現配置更改需要raload nginx,重新開機Nginx。
  • Consul+OpenResty 實作無需raload動态負載均衡。
  • Consul+upsync+Nginx 實作無需raload動态負載均衡。

常用伺服器注冊與發現架構

1.常見服務發現架構 Consul、Eureka、ZooKeeper以及Etcd,ZooKeeper是這種類型的項目中曆史最悠久的之一,它起源于Hadoop。它非常成熟、可靠,被許多大公司(YouTube、eBay、雅虎等)使用。

2.Consul 是 HashiCorp 公司推出的開源工具,用于實作分布式系統的服務發現與配置。Consul 使用 Go 語言編寫,是以具有天然可移植性(支援Linux、windows和Mac OS X)。Consul采用主從模式的設計,使得叢集的數量可以大規模擴充,叢集間通過RPC的方式調用(HTTP和DNS)。Consul 内置了服務注冊與發現架構、分布一緻性協定實作、健康檢查、Key/Value 存儲、多資料中心方案,不再需要依賴其他工具(比如 ZooKeeper 等),使用起來也較為簡單。

3.etcd是一個采用HTTP協定的健/值對存儲系統,它是一個分布式和功能層次配置系統,可用于建構服務發現系統。其很容易部署、安裝和使用,提供了可靠的資料持久化特性。它是安全的并且文檔也十分齊全。

對于nginx下要實作動态負載均衡,目前有三個選擇方式:

1.Tengine 的Dyups子產品。

2.微網誌的Upsync+Consul 實作動态負載均衡。

3.OpenResty的balancer_by_lua(又拍雲使用其開源的slardar(Consul balancer_by_lua))。

文章下面介紹怎麼使用upsync子產品和consul來實作nginx的動态負載均衡。

二、CONSUL快速入門

Consul是一款開源的分布式服務注冊與發現系統,通過HTTP API可以使得服務注冊、發現實作起來非常簡單,它支援如下特性。

  • 服務注冊:服務實作者可以通過HTTP API或DNS方式,将服務注冊到Consul。
  • 服務發現:服務消費者可以通過HTTP API或DNS方式,從Consul擷取服務的IP和PORT。故障檢測:支援如TCP、HTTP等方式的健康檢查機制,進而當服務有故障時自動摘除。
  • K/V存儲:使用K/V存儲實作動态配置中心,其使用HTTP長輪詢實作變更觸發和配置更改。
  • 多資料中心:支援多資料中心,可以按照資料中心注冊和發現服務,即支援隻消費本地機房服務,使用多資料中心叢集還可以避免單資料中心的單點故障。
  • Raft算法:Consul使用Raft算法實作叢集資料一緻性。
  • 通過Consul可以管理服務注冊與發現,接下來需要有一個與Nginx部署在同一台機器的Agent來實作Nginx配置更改和Nginx重新開機功能。我們有Confd或者Consul-template兩個選擇,而Consul-template是Consul官方提供的,我們就選擇它了。其使用HTTP長輪詢實作變更觸發和配置更改(使用Consul的watch指令實作)。也就是說,我們使用Consul-template實作配置模闆,然後拉取Consul配置渲染模闆來生成Nginx實際配置。

CONSUL環境搭建

1.下載下傳consul_1.13.1_linux_amd64.zip

wget https://releases.hashicorp.com/consul/1.13.1/consul_1.13.1_linux_amd64.zip
           

2.解壓consul_0.7.5_linux_amd64.zip

unzip consul_0.7.5_linux_amd64.zip
#如果解壓出現該錯誤
-bash: unzip: #未找到指令
#centos解決辦法
yum -y install unzip
#ubuntu解決辦法
apt-get -y install unzip
           

3.執行 ./consul 出現以下資訊就說明安裝成功

Linux下玩轉nginx系列(八)---如何使用upsync子產品實作動态負載均衡

4.啟動consul ,我的linux ip位址192.168.199.104

./consul agent -dev -ui -node=consul-dev -client=192.168.199.104
           

5.臨時關閉防火牆

systemctl stop firewalld
           

6.浏覽器通路192.168.199.104:8500,會出現如下頁面

Linux下玩轉nginx系列(八)---如何使用upsync子產品實作動态負載均衡

7.使用PostMan 注冊HTTP服務

//此處ip位址為ConsulServer所在伺服器的位址
//http://192.168.199.104:8500/v1/catalog/register/
//參數1
{
    "Datacenter": "dc1",
    "Node": "go-http",
    "Address": "192.168.199.104", //本地位址
    "Service": {
        "Id": "192.168.199.104:8080", # 8080
        "Service": "helloword",
        "tags": [
            "dev"
        ],
        "Port": 8080
    }
}
//參數2
{
    "Datacenter": "dc1",
    "Node": "go-http",
    "Address": "192.168.199.104",
    "Service": {
        "Id": "192.168.199.104:8081",//8081
        "Service": "helloword",
        "tags": [
            "dev"
        ],
        "Port": 8081
    }
}
           

Datacenter指定資料中心,

Address指定服務IP,

Service.Id指定服務唯一辨別,

Service.Service指定服務分組,

Service.tags指定服務标簽(如測試環境、預發環境等),

Service.Port指定服務端口。

8.發現HTTP服務

http://192.168.199.104:8500/v1/catalog/service/helloword
           

三、NGINX-UPSYNC-MODULE

注意:清除之前Nginx環境,重新安裝。

NGINX-UPSYNC-MODULE簡介

Upsync是新浪微網誌開源的基于Nginx實作動态配置的三方子產品。Nginx-Upsync-Module的功能是拉取Consul的後端server的清單,并動态更新Nginx的路由資訊。此子產品不依賴于任何第三方子產品。Consul作為Nginx的DB,利用Consul的KV服務,每個Nginx Work程序獨立的去拉取各個upstream的配置,并更新各自的路由。

NGINX-UPSYNC-MODULE安裝及配置整個流程

安裝Nginx

wget http://nginx.org/download/nginx-1.23.1.tar.gz
#作用:實作反向代理、負載負載庫
           

安裝consul

wget https://releases.hashicorp.com/consul/1.13.1/consul_1.13.1_linux_amd64.zip
#作用:對動态負載均衡均配置實作注冊
           

安裝nginx-upsync-module

wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip
#作用:nginx動态擷取最新upstream資訊
#解壓安裝
unzip master.zip
unzip consul_1.13.1_linux_amd64.zip
#如果解壓出現該錯誤
-bash: unzip: 未找到指令
#centos解決辦法
yum -y install unzip
#ubuntu解決辦法
apt-get -y install unzip

#安裝Nginx
#解壓Nginx
tar -zxvf nginx-1.23.1.tar.gz
#配置Nginx
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx
           

編譯Nginx

cd nginx-1.23.1
./configure   --prefix=/usr/local/nginx   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --with-http_realip_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre --add-module=../nginx-upsync-module-master
make && make install

編譯的是報錯
./configure: error: SSL modules require the OpenSSL library.
centos解決辦法
yum -y install openssl openssl-devel
ubuntu解決辦法
apt-get -y install openssl openssl-devel
           

建立upsync_dump_path

mkdir /usr/local/nginx/conf/servers/
#upsync_dump_path指定從consul拉取的上遊伺服器後持久化到的位置,這樣即使consul伺服器出問題了,本地還有一個備份。
           

Upstream 動态配置

http {
    upstream helloworld{
        server 127.0.0.1:1111;
        #連接配接Consul Server 動态擷取 配置負載均衡資訊,間隔0.5s擷取資訊
        upsync 192.168.199.104:8500/v1/kv/upstreams/helloworld upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
        #動态的拉去ConsulServer 相關負載均衡資訊持久化到硬碟上
        upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://helloword;
            index  index.html index.htm;
        }
    }
}
           

1.upsync指令指定從consul哪個路徑拉取上遊伺服器配置;

upsync_timeout配置從consul拉取上遊伺服器配置的逾時時間;upsync_interval配置從consul拉取上遊伺服器配置的間隔時間;upsync_type指定使用consul配置伺服器;

strong_dependency配置nginx在啟動時是否強制依賴配置伺服器,如果配置為on,則拉取配置失敗時nginx啟動同樣失敗。

upsync_dump_path指定從consul拉取的上遊伺服器後持久化到的位置,這樣即使consul伺服器出問題了,本地還有一個備份。

2.注意:替換 consul 注冊中心位址

啟動consul

臨時關閉防火牆systemctl stop firewalld
我的linux Ip位址192.168.199.104
    ./consul agent -dev -ui -node=consul-dev -client=192.168.199.104
           

添加nginx Upstream服務

1.使用linux指令方式發送put請求
curl -X PUT http://192.168.199.104:8500/v1/kv/upstreams/helloworld/192.168.199.104:8080
curl -X PUT http://192.168.199.104:8500/v1/kv/upstreams/helloworld/192.168.199.104:8081
2.使用postmen 發送put請求
http://192.168.199.104:8500/v1/kv/upstreams/helloworld/192.168.199.104:8080 http://192.168.199.104:8500/v1/kv/upstreams/helloworld/192.168.199.104:8081
負載均衡資訊參數
{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}
           

啟動Nginx

參考文檔

nginx-proxy_pass官網

Full Example Configuration

nginx-upsync-module

【NGINX】基于CONSUL+UPSYNC+NGINX實作動态負載均衡