加入收藏 | 设为首页 | 会员中心 | 我要投稿 阜阳站长网 (https://www.0558zz.com/)- 科技、建站、内容创作、云计算、网络安全!
当前位置: 首页 > 运营中心 > Nginx > 正文

Docker中的NGINX和Consul-Template

发布时间:2020-07-23 16:09:06 所属栏目:Nginx 来源:互联网
导读:我在使用EC2,AWS,Docker,Consul-Template,Consul和NGINX进行一致的服务发现方面遇到了麻烦.我有多个服务,每个服务都运行在自己的EC2实例上.在这些实例中,我运行以下容器(按此顺序): cAdvisor(监控)节点导出器(监控)领事(在代理模式下运行) Registrator我的服

我在使用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重启.

有人可以帮忙吗?

最佳答案 一旦写入后运行的脚本返回非零退出代码,Consul Template将退出. See here for the documentation.

文档建议放一个||在重启(或重新加载)命令之后为true.这将使Consul Template独立于退出代码运行.

您可以考虑将重新启动包装在自己的shell脚本中,该脚本首先在触发重新加载之前测试配置(使用nginx -t).您甚至可以将nginx的初始启动移动到此脚本,因为只有在编写了第一个(有效)配置后启动nginx才有意义?!

(编辑:阜阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读