Automatically add docker container to top nginx load balancing configuration

I am running Docker Compose (v2) and have a node service (website) and python based api deployed with nginx sitting in front of them.

One thing I would like to do is scale the services by adding more containers. If I know in advance how many containers I will have, I can tightly configure the nginx upstream config with links to the IP addresses of the containers that the docker provides. However, the problem is that I want the nginx upstream configuration to be dynamic, for example. if I add another Docker container, it will simply add the location of the container to the upstream list of IP addresses in the top block.

My idea was to create a script that would automatically add upstream servers using env variables when the containers change, but I'm not sure where to start, and can't find a good example.

+5
source share
1 answer

There are several ways to achieve this. What you mean is usually called service discovery and takes many forms. I will describe two of them that I used before.

The first and easiest (which works fine for single servers or locally detects containers on a single server) is a local proxy server that uses a Docker socket or API. https://github.com/jwilder/nginx-proxy is one of the most popular and should work well for prototyping scalable services in Compose.

Another way (more friendly to multiple hosts, but more complex) is to register services in the registry (for example, etcd or Consul), and then dynamically record the configuration. To do this, you can use the registration system (for example, https://github.com/gliderlabs/registrator ) to register containers and their ports. Then your proxy server or application can use a configuration file written using a template system, for example https://github.com/kelseyhightower/confd .

+2
source

Source: https://habr.com/ru/post/1247312/


All Articles