Logo

dev-resources.site

for different kinds of informations.

Traefik v2 with Docker Swarm

Published at
12/6/2019
Categories
docker
traefik
swarm
Author
ohffs
Categories
3 categories in total
docker
open
traefik
open
swarm
open
Author
5 person written this
ohffs
open
Traefik v2 with Docker Swarm

Traefik v2 with Docker Swarm

I've been a happy user of Traefik all through the v1.x series but with v2.1 coming out I began to have a proper look at upgrading. The docs are very thorough, but as with a lot of thorough docs also not very enlightening about 'how do I do the thing?'.

So after a bit of faffing about, watching yotube videos (the files here are modified versions of the compose-style ones attached to the video) etc I've got something running. This is a very basic 'just get it up and running' example - mostly as an aide-memoire for myself and hopefully to give some pointers to other people migrating from v1 to v2. I'm assuming familiarity with Traefik v1 so I'm not documenting everything line by line.

The stack files

Our setup is a traefik instance running listening on an overlay network called 'proxy'. Any web apps that need to talk to the outside world also sit on that network and have the magic traefik labels set so they get picked up. So the v2 traefik file I have so far is :

version: "3.3"

services:
  traefik:
    image: traefik:v2.0
    restart: always
    container_name: traefik
    ports:
      - "80:80"
      - "8080:8080" # traefik dashboard
      - "443:443"
    command:
      - --api.insecure=true # set to 'false' on production
      - --api.dashboard=true # see https://docs.traefik.io/v2.0/operations/dashboard/#secure-mode for how to secure the dashboard
      - --api.debug=true # enable additional endpoints for debugging and profiling
      - --log.level=DEBUG # debug while we get it working, for more levels/info see https://docs.traefik.io/observability/logs/
      - --providers.docker=true
      - --providers.docker.swarmMode=true
      - --providers.docker.exposedbydefault=false
      - --providers.docker.network=proxy
      - --entrypoints.web.address=:80
      - --entrypoints.web-secured.address=:443
      - --certificatesresolvers.mytlschallenge.acme.httpChallenge.entrypoint=web
      - --certificatesresolvers.mytlschallenge.acme.email=you@whatever.com
      - --certificatesresolvers.mytlschallenge.acme.storage=/letsencrypt/acme.json
    volumes:
      - letsencrypt:/letsencrypt
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - proxy
    deploy:
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.api.rule=Host(`traefik.yourdomain.com`)"
        - "traefik.http.routers.api.service=api@internal" # Let the dashboard access the traefik api

networks:
  proxy:
    external: true

volumes:
  letsencrypt:

And a basic example wordpress stack file :

version: "3.3"

services:

  wordpress:
    image: wordpress
    restart: always
    container_name: wp
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html
    networks:
      - proxy
      - backend
    deploy:
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.wordpress.rule=Host(`wordpress.yourdomain.com`)"
        - "traefik.http.routers.wordpress.entrypoints=web"
        - "traefik.http.services.wordpress.loadbalancer.server.port=80" # it seems you always need to give traefik a port so it 'notices' the service
        - "traefik.http.routers.wordpress-secured.rule=Host(`wordpress.yourdomain.com`)"
        - "traefik.http.routers.wordpress-secured.entrypoints=web-secured"
        - "traefik.http.routers.wordpress-secured.tls.certresolver=mytlschallenge"

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
    networks:
      - backend

networks:
  backend:
  proxy:
    external: true

volumes:
  db:
  wordpress:

Using it

# assuming you are on a swarm master node
docker network create --driver=overlay proxy
docker stack deploy -c traefik.yml traefik
docker stack deploy -c wordpress.yml wordpress

After a short delay you should be able to visit the urls defined in the stack files on both http and https.

CI/CD

As each traefik-enabled service now has labels that have names to make them unique (eg, traefik.http.routers.wordpress.entrypoints=web) having a stack file with something like traefik.http.routers.${STACK_NAME}.entrypoints=web, traefik.http.routers.${STACK_NAME}-secured.entrypoints=web-secured is probably worth thinking about so you can do :

export STACK_NAME=wordpress
docker stack deploy -c wordpress.yml ${STACK_NAME}

and tie things together.

Further

Obviously this is a very basic setup. To take this into production you'd be looking at consul for the letsencrypt store, sensible deploy: flags, not giving traefik access
to the docker socket directly etc. But as a 'how on earth do I use v2' I hope it helps someone and saves them having to dig through things for
as long as I did.

swarm Article's
30 articles in total
Favicon
Knowledgeable Agents with FalkorDB Graph RAG
Favicon
Building Swarm-based agents with AG2
Favicon
Quick tip: Using SingleStore with OpenAI's Swarm
Favicon
OpenAI Swarm: Exploring Lightweight Multi-Agent Orchestration
Favicon
Building an 🐝 OpenAI SWARM πŸ” Web Scraping and Content Analysis Streamlit Web App with πŸ‘₯ Multi-Agent Systems
Favicon
What’s an AI Agent and what are its current advantages and possible future?
Favicon
Docker Swarm
Favicon
Docker Swarm Series: #7th Advanced Managing config and secret objects
Favicon
Docker Swarm Series: #5th Troubleshooting
Favicon
Docker Swarm Series: #8th Publishing Modes
Favicon
DOCKER SWARM CLUSTER & NFS
Favicon
Docker Swarm Series: #6th Managing config and secret objects
Favicon
Docker Swarm: Simplifying Container Orchestration at Scale
Favicon
Docker Swarm Series: #4th Deploy a Stack to a swarm Cluster
Favicon
Docker Swarm Series: #3rd Deploy a highly available Container
Favicon
Docker Swarm Series: #1st Setup the Environment
Favicon
Docker in small scale production (Docker Swarm)
Favicon
How to export your complete Foursquare checkin history
Favicon
Deploy a high available etcd cluster using docker
Favicon
Time to say goodbye to Docker Swarm
Favicon
Understanding Docker, Docker Compose & Swarm
Favicon
Everything you need to know about Docker Swarm
Favicon
Zero Downtime Deployment with Docker Swarm
Favicon
Workflow on Docker Swarm
Favicon
Free docker cluster mesh with swarm and GCP
Favicon
Traefik v2 with Docker Swarm
Favicon
swarm-cronjob
Favicon
Docker Swarm Concepts, Tips, and Tricks for a Docker Beginner
Favicon
Deploying gitlab on Docker Swarm
Favicon
Adventure with Docker: Conflicts with UIDs of the container and the host

Featured ones: