博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
consul-template + nginx部署高可用负载均衡
阅读量:6868 次
发布时间:2019-06-26

本文共 4494 字,大约阅读时间需要 14 分钟。

一、目标

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值

转载于:https://www.cnblogs.com/MrCandy/p/7152312.html

你可能感兴趣的文章
转: Eclipse 分屏显示同一个文件
查看>>
Selenium2+python自动化15-select下拉框
查看>>
Asp.net 实现Session分布式储存(Redis,Mongodb,Mysql等) sessionState Custom
查看>>
ASP.NET MVC5+EF6+EasyUI 后台管理系统(81)-数据筛选(万能查询)
查看>>
如果觉得配置文件没有错,但web-dev-server总是报错,可以在hosts文件里加一行127.0.0.1 localhost...
查看>>
【转】分享8年开发经验,浅谈个人发展经历,明确自己发展方向
查看>>
If you only do what you can do you'll never be more than you are now.
查看>>
详解:基于WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器功能...
查看>>
JavaScript获取浏览器类型与版本
查看>>
STM32普通定时器(TIM2-7)的时钟源
查看>>
[Android]使用Dagger 2进行依赖注入 - Producers(翻译)
查看>>
时序列数据库选型
查看>>
lamp安装
查看>>
QDEZ集训笔记【更新中】
查看>>
手工配置rsyslog配置文件详解
查看>>
composer安装
查看>>
Linux下快速迁移海量文件的操作记录
查看>>
windows环境redis主从安装部署
查看>>
mongodb指南
查看>>
su: user tomcat does not exist
查看>>