I’m developing a web server in Rust and it works fine using just Docker and defining two services in compose. As follows:
x-common_auth_service: &common_auth_service
container_name: auth
build:
context: ./auth
dockerfile: Dockerfile
network_mode: host
restart: always
deploy:
resources:
limits:
memory: 40M
reservations:
memory: 20M
depends_on:
- redis
services:
auth:
<<: *common_auth_service
container_name: auth
environment:
APP_PORT: 3002 //specific port
auth2:
<<: *common_auth_service
container_name: auth2
environment:
APP_PORT: 3003 //specific port
But now I need to implement these services using Docker Swarm.
Here I came across the first problem when using replicas.
compose.yml
auth_service:
image: service/auth_app_rust
build:
context: ./auth
dockerfile: Dockerfile
networks:
- network_overlay
ports:
- "5000-5001:3002"
deploy:
mode: replicated
replicas: 2
nginx:
image: nginx:latest
volumes:
- ./nginx/auth.conf:/etc/nginx/nginx.conf
networks:
- host
depends_on:
- auth_service
nginx.conf
upstream auth_server {
server 127.0.0.1:5000;
server 127.0.0.1:5001;
keepalive 200;
}
server {
listen 9999;
location / {
proxy_buffering off;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Keep-Alive "";
proxy_set_header Proxy-Connection "keep-alive";
proxy_pass http://auth_server;
}
}
Services that previously defined ports manually used the same port across replicas, so I decided to use “port range mapping”.
And at first it seemed to work fine, but I noticed that Docker Swarm doesn’t assign each port to the container, it actually balances between the replicas.
In other words, if I run:
curl http://localhost:5000
It won’t necessarily run in container 1.
Although I have found it very convenient to scale services using replicas, and the model also makes it easy to upgrade services, how do I solve this problem?
Because I would still like to use replicas, instead of defining several services, but I would not like to use the Docker Swarm Load Balancer, as I already use it through Nginx.
In short, I want to scale the number of containers running the web server in Rust, without having to create several of them in docker-compose, and also not use the load balancer, as the load balancer alignment would be bad.