From 0 to Kubernetes – Step 3, deploying Traefik

The next step here is to deploy traefik.

Traefik is a http(s) and TCP/UDP loadbalancer, that basically does the same kind of job that the router in openshift does: it accepts http and https connections, as well as generic tcp and udp traffic, and routes it to the right pods according to rules.

As an example, you could deploy two separate web servers in K3s, and they would run in different pods on the same worker node, and traefik would look at the SNI information in the HTTP request, and redirect the request to the proper container.

If your K3s “cluster” is accessible from the outside (i.e. the internet) you can just follow the instructions in this article here. That will set you up with traefik on your K3s, and for HTTPS it would automatically create Let’s Encrypt certificates.

If your K3s is not reachable from the outside, you need to have your own way of doing certificates – hence that post about prerequisites.

Before I go into detail here’s the long awaited explanation as to why we actually disabled traefik during K3s installation: Simply said, the traefik version that gets deployed with K3s is not the most recent release… and we want that part of your setup to be up to date, after all, so let’s do it manually.

What you have to have ready at this point is all the stuff from the prerequisites post.

But first lets deploy the Custom Resources according to that article I mentioned earlier.

kubectl apply -f

Next, we need to create a secret in K3s where we store the custom ssl certificate we created at the beginning (Step 1)

kubectl create secret tls appsrv --cert=/path/to/certificate/file --key=/path/to/key/file

The next step is to deploy Traefik:

kubectl apply -f /path/to/your/deployment/file.yaml

I’ve used the one here which will set up Traefik with enforced https via 301 redirects for all http hosts – so we definitely want that certificate we created to be the default for all https routes.

To get it to that point all we need is a tls store that is named “default” and points at the secret we created two steps ago:

Here is my yaml file for it, so a simple

kubectl apply -f

will do the trick nicely.

Finally, we need to test it, so let’s look at the traefik dashboard first, with should be at and look like this:

For a full test we need to actually deploy something –  so here is a small deployment for the webserver that is also used in the original article on traefik. Of course you will have to change the rule in line 48 to match your own network – simply use any random hostname that gets handled by *.apps.yourdomain in your nameserver, see Step 1.

The one interesting bit is the part at the end, where it says “tls: {}” which basically tells traefik to use the default certificate for this route.

Once everything is deployed you should be able to access, get redirected to and then get some output similar to this:

mathias@appsrv:~$ curl -k
Hostname: whoami-app-7564dd9695-j48zn
IP: ::1
IP: fe80::5020:59ff:feda:a0de
GET / HTTP/1.1
User-Agent: curl/7.64.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-Port: 443
X-Forwarded-Proto: https
X-Forwarded-Server: traefik-6dd5886ddf-hh9zh

Continued here.

1 thought on “From 0 to Kubernetes – Step 3, deploying Traefik

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: