Docker consult container service update and discovery
Container service update and discovery topology
The nginx proxy service is deployed on the consumer server. The Registrar detects the service in the docker container and adds the registration to the consumer. The consumer template is registered and written into the created template according to the service information in the consumer. The nginx proxy server is set to recognize the template file generated by the consumer template. External network users can access the doc by visiting the nginx proxy server Nginx service in Ker container
Overview of Consul
(1) Consul is an open source tool launched by HashiCrop company to realize service discovery and configuration of distributed system
(2) Characteristics of Consul
- Consul supports health checks, allowing key value pairs to be stored
- The consistency protocol adopts the Raft algorithm to ensure the high availability of services
- GOSSIP protocol is adopted for member management and message broadcasting, and ACL access control is supported
(3) It is easy to deploy and can work seamlessly with lightweight containers such as Docker
Services required by Consul
1. Deploy and run Consul agent on each service providing node
There are two operation modes of Consul agent - Server
- Client
The Server and Client are only the distinction at the Cluster level, not the application services built on the Cluster
2.consul-template
Is the application of Consul based automatic replacement configuration file
You can query the service directory, Key, Key values, etc. in Consul
Especially suitable for dynamic profile creation
3.Gliderlabs/Registrator
Check the running state of the container
Automatically register and unregister the service of docker container to service configuration center
Currently supports Consul, Etcd and SkyDNS2
Experimental deployment
Deploy the consumer service, realize the automatic discovery of deployed nginx service and realize the load balancing service of nginx
1, Building a Docker service architecture for automatic discovery
1. Establish Consul service[root@consul ~]# mkdir consul [root@consul ~]# cp /opt/consul_0.9.2_linux_amd64.zip /root/consul/ [root@consul ~]# cd /root/consul/ [root@consul consul]# unzip consul_0.9.2_linux_amd64.zip [root@consul consul]# ls consul consul_0.9.2_linux_amd64.zip [root@consul consul]# mv consul /usr/bin/ [root@docker consul]# Consumer agent \ / / use agent function -server \ //Provide server function -bootstrap \ //Take part in the election as a leader -ui \ //Provide web access interface -data-dir=/var/lib/consul-data \ //Parameter storage location -bind=192.168.7.129 \ //Binding address -client=0.0.0.0 \ //Facing all node terminals -node=consul-server01 &> /var/log/consul.log & //Specify the local node name, log file directory, and run in the background
2. View cluster information
[root@consul consul]# consul members Node Address Status Type Build Protocol DC consul-server01 192.168.7.129:8301 alive server 0.9.2 2 dc1 [root@consul consul]# consul info | grep leader leader = true leader_addr = 192.168.7.129:8300
3. Get cluster information through httpd api
[root@consul ~]# curl 127.0.0.1:8500/v1/status/peers / / view cluster server members [root@consul ~]# curl 127.0.0.1:8500/v1/status/leader / / view leader information [root@consul ~]# curl 127.0.0.1:8500/v1/catalog/services / / all registered services [root@consul ~]# curl 127.0.0.1:8500/v1/catalog/nginx / / view nginx service information [root@consul ~]# curl 127.0.0.1:8500/v1/catalog/nodes / / cluster node details
2, Container service automatically added to nginx cluster
1. Install gliderlabs / Registrar[root@docker ~]# docker run -d \ --name=registrator \ //Specify name --net=host \ //Specify network -v /var/run/docker.sock:/tmp/docker.sock \ //Specify data volume --restart=always \ //restart function gliderlabs/registrator:latest \ -ip=192.168.7.168 \ //Specify local IP address consul://192.168.7.129:8500
2. Add nginx service
[root@docker ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx [root@docker ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 960d01d212c4 nginx "nginx -g 'daemon of..." 4 seconds ago Up 3 seconds 0.0.0.0:84->80/tcp test-02 bc075c1c3670 nginx "nginx -g 'daemon of..." 59 seconds ago Up 57 seconds 0.0.0.0:83->80/tcp test-01 5dd120cd1232 gliderlabs/registrator:latest "/bin/registrator -i..." 3 minutes ago Up 3 minutes registrator
3. Verify that the nginx service is registered with consumer
View services on the consumer server[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/services {"consul":[],"nginx":[]}
4. Prepare template nginx template file
[root@consul ~]# vim /root/consul/nginx.ctmpl upstream http_backend { {{range service "nginx"}} server {{.Address}}:{{.Port}}; {{end}} } server { listen 83; server_name localhost 192.168.7.129; access_log /var/log/nginx/kgc.cn-access.log; index index.html index.php; location / { proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://http_backend; } }
5. Compile, install and configure nginx
[root@consul ~]# yum install gcc pcre-devel zlib-devel -y [root@consul ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt [root@consul ~]# cd /opt/nginx-1.12.0 [root@consul ~]# ./configure --prefix=/usr/local/nginx [root@consul ~]# make && make install [root@consul ~]# vim /usr/local/nginx/conf/nginx.conf http { include mime.types; include vhost/*.conf; //Add virtual host directory default_type application/octet-stream; #Create virtual host directory [root@consul ~]# mkdir /usr/local/nginx/conf/vhost #Create log file directory [root@consul ~]# mkdir /var/log/nginx #start nginx /usr/local/nginx/sbin/nginx
6. Configure and start template
Upload the customer-template ﹣ 0.19.3 ﹣ Linux ﹣ amd64.zip to the / root / customer directory[root@consul consul]# unzip consul-template_0.19.3_linux_amd64.zip [root@consul consul]# mv consul-template /usr/bin [root@consul consul]# consul-template -consul-addr 192.168.7.129:8500 \ -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/aaa.conf:/usr/local/nginx/sbin/nginx -s reload" \ --log-level=info #After startup, the aaa.conf configuration file will be automatically generated in the vhost directory [root@consul ~]# ls /usr/local/nginx/conf/vhost/ abc.conf
7. Add a nginx container node to test the service discovery and configuration update functions (added on the Registrar server side)
[root@docker ~]# docker run -itd -p:85:80 --name test-03 -h test03 nginx`
Automatic update will be prompted in the monitoring of the consumer server