nginx

72次阅读
没有评论

共计 3863 个字符,预计需要花费 10 分钟才能阅读完成。

这篇文章将为大家详细讲解有关 nginx-template 如何实现动态更新 Nginx upstream,文章内容质量较高,因此丸趣 TV 小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

Consul Template 提供一个方便的方式从 Consul 服务获取数据通过 consul-template 的后台程序保存到文件系统,这个后台进程监控 Consul 中数据的变化并更新任意数量的模板到文件系统。模板更新完成后 consul-template 也可以触发相关的指令或者脚本,此处通过简单的实践动态更新 Nginx 的 upstream server 并且触发 reloadnginx 服务。当然对于小规模下的应用场景还有很多,比如 Haproxy 配置文件动态更新等。

这里小小的做个调查,你使用过以下那种方案:

下面,我们就开始今天的实践目的吧,通过 consul-template 与 consul 完成 nginx 配置文件的自动更新:

Docker 部署 Consul 集群

实验版本信息:

软件版本主机 CentOS Linux 7 (Core) 内核 3.10.0-1160.15.2.el7.x86_64docker20.10.4Consul 镜像 consul:latest – 1.9.1Consul-template0.20.0

准备 consul 集群,你也可以使用单独的一个 consul 实例做基础环境的支撑,可以参考通过 Docker 一键部署 consul 集群

nginx
 Docker 一键部署 Consul 集群原文   安装 consul-template 以及基础环境

此处,安装下 consul-template, 然后运行一个 nginx 容器,并且把 nginx 配置文件挂载到宿主机上:

wget https://releases.hashicorp.com/consul-template/0.20.0/consul-template_0.20.0_linux_amd64.zip
unzip consul-template_0.20.0_linux_amd64.zip
chmod +x consul-template
mv consul-template /usr/local/bin/
docker run -d --name nginx --restart=always -p 80:80 nginx:1.18
docker cp nginx:/etc/nginx /tmp/nginx
docker stop nginx   docker rm nginx
docker run -d --name nginx --restart=always -p 80:80 -v /tmp/nginx:/etc/nginx nginx:1.18

 

准备一个脚本文件,当 consul-template 从 consul 中获取到数据的更新后,触发该脚本运行 nginx_operator.sh,实现 reloadnginx 的配置文件。

#!/bin/sh
docker ps -aq --filter  name=nginx
if [ $? -ne 0 ]; then
 echo  Starting Nginx Server...
 docker run -d --name nginx --restart=always -p 80:80 -v /tmp/nginx:/etc/nginx nginx:1.18
 echo  nginx start done.
else
 echo  Reloading nginx...
 docker exec -it nginx nginx -s reload;
 echo  nginx reloading done.
fi

 

接下来准备 consul-template 运行的配置文件 nginx.hcl,里面通过 source 指定了我们要使用的模板文件,以及 destination 指定根据模板渲染后生成的配置文件存储的路径位置,最后一个 command 就是当完成一次渲染后,要触发的动作,这里是一个脚本,就是会触发该脚本的内容。

wait {
 min =  3s
 max =  9s
}

syslog {
 enabled = true
 facility =  LOCAL5
}

consul {
 address =  192.168.99.3:8500
}

template {
 source =  /tmp/nginx/conf.d/consul/backend_nginx.ctmpl
 destination =  /tmp/nginx/conf.d/consul/nginx01.conf
 command =  /root/start_nginx.sh
}

 

然后,需要准备一下 consul-template 要监听的模板文件 backend_nginx.ctmpl,consul-template 可以同时指定多个模板文件,如下所示:

consul-template \
 -consul $consul_cluster_address:8500 \
 -template  $template1:$file1:$command1  \
 -template  $template2:$file2:$comand2  \
 -template  $template3:$file3

 

此处只用一个模板做一下测试,模板的中的语法是 go template 语法,这里实现的比较简单,只是做了 upstream.server 的渲染,对于实际使用来说,可以把模板文件中的 nginx1 与 server_name 渲染的数据也存在 consul 集群中,这样也是可以渲染的。

upstream nginx1 {
 server 127.0.0.1:2000 down;
 {{ range service  local.www@dc1  }}
 server {{.Address}}:{{.Port}} weight=1;
 {{end}}
}

server {
 listen 80;
 server_name www.kubemaster.top;
 location / {
 proxy_pass http://nginx1;
 }
}

 

在这个实验中,consul-template 通过 consul 拿到变化后的数据,将数据成功的渲染到配置文件之后,我们可以通过 www.kubeamster.top 能正常访问 upstream 后的服务,所以还需要准备一个上游服务,这里直接使用 docker 快速的运行一个即可

docker run -d --name nginx1 -p 8081:80 nginx:1.18
docker exec -it nginx1 bash
echo  backend server for consul-template    /usr/share/nginx/html/index.html

 

此时,我们就可以把 consul-template 运行起来,作为后端进程运行着:

[root@node1 consul]# consul-template -config  nginx.hcl  --log-level=info
2021/03/04 19:25:21 [DEBUG] (logging) enabling syslog on LOCAL5
2021/03/04 11:25:21.098622 [INFO] consul-template v0.20.0 (b709612c)
2021/03/04 11:25:21.098767 [INFO] (runner) creating new runner (dry: false, once: false)
2021/03/04 11:25:21.099244 [INFO] (runner) creating watcher
2021/03/04 11:25:21.101210 [INFO] (runner) starting
...

 

最后我们通过 curl 或者 postman 把运行的 nginx1 服务往 consul 集群中注册一下

curl --location --request PUT  http://192.168.99.3:8500/v1/catalog/register  \
--header  Content-Type: application/json  \
--data-raw  {Datacenter :  dc1 , Node :  721d401b9555 , Address :  192.168.99.3 , Service : {  Id :  172.17.0.12:80 ,  Service :  www , tags : [  local  ], Port : 8081}}

 

通过 hostctl 增加一条本地 DNS 解析

nginx
  通过 hostctl 增加一条本地 DNS 解析

最后,我们通过浏览器访问一下 http://www.kubemaster.top 查看返回的内容就是 nginx1 的服务。这说明 consul-template 按照预期完成了工作。

nginx
 Access Backend server by consul-template

到这里,基本上完成了使用 consul-template 与 consul 实现 nginx 配置文件的自动更新。

关于 nginx-template 如何实现动态更新 Nginx upstream 就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-25发表,共计3863字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)