使用Docker搭建consul集群+registrator实现服务自动注册。


准备工作:
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.定义模版文件并查看结果。
[root@master ~]# cat /tmp/consul.ctmpl
{{range service "python-micro-service"}}\nserver {{.Address}}:{{.Port}}{{end}}
[root@master ~]# 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及相关文件。
[root@master nginx]# pwd
/root/nginx
[root@master nginx]# ls
consul-template Dockerfile start.sh
[root@master 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

[root@master 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.定义模版文件与结果文件
[root@master ~]# 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/

posted @ 2016-06-07 08:23  xkops  阅读(3155)  评论(0编辑  收藏  举报