- Before we begin, lets cleanup any running Docker stack
docker stack rm traefikIf you named you stack something else use your specified name. If you don't remember rundocker stack ls - Change to the
03-Routers-and-Servicesfolder - Open the
docker-compose.ymlfile in your favorite editor and review thecatappsection - Start Traefik and the
catappbut with no labelsdocker stack deploy -c docker-compose.yml traefik - Open the Traefik Dashboard http://0.0.0.0:8080 and verify Traefik is running and
catappdoes not have a router or service in Traefik. - From the
03-Routers-and-Servicesedit thedocker-compose.ymlfile and add our first label to thecatappas seen below. We are adding/removing the comment from thelabels:option and uncomment the Traefik label- "traefik.enable=true"to enable catapp inside Traefik.
catapp:
image: mikesir87/cats:1.0
labels:
- "traefik.enable=true"- From the
03-Routers-and-Servicesdirectory execute this command ->docker stack deploy -c docker-compose.yml traefikthis will update our Docker Swarm Stack with the new Label changes. Changes take about 10-15 seconds to apply - Open the Traefik Dashboard http://0.0.0.0:8080 and review the
catappRouter & Service. What do you see?
catappis now running but with default configurations provided from Traefik. Now, we will set additional Labels to define the Router Rule, Entrypoint, and service.
- From the
03-Routers-and-Servicesdirectory edit thedocker-compose.ymlfile and add the Labels to thecatappfor a Router rule, define the Entrypoint, and service as seen below. Uncomment the- "traefik.http.routers.catapp.rule=Host(catapp.localhost)"to define the hostname of ourcatappascatapp.localhost. Next, we define which Entrypoint to use with the Label uncomment- "traefik.http.routers.catapp.entrypoints=web"to define HTTP/web Entrypoint. Finally, we uncommet the next label- "traefik.http.routers.catapp.service=catapp"to tell the Router which Service to use. This is for Demo purposes only as normally Traefik pulls this configuration automatically. The newcatappsection thedocker-compose.ymlfile should look like this:
catapp:
image: mikesir87/cats:1.0
labels:
- "traefik.enable=true"
- "traefik.http.routers.catapp.rule=Host(`catapp.localhost`)"
- "traefik.http.routers.catapp.entrypoints=web"
- "traefik.http.routers.catapp.service=catapp"- From the
03-Routers-and-Servicesdirectory execute this command ->docker stack deploy -c docker-compose.yml traefikthis will update our Docker Swarm Stack with the new Label changes. Changes take about 10-15 seconds to apply - Review the logs output
docker service logs traefik_traefikNote the name comes from Stack name + service name. So if you used a different Stack name you need to rundocker service logs <your-stack-name_traefik> - The last line in the log file should indicate the issue
- Open the Traefik Dashboard http://0.0.0.0:8080 and review the
catappRouter & Service. What do you see?
OK, so we broke our catapp What exactly shall we do? Let's investigate why catappis not working with Traefk.
- Open the Traefik Dashboard http://0.0.0.0:8080 and notice we have a Router Error.
- Click on our
catappRouter to view the error. Do you notice the error is the same as what we saw in the logs?
- In the **Router Details of the
catappclick the on the Service ->catappWhat do you see?
- When you click the
catappservice from the Router Details screen you should have been greeted by an errorService not found catapp@docker - In the Traefik Dashboard click the HTTP Services Menu
- Look at the services, what do you notice about the
catappservice?
NOTE Take a look at the name of the catapp service. You will notice the name is random generated. traefik-catapp-random
- From the
03-Routers-and-Servicesdirectory edit thedocker-compose.ymlfile and add the Label to thecatappto define the Load Balancer port- "traefik.http.services.catapp.loadbalancer.server.port=5000"by removing the comment - From the
03-Routers-and-Servicesdirectory execute this command ->docker stack deploy -c docker-compose.yml traefikthis will update our Docker Swarm Stack with the new Label changes. - Open the Traefik Dashboard http://0.0.0.0:8080 and wait about 15-20 seconds for the changes to be visible in the dashboard. Notice that the Router error is now cleared.
- Navigate to the Router menu and notice everything is now healthy
- Navigate to the Services menu and notice the service is now named
catapp@dockeras intended
In this Lab we will comment out the Service and Load Balancer Labels to see how Traefik will Dynamically create the service and Load Balancer.
- From the
03-Routers-and-Servicesdirectory edit thedocker-compose.ymlfile and remove/add comment to the Labels- "traefik.http.routers.catapp.service=catapp"and- "traefik.http.services.catapp.loadbalancer.server.port=5000"from thecatapp. The only Labels enabled as seen below:
catapp:
image: mikesir87/cats:1.0
labels:
- "traefik.enable=true"
- "traefik.http.routers.catapp.rule=Host(`catapp.localhost`)"
- "traefik.http.routers.catapp.entrypoints=web"- From the
03-Routers-and-Servicesdirectory execute this command ->docker stack deploy -c docker-compose.yml traefikthis will update our Docker Swarm Stack with the new Label changes. - Open the Traefik Dashboard http://0.0.0.0:8080 and wait about 15-20 seconds for the changes to be visible in the dashboard.
- Navigate to the HTTP Router & HTTP Services menus. You should now see that Traefik Dynamically created service name which is now recognized by the Router
- Stop and clean-up
docker stack rm traefik
Please check the 03-Routers-and-Services/docker-compose.answers.yml if you get stuck anywhere during the lab or need a reference.
catapp:
image: mikesir87/cats:1.0
labels:
- "traefik.enable=true"
- "traefik.http.routers.catapp.rule=Host(`catapp.localhost`)"
- "traefik.http.routers.catapp.entrypoints=web"
- "traefik.http.routers.catapp.service=catapp"
- "traefik.http.services.catapp.loadbalancer.server.port=5000"


