Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate configuration dynamically through nginx+confd in docker

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to dynamically generate configuration through nginx+confd in docker. I hope you will get something after reading this article. Let's discuss it together.

When we have more and more projects, it is troublesome and possible to go to the server to modify nginx configuration manually. We can implement a set of solutions through the nginx+confd+ configuration center to avoid errors and reduce tedious processes.

First of all, go directly to nginx+confd 's dockerfile.

FROM nginx:1.21.6# copy confd binary executable file https://github.com/kelseyhightower/confd/releases/tag/v0.16.0COPY. / confd-0.16.0-linux-amd64 / usr/local/bin/confd# copy wait-for script https://github.com/Eficode/wait-forCOPY. / wait-for / # install nc support wait-for script RUN apt-get update\ & & DEBIAN_FRONTEND=noninteractive apt-get install- Y\ net-tools\ & & apt-get clean\ & & rm-rf / var/lib/apt/lists/*RUN apt-get update\ & & DEBIAN_FRONTEND=noninteractive apt-get install-y\ netcat\ & & apt-get clean\ & & rm-rf / var/lib/apt/lists/*# create a configuration file directory RUN mkdir-p / etc/confd/conf.d\ # give executable permissions & & chmod + x / usr/local/bin / confd\ & & chmod 777 / wait-for\ # start multiple processes using scripts & & echo "#! / bin/bash" > > start.sh\ & & echo "nohup / usr/local/bin/confd-config-file / etc/confd/conf/confd.toml &" > start.sh\ & echo "nginx-g 'daemon off '"> > start.sh\ & & chmod 664. / start.sh CMD [" bash "," start.sh "]

If you don't want to build it yourself, you can use it:

Https://hub.docker.com/repository/docker/lablelan/nginx-confd

Docker-compose is used here to demonstrate how to use nginx+confd+etcd+etcdkeeper to graphically modify the nginx configuration

Version: '2'networks: app-tier: driver: bridge ipam: driver: default config:-subnet: 172.22.0.0/16services: Etcd: image:' bitnami/etcd:3.5.2' environment:-ALLOW_NONE_AUTHENTICATION=yes-ETCD_ADVERTISE_CLIENT_URLS= http://etcd:2379 ports:-2379-2380 networks: App-tier: ipv4_address: 172.22.0.2 EtcdKeeper: image: 'deltaprojects/etcdkeeper:latest'-8000 image 8080 ipv4_address: 172.22.0.3 Nginx: image:' lablelan/nginx-confd' command: sh-c'/ wait-for Etcd:2379-- bash start.sh' depends_on:-Etcd volumes:-". / confd.toml:/etc/ Confd/conf/confd.toml "-". / nginx.tmpl:/etc/confd/templates/nginx.tmpl "-". / myapp-nginx.toml:/etc/confd/conf.d/myapp-nginx.toml "-". / nginx.conf:/etc/nginx/nginx.conf "-". / conf.d:/etc/nginx/conf.d "- 80:80 ipv4_address: 172.22.0.4

When / myapp/services/web/www/1 is created or updated through etcdkeeper after startup, the nginx configuration (port 80 forwarding port 8080) is dynamically generated and takes effect automatically. Here, ip is configured with private network ip that does not use 127.0.0.1, because the service does not run on the nginx container (note that www.lablelan.com my local hosts is set to 127.0.0.1)

The service running on port 8080 here is a simple demo service (nodejs), which can be obtained from gitee.

Https://gitee.com/lablelan/amis-demo

After configuring etcd, we can see that the visit to http://www.lablelan.com/select has been returned successfully.

This may not reflect the practicability of this solution, but we can achieve the service discovery function by registering the current service information with etcd in the business code, so that the nginx configuration can be generated dynamically so as to avoid manually modifying the nginx configuration and reduce errors.

After reading this article, I believe you have a certain understanding of "how to dynamically generate configuration through nginx+confd in docker". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report