- Before we begin, lets cleanup any running Docker stack
docker stack rm traefik
If you named you stack something else use your specified name. If you don't remember rundocker stack ls
- Change to the
03-Routers-and-Services
folder - Open the
docker-compose.yml
file in your favorite editor and review thecatapp
section - Start Traefik and the
catapp
but 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
catapp
does not have a router or service in Traefik. - From the
03-Routers-and-Services
edit thedocker-compose.yml
file and add our first label to thecatapp
as 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-Services
directory execute this command ->docker stack deploy -c docker-compose.yml traefik
this 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
catapp
Router & Service. What do you see?
catapp
is 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-Services
directory edit thedocker-compose.yml
file and add the Labels to thecatapp
for 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 ourcatapp
ascatapp.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 newcatapp
section thedocker-compose.yml
file 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-Services
directory execute this command ->docker stack deploy -c docker-compose.yml traefik
this 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_traefik
Note 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
catapp
Router & Service. What do you see?
OK, so we broke our catapp
What exactly shall we do? Let's investigate why catapp
is not working with Traefk.
- Open the Traefik Dashboard http://0.0.0.0:8080 and notice we have a Router Error.
- Click on our
catapp
Router 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
catapp
click the on the Service ->catapp
What do you see?
- When you click the
catapp
service 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
catapp
service?
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-Services
directory edit thedocker-compose.yml
file and add the Label to thecatapp
to define the Load Balancer port- "traefik.http.services.catapp.loadbalancer.server.port=5000"
by removing the comment - From the
03-Routers-and-Services
directory execute this command ->docker stack deploy -c docker-compose.yml traefik
this 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@docker
as 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-Services
directory edit thedocker-compose.yml
file 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-Services
directory execute this command ->docker stack deploy -c docker-compose.yml traefik
this 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"