docs(examples): add multiple containers example

This commit is contained in:
Alexis COUVREUR
2021-10-01 08:48:02 +02:00
parent 7663b06403
commit 60a81100ef
3 changed files with 90 additions and 0 deletions

View File

@@ -56,6 +56,7 @@ The docker library that interacts with the docker deamon uses `unsafe` which mus
- [Docker Classic](./examples/docker_classic/)
- [Docker Swarm](./examples/docker_swarm/)
- [Multiple Containers](./examples/multiple_containers/)
## Authors

View File

@@ -0,0 +1,24 @@
# Docker swarm
## Run the demo
1. `git clone git@github.com:acouvreur/traefik-ondemand-plugin.git`
2. `cd traefik-ondemand-plugin/examples/multiple_containers`
3. `docker swarm init`
4. `export TRAEFIK_PILOT_TOKEN=...`
5. `docker stack deploy -c docker-stack.yml DOCKER_SWARM`
6. Load `http://localhost/nginx`
7. Load `http://localhost/whoami`
8. After 1 minute whoami is scaled to 0/0
9. After 5 minutes nginx is scaled to 0/0
10. `docker stack rm DOCKER_SWARM`
## Limitations
### Define a middleware per service/container
Due to Traefik plugin, the interface is to provide a config and a `ServeHTTP` request.
This function has no access to the Traefik configuration, thus no way to determine the container/service associated to the request.
See https://github.com/acouvreur/traefik-ondemand-plugin/issues/8#issuecomment-931940533.

View File

@@ -0,0 +1,65 @@
version: "3.9"
services:
traefik:
image: traefik
command:
- --api=true
- --api.insecure=true
- --pilot.token=$TRAEFIK_PILOT_TOKEN
- --experimental.plugins.traefik-ondemand-plugin.moduleName=github.com/acouvreur/traefik-ondemand-plugin
- --experimental.plugins.traefik-ondemand-plugin.version=v0.1.1
- --providers.docker=true
- --providers.docker.swarmmode=true
- --providers.file.filename=/etc/traefik/dynamic-config.yml
- --entrypoints.http.address=:80
- --entrypoints.https.address=:443
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
ondemand:
image: ghcr.io/acouvreur/traefik-ondemand-service:main
command:
- --swarmMode=true
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
deploy:
labels:
- traefik.enable=true
- traefik.http.middlewares.ondemand-nginx.plugin.traefik-ondemand-plugin.name=DOCKER_SWARM_nginx
- traefik.http.middlewares.ondemand-nginx.plugin.traefik-ondemand-plugin.serviceUrl=http://ondemand:10000
- traefik.http.middlewares.ondemand-nginx.plugin.traefik-ondemand-plugin.timeout=5m
- traefik.http.middlewares.ondemand-whoami.plugin.traefik-ondemand-plugin.name=DOCKER_SWARM_whoami
- traefik.http.middlewares.ondemand-whoami.plugin.traefik-ondemand-plugin.serviceUrl=http://ondemand:10000
- traefik.http.middlewares.ondemand-whoami.plugin.traefik-ondemand-plugin.timeout=1m
- traefik.http.services.ondemand.loadbalancer.server.port=10000
nginx:
image: nginx
deploy:
replicas: 0
labels:
- traefik.enable=true
# If you do not use the swarm load balancer, traefik will evict the service from its pool
# as soon as the service is 0/0. If you do not set that, fallback to dynamic-config.yml file usage.
- traefik.docker.lbswarm=true
- traefik.http.routers.nginx.middlewares=ondemand-nginx@docker
- traefik.http.routers.nginx.rule=PathPrefix(`/nginx`)
- traefik.http.services.nginx.loadbalancer.server.port=80
whoami:
image: containous/whoami
deploy:
replicas: 0
labels:
- traefik.enable=true
- traefik.docker.lbswarm=true
- traefik.http.routers.whoami.middlewares=ondemand-whoami@docker
- traefik.http.routers.whoami.rule=PathPrefix(`/whoami`)
- traefik.http.services.whoami.loadbalancer.server.port=80