2 min read

How to configure Traefik on k3s?

How to configure Traefik on k3s?

Configure Traefik on the k3s

To install the K3s, on the new VM instance, you can simply run the script in the terminal:

curl -sfL https://get.k3s.io | sh -

After the installation and initial setup process (which can take a few minutes), you can access the k3s cluster using the kube config file located at the /etc/rancher/k3s/k3s.yaml.

You can easily use this file as a Kube config exporting KUBECONF environment variable.

export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
ubuntu@k3s:~$ k get pods -n kube-system
NAME                                      READY   STATUS
local-path-provisioner-6c79684f77-zv678   1/1     Running
coredns-d76bd69b-j9b6z                    1/1     Running
metrics-server-7cd5fcb6b7-g5ff6           1/1     Running
helm-install-traefik-crd-w7b52            0/1     Completed
helm-install-traefik-tnt4w                0/1     Completed
svclb-traefik-c7zxl                       2/2     Running
traefik-df4ff85d6-w8wn4                   1/1     Running
💡
It can be possible for path /etc/rancher to be under root ownership so you should set permissions accordingly for the user. For the current setup: sudo chown $USER /etc/rancher/k3s/k3s.yaml.

As you can see traefik-df4ff85d6-w8wn4 pod is created.

You can modify the Traefik installation using static and dynamic configuration. See more details at the Traefik static and dynamic configuration.

To modify Traefik on the k3s you need to redeploy it via HelmChartConfig.

Redeploy Traefik on the k3s

To configure Traefik create a new YAML file and provide the needed details as shown below.

apiVersion: helm.cattle.io/v1
kind: HelmChartConfig
metadata:
  name: traefik
  namespace: kube-system
spec:
  valuesContent: |-
    additionalArguments:
      - "--api"
      - "--api.dashboard=true"
      - "--api.insecure=true"
      - "--log.level=DEBUG"
    ports:
      traefik:
        expose: true
    providers:
      kubernetesCRD:
        allowCrossNamespace: true

K3s is managing Traefik using CRs: HelmChartConfig and HelmChart. YAML definition above creates HelmChartConfig object on the k3s cluster and k3s will reconfigure the already installed k3s helm charts.

The arguments are passed via additionalArguments - this is modifying the Traefik static parameters. Needed arguments can be added easily.

It's important to say that you need to specify namespace as kube-system, since Traefik should be running there.

When you run kubectl apply -f traefik-custom-conf.yaml, Traefik will be restarted and the new pod will pick up the configuration provided (Can take some time).

If you want to persist the changes on k3s restart you can create a new file in the /var/rancher/k3s/server/manifests/ named traefik-anything-you-want.yaml and k3s will pick up the configuration on file change and k3s restarts.

Be sure to persist already created traefik.yaml and traefik-crd.yaml files in the path mentioned above.

Do you need a reliable freelancer to take your project back on track?

I am ready to deliver!

Learn more about my services