Use Docker to build consul cluster +registrator for automatic service registration.

Source: Internet
Author: User
Tags docker run


Preparatory work:
10.173.16.83 Master
10.172.178.76 Node1
10.171.19.139 Node2
10.162.204.252 Node3


First, install the Consul-cluster
Master
Docker run-d-H master-v/mnt:/data \
-P 10.173.16.83:8300:8300 \
-P 10.173.16.83:8301:8301 \
-P 10.173.16.83:8301:8301/UDP \
-P 10.173.16.83:8302:8302 \
-P 10.173.16.83:8302:8302/UDP \
-P 10.173.16.83:8400:8400 \
-P 8,500:8,500 \
Progrium/consul-server-advertise 10.173.16.83-bootstrap-expect 3


Node1:
Docker run-d-H node1-v/mnt:/data \
-P 10.172.178.76:8300:8300 \
-P 10.172.178.76:8301:8301 \
-P 10.172.178.76:8301:8301/UDP \
-P 10.172.178.76:8302:8302 \
-P 10.172.178.76:8302:8302/UDP \
-P 10.172.178.76:8400:8400 \
-P 8,500:8,500 \
Progrium/consul-server-advertise 10.172.178.76-join 10.173.16.83

Node2:
Docker run-d-H node2-v/mnt:/data \
-P 10.171.19.139:8300:8300 \
-P 10.171.19.139:8301:8301 \
-P 10.171.19.139:8301:8301/UDP \
-P 10.171.19.139:8302:8302 \
-P 10.171.19.139:8302:8302/UDP \
-P 10.171.19.139:8400:8400 \
-P 8,500:8,500 \
Progrium/consul-server-advertise 10.171.19.139-join 10.173.16.83


Second, installation consul-client
NODE3:
Docker run-d-H node3-v/mnt:/data \
-P 10.162.204.252:8300:8300 \
-P 10.162.204.252:8301:8301 \
-P 10.162.204.252:8301:8301/UDP \
-P 10.162.204.252:8302:8302 \
-P 10.162.204.252:8302:8302/UDP \
-P 10.162.204.252:8400:8400 \
-P 8,500:8,500 \
Progrium/consul-advertise 10.162.204.252-join 10.173.16.83


Third, installation Regitrator
Master
Docker run-d \
-v/var/run/docker.sock:/tmp/docker.sock \
--name registrator-h registrator \
Gliderlabs/registrator:latest consul://10.173.16.83:8500

Node1:
Docker run-d \
-v/var/run/docker.sock:/tmp/docker.sock \
--name registrator-h registrator \
Gliderlabs/registrator:latest consul://10.172.178.76:8500


Node2:
Docker run-d \
-v/var/run/docker.sock:/tmp/docker.sock \
--name registrator-h registrator \
Gliderlabs/registrator:latest consul://10.171.19.139:8500

NODE3:
Docker run-d \
-v/var/run/docker.sock:/tmp/docker.sock \
--name registrator-h registrator \
Gliderlabs/registrator:latest consul://10.162.204.252:8500

Four, simple test.
1. Start the Python-micro-service container. (Start multiple, on a machine or multiple machines)
Docker run-d-P--name node1-h Node1 jlordiales/python-micro-service:latest

2. Define the template file and view the results.
[Email protected] ~]# Cat/tmp/consul.ctmpl
{{Range service ' Python-micro-service '}}\nserver {.} address}}:{{. Port}}{{end}}
[Email protected] ~]# Consul-template-consul master:8500-template/tmp/consul.ctmpl:/tmp/consul.result-dry-once

Five, test installation Nginx
1. Download and install Consul-template
wget Https://releases.hashicorp.com/consul-template/0.7.0/consul-template_0.7.0_linux_amd64.zip
Unzip consul-template_0.7.0_linux_amd64.zip-d/usr/bin/

2. View Nginx's dockerfile and related files.
[Email protected] nginx]# pwd
/root/nginx
[[email protected] nginx]# ls
Consul-template Dockerfile start.sh
[email protected] nginx]# cat Dockerfile
From Nginx:latest

entrypoint ["/bin/start.sh"]
EXPOSE 80
Volume/templates
ENV Consul_url consul:8500

ADD start.sh/bin/start.sh
RUN rm-v/etc/nginx/conf.d/*
ADD consul-template/usr/local/bin/
#RUN chmod +x/usr/local/bin/consul-template && chmod +x/bin/start.sh

[email protected] nginx]# cat start.sh
#!/bin/bash
Service Nginx Start
consul-template-consul= $CONSUL _url-template= "/templates/service.ctmpl:/etc/nginx/conf.d/service.conf:service Nginx Reload "


3. Define template files and result files
[Email protected] ~]# Cat/tmp/service.ctmpl
Upstream Python-service {
Least_conn;
{{Range service ' Python-micro-service '}}server {.} address}}:{{. Port}} max_fails=3 fail_timeout=60 weight=1;
{{Else}}server 127.0.0.1:65535; # Force a 502 {{end}}
}

server {
Listen default_server;
CharSet Utf-8;

Location/{
Proxy_pass Http://python-service;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
}
}


4. Build the Nginx image and start it.
Docker build-t Nginx.
Docker run-p 8080:80-d--name nginx--volume/tmp/service.ctmpl:/templates/service.ctmpl--link consul:consul nginx

5. View the template results.
Enter the Nginx container to view the contents of the configuration file.


Reference Documentation:
1.https://jlordiales.me/2015/04/01/consul-template/
2.https://jlordiales.me/2015/02/03/registrator/#advertise
3.https://jlordiales.me/2015/01/23/docker-consul/

Use Docker to build consul cluster +registrator for automatic service registration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.