Skip to main content

Ingress with Traefik on K3s

·2 mins

This post is a tutorial on how to expose a website hosted with nginx by using the K3s built-in ingress controller “Traefik”.

It is based on my last post

The result of this was an “empty” cluster without any “useful” services. In my first post on installing K3s I have created an ingress controller based on HAProxy. At that time I was not aware that K3s already comes with an ingress controller all up and running.

This time, four resources are needed

  • Config Map
  • Deployment
  • Services
  • Ingress

The config map is a quick and dirty way of making it possible for nginx to access the html-file. It is not a clean way of hosting a website and is used just for this purpose.

First, the index.html has to be created

<html>
<head><title>Hello World!</title>
  <style>
    html {
      font-size: 500.0%;
    }
    div {
      text-align: center;
    }
  </style>
</head>
<body>
  <div>Hello World!</div>
</body>
</html>

Then, the config map is created with

kubectl create configmap test-html --from-file index.html

The rest of the resources are similar to as it was in the last tutorial, except the volume declaration in the deployment

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    kubernetes.io/ingress.class: "traefik"
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    run: nginx
spec:
  ports:
    - port: 80
      protocol: TCP
  selector:
    app:  nginx

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 10
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: html-volume
          mountPath: /usr/share/nginx/html
      volumes:
      - name: html-volume
        configMap:
          name: test-html

Everything is applied with

kubectl apply -f nginx.yaml

Let’s check under which IP the website can be reached

$ kubectl get ingress
NAME            CLASS    HOSTS   ADDRESS        PORTS   AGE
nginx-ingress   <none>   *       12.345.6.789   80      22m

Give it a try with

wget 12.345.6.789

This should return the html that has been added to the config map in the beginning of this tutorial.

Resources #