All Articles

Docker Stack with Traefik

I’ve created a simple docker stack to demonstrate Traefik

Træfik (pronounced like traffic) is a modern HTTP reverse proxy and load balancer made to deploy microservices with ease. It supports several backends (Docker, Swarm mode, Kubernetes, Marathon, Consul, Etcd, Rancher, Amazon ECS, and a lot more) to manage its configuration automatically and dynamically.

Source: https://traefik.io/

We’ll use Docker Swarm as the backend in this example.

Save the compose file below as traefik-stack.yml. You can also download it from my github repo

version: "3.3"
services:
  traefik:
    image: traefik
    command: --web \
      --docker \
      --docker.swarmmode \
      --docker.domain=traefik \
      --docker.watch \
      --logLevel=DEBUG
    networks:
      - traefik-net
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /dev/null:/traefik.toml
    deploy:
      placement:
        constraints: [node.role==manager]

  portainer:
    image: portainer/portainer
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "portainerdata:/data"
    networks:
      - traefik-net
    deploy:
      placement:
        constraints: [node.role == manager]
      labels:
        - "traefik.enable=true"
        - "traefik.port=9000"
        - "traefik.docker.network=mystack_traefik-net"
        - "traefik.frontend.rule=Host:portainer.traefik"

  ms1:
    image: melvindave/spring-boot-example
    networks:
      - traefik-net
    depends_on:
      - mongodb
    deploy:
      replicas: 1
      labels:
        - "traefik.enable=true"
        - "traefik.port=8080"
        - "traefik.docker.network=mystack_traefik-net"
        - "traefik.frontend.rule=Host:ms1.traefik"

  mongodb:
    image: mongo
    volumes:
      - "mongodata:/data/db"
    networks:
      - traefik-net
    deploy:
      replicas: 1
    ports:
      - "27017:27017"  

networks:
  traefik-net:

volumes:
   mongodata:

To run this stack, you need to use Swarm Mode. Once you have a cluster, execute this command.

docker stack deploy -c traefik-stack.yml mystack

Add these domain names in your hosts file to simulate DNS resolution, change the IP ==192.168.1.9== with your Swarm manager’s IP address.

192.168.1.9	portainer.traefik ms1.traefik mongo.traefik

==192.168.1.9== your Swarm manager’s IP address

Once the stack has been deployed, you can access Traefik’s web console at http://192.168.1.9:8080

Screenshot-2017-11-07-00.01.17

All traffic to the backend services can now be routed using port 80

http://portainer.traefik --> portainer service running on port 8080

Screenshot-2017-11-07-00.37.04

http://ms1.traefik --> ms1 service running on port 9000

Screenshot-2017-11-07-00.37.21

To cleanup, since we’re using docker stack, it is just one command

docker stack rm mystack

I’m not a network expert so if you see something wrong with the demo, let me know!

References: