nginx動態負載均衡

方案

傳統的負載均衡,若是Upstream參數發生變化,每次都須要從新加載nginx.conf文件,所以擴展性不是很高,因此咱們能夠採用動態負載均衡,實現Upstream可配置化、動態化,無需人工從新加載nginx.conf,相似分佈式的配置中心html

  1. Consul+Consul-template 每次發現配置更改須要raload nginx,重啓Nginx。
  2. Consul+OpenResty 實現無需raload動態負載均衡
  3. Consul+upsync+Nginx 實現無需raload動態負載均衡

Consul+upsync+Nginx

Consul環境搭建

  1. https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip
  2. yum -y install unzip
  3. unzip consul_0.7.5_linux_amd64.zip
  4. ./consul 出現下面提示說明成功
[root@localhost local]# ./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

5.啓動consul
./consul agent -dev -ui -node=consul-dev -client=192.168.102.129node

nginx-upsync-module

Upsync是新浪微博開源的基於Nginx實現動態配置的三方模塊。Nginx-Upsync-Module的功能是拉取Consul的後端server的列表,並動態更新Nginx的路由信息。此模塊不依賴於任何第三方模塊。Consul做爲Nginx的DB,利用Consul的KV服務,每一個Nginx Work進程獨立的去拉取各個upstream的配置,並更新各自的路由。
nginx-upsync-module:linux

  1. cd /usr/local
  2. wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip
  3. unzip master.zip
  4. unzip consul_0.7.1_linux_amd64.zip

配置Nginx

  1. tar -zxvf nginx-1.9.9.tar.gz
  2. groupadd nginx
  3. useradd -g nginx -s /sbin/nologin nginx
  4. mkdir -p /var/tmp/nginx/client/
  5. mkdir -p /usr/local/nginx
  6. cd nginx-1.9.9/
  7. ./configure --prefix=/usr/local/nginx --user=nginx --group=root --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
  8. make && make install

動態負載均衡

  1. 建立upsync_dump_pathmkdir /usr/local/nginx/conf/servers/

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

  1. 添加nginx Upstream服務

負載均衡信息參數
{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}git

Upstream 動態配置
##動態去consul 獲取註冊的真實反向代理地址
upstream tmpup{github

server 127.0.0.1:11111;
    upsync 192.168.212.134:8500/v1/kv/upstreams/tmp 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://tmpup;
        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服務器出問題了,本地還有一個備份。後端

相關文章
相關標籤/搜索