Multiple postgres deployments using different subdns

Hello,

I am trying to use multiple postgres databases using docker swarm. But in the configuration I shared below, if I turn postgre containers into workers, I cannot connect to my postgre servers.

version: '3.8'

services:
traefik:
image: 'traefik:v3.1'
ports:
- "80:80"
- "443:443"
- "5432:5432"
deploy:
mode: global
placement:
constraints:
- node.role==manager
labels:
- "traefik.enable=true"
- "traefik.docker.network=traefik-public"
- "traefik.http.middlewares.https-redirect.redirectscheme.scheme=https"
- "traefik.http.middlewares.https-redirect.redirectscheme.permanent=true"
- "traefik.http.routers.traefik-public-http.rule=Host(traefik.dynhyp.com)"
- "traefik.http.routers.traefik-public-http.entrypoints=http"
- "traefik.http.routers.traefik-public-http.middlewares=https-redirect"
- "traefik.http.routers.traefik-public-https.rule=Host(traefik.dynhyp.com)"
- "traefik.http.routers.traefik-public-https.entrypoints=https"
- "traefik.http.routers.traefik-public-https.tls=true"
- "traefik.http.routers.traefik-public-https.service=api@internal"
- "traefik.http.routers.traefik-public-https.tls.certresolver=stagingresolver"
- "traefik.http.services.traefik-public.loadbalancer.server.port=80"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "traefik-certificates:/certificates"
command:
- "--providers.docker"
- "--providers.docker.exposedbydefault=false"
- "--providers.swarm.endpoint=unix:///var/run/docker.sock"
- "--entrypoints.http.address=:80"
- "--entrypoints.https.address=:443"
- "--entrypoints.postgres.address=:5432"
- "--certificatesresolvers.stagingresolver.acme.email=berk.xxxxx@gmail.com"
- "--certificatesresolvers.stagingresolver.acme.tlschallenge=true"
- "--certificatesresolvers.stagingresolver.acme.storage=/certificates/acme.json"
- "--certificatesresolvers.stagingresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
- "--accesslog"
- "--log"
- "--api.dashboard=true"
- "--api"
networks:
- traefik-public

customer_000001_postgres:
image: postgres:latest
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres001
volumes:
- customer_000001:/var/lib/postgresql/data
networks:
- traefik-public
deploy:
mode: global
placement:
constraints:
- node.role==manager
labels:
- "traefik.enable=true"
- "traefik.tcp.routers.customer_000001_postgres.entrypoints=postgres"
- "traefik.tcp.routers.customer_000001_postgres.rule=HostSNI(customer1.example.com)"
- "traefik.tcp.routers.customer_000001_postgres.tls=true"
- "traefik.tcp.routers.customer_000001_postgres.tls.certresolver=stagingresolver"
- "traefik.tcp.services.customer_000001_postgres.loadbalancer.server.port=5432"

customer_000002_postgres:
image: postgres:latest
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres002
volumes:
- customer_000002:/var/lib/postgresql/data
networks:
- traefik-public
deploy:
mode: global
placement:
constraints:
- node.role==manager
labels:
- "traefik.enable=true"
- "traefik.tcp.routers.customer_000002_postgres.entrypoints=postgres"
- "traefik.tcp.routers.customer_000001_postgres.rule=HostSNI(customer2.example.com)"
- "traefik.tcp.routers.customer_000002_postgres.tls=true"
- "traefik.tcp.routers.customer_000002_postgres.tls.certresolver=stagingresolver"
- "traefik.tcp.services.customer_000002_postgres.loadbalancer.server.port=5432"

volumes:
traefik-certificates:
driver: local
driver_opts:
type: nfs
o: addr=10.10.10.131,nfsvers=4
device: ":/mnt/nfsdisk/certificates"
customer_000001:
driver: local
driver_opts:
type: nfs
o: addr=10.10.10.131,nfsvers=4
device: ":/mnt/nfsdisk/customer_000001/postgres_data"
customer_000002:
driver: local
driver_opts:
type: nfs
o: addr=10.10.10.131,nfsvers=4
device: ":/mnt/nfsdisk/customer_000002/postgres_data"

networks:
traefik-public:
external: true


deploy:
  mode: replicated
  replicas: 1
  placement:
    constraints:
      - node.role==worker

the error I saw in the traefik logs:

traefik_traefik.0.yd3iy22ojsa2@ubntsrv01 | 2024-07-12T13:22:40Z ERR Error while dialing backend error="dial tcp 10.0.1.42:5432: i/o timeout"

1 Like