Nginx   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Docker中的NGINX和Consul-Template大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我在使用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才有意义?!

大佬总结

以上是大佬教程为你收集整理的Docker中的NGINX和Consul-Template全部内容,希望文章能够帮你解决Docker中的NGINX和Consul-Template所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: