Skip to content

Instantly share code, notes, and snippets.

@johnharris85
Created October 25, 2017 17:12
Show Gist options
  • Save johnharris85/02133e09ba9ea082bcc188cb08ed69fb to your computer and use it in GitHub Desktop.
Save johnharris85/02133e09ba9ea082bcc188cb08ed69fb to your computer and use it in GitHub Desktop.
Multiple Traefik instances in a single Docker swarm using constraints
# for i in traefik-net traefik-net2; do docker network create -d overlay $i; done
# docker stack deploy -c all-in-one-stack.yml multi-traefik-test
#
# traefik1 - UI: 8010, App: 8001
# traefik2 - UI: 8020, App: 8002
version: "3.1"
services:
traefik:
image: traefik:1.4
command: --docker \
--docker.swarmmode \
--docker.watch \
--web \
--debug=true \
--loglevel=DEBUG \
--constraints='tag==app-stack'
ports:
- 8001:80
- 8010:8080
- 8013:443
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
deploy:
restart_policy:
condition: any
mode: replicated
replicas: 1
update_config:
delay: 2s
placement:
constraints: [node.role == manager]
resources:
limits:
memory: 1G
networks:
- traefik-net
traefik2:
image: traefik:1.4
command: --docker \
--docker.swarmmode \
--docker.watch \
--web \
--debug=true \
--loglevel=DEBUG \
--constraints='tag==app-stack2'
ports:
- 8002:80
- 8020:8080
- 8023:443
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
deploy:
restart_policy:
condition: any
mode: replicated
replicas: 1
update_config:
delay: 2s
placement:
constraints: [node.role == manager]
resources:
limits:
memory: 1G
networks:
- traefik-net2
web:
image: johnharris85/simple-hostname-reporter:2
deploy:
replicas: 3
resources:
limits:
memory: 1G
labels:
- "traefik.docker.network=traefik-net"
- "traefik.port=5000"
- "traefik.frontend.rule=PathPrefix:/"
- "traefik.backend.loadbalancer.sticky=false" # To test balancing behaviour, it's set to true on web2
- "traefik.backend=app-stack"
- "traefik.tags=app-stack"
networks:
- traefik-net
web2:
image: johnharris85/simple-hostname-reporter:2
deploy:
replicas: 3
resources:
limits:
memory: 1G
labels:
- "traefik.docker.network=traefik-net2"
- "traefik.port=5000"
- "traefik.frontend.rule=PathPrefix:/"
- "traefik.backend.loadbalancer.sticky=true"
- "traefik.backend=app-stack2"
- "traefik.tags=app-stack2"
networks:
- traefik-net2
networks:
traefik-net:
external:
name: traefik-net
traefik-net2:
external:
name: traefik-net2
@EU-debenson
Copy link

EU-debenson commented May 20, 2019

I tried something similar to this and could NOT get it to work. While the consoles/UI showed only the apps with the same constraint, the routing did not work for me, except for the traefik instance that was published as port 80. We have a Host constraint on the frontend.rule which is different for each app, but otherwise, I don't see why mine would fail. I'll try yours exactly, to see if it really can work!

Thanks to your example, I believed this could work. We identified the problem in our environment where we were forwarding from from an external load balancer to the wrong traefik port. Thanks to you for making me believe this could work!

@mshahat
Copy link

mshahat commented May 26, 2019

Thanks for this, John.
Could we have some narration for this example? maybe also a diagram would be great. I'm trying to do something similar to this to have multiple Traefik instances without a config store. @EU-debenson

I tried something similar to this and could NOT get it to work. While the consoles/UI showed only the apps with the same constraint, the routing did not work for me, except for the traefik instance that was published as port 80. We have a Host constraint on the frontend.rule which is different for each app, but otherwise, I don't see why mine would fail. I'll try yours exactly, to see if it really can work!

Thanks to your example, I believed this could work. We identified the problem in our environment where we were forwarding from from an external load balancer to the wrong traefik port. Thanks to you for making me believe this could work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment