使用Docker搭建consul叢集 registrator實現服務自動註冊。

NO IMAGE

準備工作:
10.173.16.83 master
10.172.178.76 node1
10.171.19.139 node2
10.162.204.252 node3


一、安裝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 8500:8500 \
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 8500:8500 \
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 8500:8500 \
progrium/consul -server -advertise 10.171.19.139 -join 10.173.16.83


二、安裝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 8500:8500 \
progrium/consul -advertise 10.162.204.252 -join 10.173.16.83

 


三、安裝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

四、簡單測試。
1.啟動python-micro-service容器。(啟動多個,在某個機器或多個機器)
docker run -d -P –name node1 -h node1 jlordiales/python-micro-service:latest

2.定義模版檔案並檢視結果。
[[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

 

五、測試安裝nginx
1.下載安裝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.檢視nginx的dockerfile及相關檔案。
[[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.定義模版檔案與結果檔案
[[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 80 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.構建nginx映象並並啟動。
docker build -t nginx .
docker run -p 8080:80 -d –name nginx –volume /tmp/service.ctmpl:/templates/service.ctmpl –link consul:consul nginx

5.檢視模版結果。
進入nginx容器,檢視配置檔案內容。


參考文件:
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/