我在使用 EC2、AWS、Docker、Consul-Template、Consul 和 NGINX 进行一致的服务发现时遇到问题。
我有多个服务,每个服务都在它自己的 EC2 实例上运行。在这些实例上,我运行以下容器(按此顺序):
- cAdvisor(监控)
- 节点导出器(监控)
- Consul(以代理模式运行)
- 登记员
- 我的服务
- 同时运行 nginx 和 consul-template 的自定义容器
自定义容器具有以下 Dockerfile:
FROM nginx:1.9
#Install Curl
RUN apt-get update -qq && apt-get -y install curl
#Install Consul Template
RUN curl -L https://github.com/hashicorp/consul-template/releases/download/v0.10.0/consul-template_0.10.0_linux_amd64.tar.gz | tar -C /usr/local/bin --strip-components 1 -zxf -
#Setup Consul Template Files
RUN mkdir /etc/consul-templates
COPY ./app.conf.tmpl /etc/consul-templates/app.conf
# Remove all other conf files from nginx
RUN rm /etc/nginx/conf.d/*
#Default Variables
ENV CONSUL consul:8500
CMD /usr/sbin/nginx -c /etc/nginx/nginx.conf && consul-template -consul=$CONSUL -template "/etc/consul-templates/app.conf:/etc/nginx/conf.d/app.conf:/usr/sbin/nginx -s reload"
app.conf 文件如下所示:
{{range services}}
upstream {{.Name}} {
least_conn;{{range service .Name}}
server {{.Address}}:{{.Port}};{{end}}
}
{{end}}
server {
listen 80 default_server;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://cart/cart/;
}
location /cart {
proxy_pass http://cart/cart;
}
{{range services}}
location /api/{{.Name}} {
proxy_read_timeout 180;
proxy_pass http://{{.Name}}/{{.Name}};
}
{{end}}
}
一切似乎都可以正常启动,但是在启动后的某个时间点(我尚未确定),consul-template 似乎返回特定服务没有可用的服务器。这意味着该upstream
服务的部分不包含服务器,我最终在日志中得到了这个:
2015/12/04 07:09:34 [emerg] 77#77: no servers are inside upstream in /etc/nginx/conf.d/app.conf:336
nginx: [emerg] no servers are inside upstream in /etc/nginx/conf.d/app.conf:336
2015/12/04 07:09:34 [ERR] (runner) error running command: exit status 1
Consul Template returned errors:
1 error(s) occurred:
* exit status 1
2015/12/04 07:09:34 [DEBUG] (logging) setting up logging
2015/12/04 07:09:34 [DEBUG] (logging) config:
{
"name": "consul-template",
"level": "WARN",
"syslog": false,
"syslog_facility": "LOCAL0"
}
2015/12/04 07:09:34 [emerg] 7#7: no servers are inside upstream in /etc/nginx/conf.d/app.conf:336
nginx: [emerg] no servers are inside upstream in /etc/nginx/conf.d/app.conf:336
此后,NGINX 将不再接受请求。
我确定我遗漏了一些明显的东西,但我已经把自己绑在关于事件顺序等的心理上。我认为可能发生的是 NGINX 崩溃,但由于 consul-template 仍在运行,Docker 容器不重新启动。我实际上并不关心容器本身是否重新启动,或者只是 NGINX 重新启动。
有人可以帮忙吗?