一、目標
1、Nginx實現負載均衡
2、consul-template動態維護Nginx里面的server
3、consul-template監控Consul集群
4、每個服務配置Consul做服務發現
5、最終目的,當服務(Consul)Down機時;Nginx中的Server被Consul-template實時刪除,並重新加載(Nginx -s reload)配置文件
二、主要用到的工具
1、功能實現:Nginx、 Consul、 Consul-template
2、配合工具:docker(用來搭建Consul群集 、 提供服務)
三、原理
1、Nginx自身的負載均衡功能
2、consul-template的Config功能實時監控Consul集群的節點
3、實時的把consul節點的信息替換到Nginx配置文件、重新加載配置文件
四、配置步驟
第一步:搭建consul集群
第二步:部署服務consul客戶端(consul + webServer)使用config指定服務IP & Port
第三步:部署Nginx
第四步:搭建consul-template監聽consul客戶端的config文件
第五步:consul-template綁定Nginx配置文件
五、過程
方案一:
寫在前面:因為要使用多個單獨的服務,使用Docker來做隔離
1、consul服務端集群3個節點
2、consul客戶端(service)2個節點
3、Nginx + consul-template部署到同一個主機(本機)
很多Dockers的配置,先不記錄。
------------------------------------------------------------------------------------
方案二:
使用物理機
192.168.102.134 本機(Windows物理機)
192.168.102.207 CentOS(本機上的虛擬機)
192.168.102.234 CentOS(物理機)
192.168.102.134 作為Leader
192.168.102.207 作為Client(consul + tomcat)
192.168.102.234 作為consul-template + nginx服務
具體配置如下:
【leader 192.168.102.134】
因為這個是Windows不適合做服務(主要原因:服務檢測check沒找到實現辦法)
consul.exe agent -server -node leader -dc dc1 -data-dir c:\tmp -ui -client 0.0.0.0 -bind=192.168.102.134 -advertise 192.168.102.134 -bootstrap-expect 1
【consul client 192.168.102.207】
1、服務部署:tomcat 8080
啟動一個Tomcat服務(任意內容)作為服務
2、Config文件,服務檢查 check : curl localhost:8080
{"service": {"name": "web", "tags": ["fyh"], "port": 8080, "check": {"script": "curl localhost:8080 >/dev/null 2>&1", "interval": "10s"}}}
3、啟動consul並join到leader下
./consul agent -data-dir /opt/consulData -config-dir /opt/consulConfig/ -advertise 192.168.102.207 -join 192.168.102.134
【consul-template 192.168.102.234】
1、Nginx配置
(1)默認Nginx.conf中增加include ***/test.conf(曲線救國,直接映射到nginx.conf中不生效,include中間文件方法在服務全掉的時候無法reload成功,因為upstream中server為空)
(2)test.conf內容為空(等待Template寫入)
(3)要求安裝nginx正確,支持./nginx -s reload命令進行重新加載
2、Template配置
(1)Template文件內容(/opt/consulTemplate/test.ctmpl)
upstream web { ip_hash; # Refer: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream # least_conn; # least_time; {{range service "web"}} server {{.Address}}:{{.Port}} fail_timeout=0; {{end}} server 127.0.0.1 fail_timeout=0; keepalive 64; } server { listen 80; server_name ipaddress; location / { client_max_body_size 0; proxy_connect_timeout 300s; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 32k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_redirect off; proxy_hide_header Vary; proxy_set_header Accept-Encoding ''; proxy_set_header Host $host; proxy_set_header Referer $http_referer; proxy_set_header Cookie $http_cookie; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400; proxy_pass http://web; } }
(2)啟動consul-template
nohup ./consul-template -consul 192.168.102.134:8500 -template /opt/consulTemplate/test.ctmpl:/usr/local/nginx/conf/conf.d/test.conf:"/usr/local/nginx/sbin/nginx -s reload" 2>&1 >/opt/consul-template.log &
六、驗證
方案一驗證:
查看template中獲取到的service 數量
停止web服務,consul的Config中配置的check是否能發現並把consul的服務狀態置為高危不可用(critical)
直接kill consul進程,查看service數量
最終還是查看最上層的服務提供nginx的服務狀態
方案二驗證:
方案二只有一個consul客戶端
驗證兩個環境搭建后是否可用
tomcat服務停止時是Consul狀態是否為critical,重啟tomcat服務后,服務正常
停止consul客戶端查看狀態
其中Consul服務端暫時未搭建集群未進行測試。
七、附錄
[consul服務端]:
./consul agent -server -data-dir /tmp/data -ui -client 0.0.0.0 -advertise 192.168.102.134 -bootstrap-expect 1
[consul客戶端]:
nohup ./consul agent -config-dir=/usr/local/config -join 192.168.102.134 -data-dir /data 2>&1 &
[consul-template]:
./consul-template -consul 192.168.102.134:8500 -template /usr/local/nginx/conf/myserver.conf.d/test.ctmpl:/usr/local/nginx/conf/nginx.conf:"/usr/local/nginx/sbin/nginx -s reload" -dry
[Consul查詢]:
http://192.168.102.207:8500/v1/catalog/service/web
http://192.168.102.207:8500/v1/catalog/nodes
http://192.168.102.207:8500/v1/kv/key2?flags=42 設置key2=42
http://192.168.102.207:8500/v1/kv/key2 查詢kv值