NGINX&;Docker中的领事模板
我在使用EC2、AWS、Docker、Concur模板、Concur和NGINX进行一致性服务发现时遇到问题 我有多个服务,每个都运行在自己的EC2实例上。在这些实例上,我运行以下容器(按此顺序):NGINX&;Docker中的领事模板,nginx,amazon-ec2,docker,consul,Nginx,Amazon Ec2,Docker,Consul,我在使用EC2、AWS、Docker、Concur模板、Concur和NGINX进行一致性服务发现时遇到问题 我有多个服务,每个都运行在自己的EC2实例上。在这些实例上,我运行以下容器(按此顺序): cAdvisor(监控) 节点导出器(监视) 领事(在代理模式下运行) 注册人 我的服务 同时运行nginx和concur模板的自定义容器 自定义容器具有以下Dockerfile: FROM nginx:1.9 #Install Curl RUN apt-get update -qq &
- cAdvisor(监控)
- 节点导出器(监视)
- 领事(在代理模式下运行)
- 注册人
- 我的服务
- 同时运行nginx和concur模板的自定义容器
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}}
}
一切似乎都很好地启动了,但在启动后的某个时刻(我还没有确定),concur模板似乎返回一个特定服务没有可用的服务器。这意味着该服务的上游
部分不包含任何服务器,我在日志中以以下内容结束:
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模板仍在运行,Docker容器不会重新启动。实际上,我并不关心容器本身是否重新启动,或者是否只是NGINX重新启动
有人能帮忙吗?一旦编写后运行的脚本返回非零退出代码,Consor模板将退出 文档建议在重新启动(或重新加载)命令后立即输入
| | true
。这将使concur模板独立于退出代码运行
你可以考虑在自己的shell脚本中包装重新启动,该脚本首先在重新加载之前测试配置(使用<代码> NGIX-T-<代码>)。您甚至可以将nginx的初始启动移到此脚本,因为只有在写入第一个(有效)配置后才有意义启动
nginx
因此,-t
是否有效地对要求其重新加载的配置执行验证检查,如果验证失败,则拒绝执行重新加载?如果是这样,这可能会有所帮助。-t
只会验证配置reload
将首先测试配置,如果成功重新加载配置,请参阅。好的,如果我添加| | true,这将阻止Consor模板退出,从而可能会保持容器运行。但是,如果Nginx已崩溃/退出,则重新加载将无效,对吗?或者,是重新加载命令返回1,而Nginx继续运行,但consur模板已退出?据我所知,只有重新加载命令返回1。nginx进程本身继续运行。太棒了。我今天就试试看,然后再给你回复。谢谢你的帮助。