我在使用EC2,AWS,Docker,Consul-Template,Consul和Nginx进行一致的服务发现方面遇到了麻烦.
我有多个服务,每个服务都运行在自己的EC2实例上.在这些实例中,我运行以下容器(按此顺序):
> cAdvisor(监控)
>节点导出器(监控)
>领事(在代理模式下运行)
> Registrator
>我的服务
>运行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似乎返回说没有可用于特定服务的服务器.这意味着该服务的上游部分不包含任何服务器,我在日志中最终得到这个:
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重启.
有人可以帮忙吗?
最佳答案