傳統的負載均衡,若是Upstream參數發生變化,每次都須要從新加載nginx.conf文件,所以擴展性不是很高,因此咱們能夠採用動態負載均衡,實現Upstream可配置化、動態化,無需人工從新加載nginx.conf,相似分佈式的配置中心html
https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip
yum -y install unzip
unzip consul_0.7.5_linux_amd64.zip
[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
Upsync是新浪微博開源的基於Nginx實現動態配置的三方模塊。Nginx-Upsync-Module的功能是拉取Consul的後端server的列表,並動態更新Nginx的路由信息。此模塊不依賴於任何第三方模塊。Consul做爲Nginx的DB,利用Consul的KV服務,每一個Nginx Work進程獨立的去拉取各個upstream的配置,並更新各自的路由。
nginx-upsync-module:linux
wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip
upsync_dump_path指定從consul拉取的上游服務器後持久化到的位置,這樣即便consul服務器出問題了,本地還有一個備份。nginx
負載均衡信息參數
{"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服務器出問題了,本地還有一個備份。後端