Find clusters based docker, consul, consul-template, registrator, nginx service registration

 
Introduction
The project mainly automatic registration service discovery, so as to improve the efficiency of operation and maintenance, so that automatically discovers services and dynamic expansion.
Registration discovery service
  • Auto service startup was found
  • Dynamic load balancing changes
  • Automatic retractable
tool
1.Registrator This is a written language Go for docker used by the native container inspection process online or stop running, tools and services to register. It is directly monitor container event by docker socket, according to the container start / stop and other event to register / unregister service. Each corresponding to a different service port exposed each container. Support pluggable registry backend, default support Consul, etcd and SkyDNS.
2.Consul Consul is a highly available distributed service discovery and configuration of shared software. HashiCorp developed by the company with the Go language.
3.consul-template consul template can query consul in the service catalog, key, key-values ​​and so on. This powerful abstractions and query language templates can be particularly suitable for the consul template to create a dynamic profile. For example: Create apache / nginx proxy balancers, haproxy backends, varnish servers, application configurations.
consul-template provides a convenient way to get the value stored from the consul in, consul-template daemon queries the consul service to update any template specified on the system, after when the update is complete, the template can choose to run some arbitrary command, for example, we use it here to update nginx.conf the configuration file, and then perform nginx -s reload command to update the routing, dynamic adjustment to achieve the purpose of load balancing.
4.nginx Needless to say, the load balancing software
Operating mechanism
我们用registrator来监控每个web server的状态。当有新的web server启动的时候,registrator会把它注册到consul这个注册中心上。由于consul_template已经订阅了该注册中心上的服务消息,此时consul注册中心会将新的web server信息推送给consul_template,consul_template则会去修改nginx.conf的配置文件,然后让nginx重新载入配置以达到自动修改负载均衡的目的。同样当一个web server挂了,registrator也能感知到,进而通知consul做出响应。
整个过程不需要运维人工的干预,自动完成。
 
 
环境搭建
build images
build web app image
mvn package docker:build
build nginx-consul-template image
$ cd consul-template-docker
$ docker build -t myimage/nginx-consul-template .
run
$ cd docker/compose
$ docker-compose up -d
scale
启动3个app服务,自动的注册服务,加入Nginx的负载均衡
$ docker-compose down
$ docker-compose scale web=3
$ docker-compose up
访问
 
可以发现,每次访问到的后端服务都是动态变化的,并且在服务关闭还是有新的服务加入进来,都能够做到动态感知,无须人工干预。
consul cluster
为了解决consul的单点问题,我们需要建立consul集群。 在这个方案中,每个提供服务的节点上都要部署和运行一个agent,所有运行Consul agent节点的集合构成Consul Cluster。
 
Consul agent有两种运行模式:Server和Client。这里的Server和Client只是Consul集群层面的区分,与搭建在Cluster之上的应用服务无关。
以Server模式运行的Consul agent节点用于维护Consul集群的状态,官方建议每个Consul Cluster至少有3个或以上的运行在Server mode的Agent,Client节点不限。
 
每个数据中心的Consul Cluster都会在运行于server模式下的agent节点中选出一个Leader节点,这个选举过程通过Consul实现的raft协议保证,多个 server节点上的Consul数据信息是强一致的。处于client mode的Consul agent节点比较简单,无状态,仅仅负责将请求转发给Server agent节点。
 
每台机器上部署几个web server,一个registrator和一个consul client,这是基本需求。另外再建立一个consul cluster集群,用来当我们的注册中心。当web server启动后,被registrator感知,进而将注册信息发送给consul client,consul client则访问注册中心的leader节点,上报新加入的服务信息。consul cluster会将新的服务信息推送给已经到它这里订阅了服务消息的consul-template,consul-template再去修改和自己同一台机器上的nginx,以达到动态调整负载均衡的目的。
 
 

 

 
 


Guess you like

Origin www.cnblogs.com/tonyq/p/12329678.html