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

It is based on my last post

The result of that was an “empty” cluster without any useful services. In my first post on installing K3s I created an ingress controller based on HAProxy. At that time I was not aware that K3s already includes an ingress controller that is ready to use.

This time, four Kubernetes resources are needed:

  • ConfigMap
  • Deployment
  • Service
  • Ingress

The ConfigMap is a quick way to make the index.html available to nginx. It is not a clean way of hosting a website and is used only for this tutorial.

First, create the index.html:

<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 create the ConfigMap:

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

The remaining manifests are similar to the previous tutorial, except for 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

Apply everything with:

kubectl apply -f nginx.yaml

Check the IP where the website can be reached:

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

Try it with:

wget 12.345.6.789

This should return the HTML that was added to the ConfigMap at the start of this tutorial.

Resources