天天看點

nginx+upsync+consul實作動态的負載均衡

1、什麼是動态負載均衡

傳統的負載均衡,如果Upstream參數發生變化,每次都需要重新加載nginx.conf檔案,

是以擴充性不是很高,是以我們可以采用動态負載均衡,實作Upstream可配置化、動态化,無需人工重新加載nginx.conf。

這類似分布式的配置中心

2、Consul環境搭建

1.下載下傳consul_0.7.5_linux_amd64.zip

wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip

2.解壓consul_0.7.5_linux_amd64.zip

unzip consul_0.7.5_linux_amd64.zip

如果解壓出現該錯誤

-bash: unzip: 未找到指令

解決辦法

yum -y install unzip

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

[[email protected] soft] ./consul

usage: consul [--version] [--help] <command> [<args>]

Available commands are:

    agent          Runs a Consul agent

    configtest     Validate config file

    event          Fire a new event

    exec           Executes a command on Consul nodes

    force-leave    Forces a member of the cluster to enter the "left" state

    info           Provides debugging information for operators

    join           Tell Consul agent to join cluster

    keygen         Generates a new encryption key

    keyring        Manages gossip layer encryption keys

    kv             Interact with the key-value store

    leave          Gracefully leaves the Consul cluster and shuts down

    lock           Execute a command holding a lock

    maint          Controls node or service maintenance mode

    members        Lists the members of a Consul cluster

    monitor        Stream logs from a Consul agent

    operator       Provides cluster-level tools for Consul operators

    reload         Triggers the agent to reload configuration files

    rtt            Estimates network round trip time between nodes

    snapshot       Saves, restores and inspects snapshots of Consul server state

    version        Prints the Consul version

    watch          Watch for changes in Consul

4.啟動consul

我的linux Ip位址192.168.212.131

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

5.臨時關閉防火牆systemctl stop firewalld

6.浏覽器通路192.168.212.131:8500

nginx+upsync+consul實作動态的負載均衡

7.使用PostMan 注冊Http服務

http://192.168.212.131:8500/v1/catalog/register

參數1

{"Datacenter": "dc1", "Node":"tomcat", "Address":"192.168.5.165","Service": {"Id" :"192.168.5.165:8081", "Service": "itmayeidu","tags": ["dev"], "Port": 8080}}

參數2

{"Datacenter": "dc1", "Node":"tomcat", "Address":"192.168.5.165","Service": {"Id" :"192.168.5.165:8081", "Service": "itmayeidu","tags": ["dev"], "Port": 8081}}

Datacenter指定資料中心,Address指定服務IP,Service.Id指定服務唯一辨別,Service.Service指定服務分組,Service.tags指定服務标簽(如測試環境、預發環境等),Service.Port指定服務端口。

7.發現Http服務

http://172.20.101.111:8500/v1/catalog/service/item_jd_tomcat

3、解壓Nginx

tar -zxvf nginx-1.9.10.tar.gz

4、配置Nginx

groupadd nginx

useradd -g nginx -s /sbin/nologin nginx

mkdir -p /var/tmp/nginx/client/

mkdir -p /usr/local/nginx

5、編譯Nginx

cd nginx-1.9.0

./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.

解決辦法

yum -y install openssl openssl-devel

6、Upstream 動态配置

  ##動态去consul 擷取注冊的真實反向代理位址

   upstream itmayiedu{

        server 127.0.0.1:11111;

        upsync 192.168.212.134:8500/v1/kv/upstreams/itmayiedu upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;

        upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;

    }

    server {

        listen       80;

        server_name  localhost;

        location / {

            proxy_pass http://itmayiedu;

            index  index.html index.htm;

        }

    }

upsync指令指定從consul哪個路徑拉取上遊伺服器配置;upsync_timeout配置從consul拉取上遊伺服器配置的逾時時間;upsync_interval配置從consul拉取上遊伺服器配置的間隔時間;upsync_type指定使用consul配置伺服器;strong_dependency配置nginx在啟動時是否強制依賴配置伺服器,如果配置為on,則拉取配置失敗時nginx啟動同樣失敗。upsync_dump_path指定從consul拉取的上遊伺服器後持久化到的位置,這樣即使consul伺服器出問題了,本地還有一個備份。

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

建立upsync_dump_path

mkdir /usr/local/nginx/conf/servers/

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

啟動consul

臨時關閉防火牆systemctl stop firewalld

我的linux Ip位址192.168.212.131

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

添加nginx  Upstream服務

1.使用linux指令方式發送put請求

curl -X PUT http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

curl -X PUT http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

2.使用postmen 發送put請求

http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081 http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

負載均衡資訊參數

{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}

繼續閱讀