通过Registrator进行服务的自动注册

目录

作者:杨冬 欢迎转载,也请保留这段声明。谢谢!
出处:https://andyyoung01.github.io/http://andyyoung01.16mb.com/

上篇文章探索了使用Consul进行服务发现。使用Consul进行服务注册需要手动管理服务条目的创建和删除。如果将其集成到应用程序中,有很多地方容易出错。所以如果有一种方式可以自动管理Consul中的服务条目是最好不过了。Registrator正是这样一种工具。

Registrator去除了需要手动管理Consul服务条目的复杂性,它监视容器的启动和停止,根据容器暴露的端口和环境变量自动注册服务。
下面就实际实验一下它的使用。本篇继续使用上篇文章的环境,有两个节点,一个运行了Consul server agent(机器名:centos7-A,ip:192.168.71.167),另一个运行了Consul client agent(机器名:centos7,ip:192.168.71.131)。下面在client agent的机器上进行操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[yangdong@centos7 ~]$ docker pull gliderlabs/registrator:latest
...
[yangdong@centos7 ~]$ docker run -d --name registrator -h centos7-reg \
-v /var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator \
-ip 192.168.71.131 -resync 60 consul://172.17.0.1:8500
34d78fab229bd90dd775ff082b1d8a4f41dd16094f911447545f3ed79b2d0463
[yangdong@centos7 ~]$ docker logs -f registrator
2016/09/28 03:12:49 Starting registrator v7 ...
2016/09/28 03:12:49 Forcing host IP to 192.168.71.131
2016/09/28 03:12:49 Using consul adapter: consul://172.17.0.1:8500
2016/09/28 03:12:49 Connecting to backend (0/0)
2016/09/28 03:12:49 consul: current leader 192.168.71.167:8300
2016/09/28 03:12:49 Listening for Docker events ...
2016/09/28 03:12:49 Syncing services on 2 containers
2016/09/28 03:12:49 ignored: 34d78fab229b no published ports
2016/09/28 03:12:49 ignored: 98a0a1c82ac9 no published ports

上面的命令先拉取了Registrator的最新镜像,然后将其启动。-ip参数告诉Registrator使用哪个ip地址来发布服务。将docker socket挂载到Registrator内部允许其监视docker的事件,在容器启动或停止时自动得到通知。其它的命令行参数告诉Registrator怎样连接到Consul agent,并且希望所有的容器信息每60秒刷新一次。上面的IP地址172.17.0.1为centos7机器的docker0网桥的地址,consul使用此ip提供服务。
现在Registrator已经正常运行,对于服务的注册就非常容易:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[yangdong@centos7 ~]$ curl -sSL 172.17.0.1:8500/v1/catalog/services | python -m json.tool
{
"consul": []
}
[yangdong@centos7 ~]$ docker run -d -e "SERVICE_NAME=files" -p 8000:80 ubuntu:14.04 python3 \
-m http.server 80
fde6f35c9f8dff45abc3201c78ef3d68320e8e429c1d606d2ae768f7649a122b
[yangdong@centos7 ~]$ docker inspect fde6f35c9f8d | grep 'Name.*/'
"Name": "/reverent_mcnulty",
[yangdong@centos7 ~]$ curl -sSL 172.17.0.1:8500/v1/catalog/services | python -m json.tool
{
"consul": [],
"files": []
}
[yangdong@centos7 ~]$ curl -sSL 172.17.0.1:8500/v1/catalog/service/files | python -m json.tool
[
{
"Address": "192.168.71.131",
"CreateIndex": 17,
"ModifyIndex": 28,
"Node": "centos7",
"ServiceAddress": "192.168.71.131",
"ServiceEnableTagOverride": false,
"ServiceID": "centos7-reg:reverent_mcnulty:80",
"ServiceName": "files",
"ServicePort": 8000,
"ServiceTags": [],
"TaggedAddresses": {
"lan": "192.168.71.131",
"wan": "192.168.71.131"
}
}
]

从上面的几个命令我们知道了在启动容器时传入了一个环境变量告诉Registrator要使用什么服务名。但这不是必须的,默认情况下,Registrator根据该容器的镜像名称生成服务名称。是否使用这种特性取决于你的命名规则。
Registrator得到了容器的监听端口,将其加入Consul,并且设置了ServiceID以便于找到容器。Registrator还会从docker容器的环境中得到其它的一些如果存在的信息,例如tags,每端口的服务名(如果有多个),以及使用健康检查的相关信息(如果使用Consul作为数据存储方式的话)。关于Registrator与Consul集成的更多信息,可以参考Registrator Backends的官方文档