11lxm 发表于 2017-12-23 12:36:53

consul-template + nginx部署高可用负载均衡

  一、目标
  1、Nginx实现负载均衡
  2、consul-template动态维护Nginx里面的server
  3、consul-template监控Consul集群
  4、每个服务配置Consul做服务发现

  5、最终目的,当服务(Consul)Down机时;Nginx中的Server被Consul-template实时删除,并重新加载(Nginx -s>  二、主要用到的工具
  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>  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_headerVary;   
  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>  

  六、验证
  方案一验证:
  查看template中获取到的service 数量
  停止web服务,consul的Config中配置的check是否能发现并把consul的服务状态置为高危不可用(critical)
  直接kill consul进程,查看service数量
  最终还是查看最上层的服务提供nginx的服务状态
  方案二验证:
  方案二只有一个consul客户端
  验证两个环境搭建后是否可用
  tomcat服务停止时是Consul状态是否为critical,重启tomcat服务后,服务正常
  停止consul客户端查看状态
  其中Consul服务端暂时未搭建集群未进行测试。
  七、附录

  ./consul agent -server -data-dir /tmp/data -ui -client 0.0.0.0 -advertise 192.168.102.134 -bootstrap-expect 1

  nohup ./consul agent -config-dir=/usr/local/config -join 192.168.102.134 -data-dir /data 2>&1 &


  ./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>:
  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值
页: [1]
查看完整版本: consul-template + nginx部署高可用负载均衡