Install Nginx As K3s Ingress Controller to Replace Traefik

Posted December 7, 2024
Installing Nginx K3s Ingress Controller and Replace Traefik

Traefik and Nginx work as ingress controllers for Kubernetes. These two are the most popular options for K3s. They let you expose K3s services to direct traffic to your app. K3s Kubernetes uses Traefik as its default ingress controller.

The question is can you replace it with a Nginx ingress controller? You are not limited to this option. If Nginx is your ingress controller, it works perfectly with K3s. This hands-on guide dives into installing and deploying Nginx instead of default Traefik as your ingress controller on K3s.

A little About Nginx and Traefik as K3s Ingress Controllers

Once you install K3s in your machine, a default Traefik K3s Ingress Controllers will be created:

About Nginx and Traefik as K3s Ingress Controllers

Traefik is enough to accommodate all your K3s cluster’s needs. But Nginx is:

  • Multi-architecture for x86 and ARM.
  • Has a mature ecosystem.
  • Nginx has high performance for HTTP and TCP/UDP traffic.
  • It’s known for stability under high-load scenarios.

You will find yourself with such unique cases and k3s will need Nginx to replace traefik.

Installing K3s Without Traefik Controller

You normally use the following command to install K3s:

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

A quick kubectl get pods -n kube-system command will tell you to have traefik pods read as such:

Installing K3s Without Traefik Controller

It’s a confirmation you installed K3s with Traefik. To use Nginx, you’ll need to let your machine install K3s without Traefik with this INSTALL_K3S_EXEC command:

curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --disable=traefik" K3S_KUBECONFIG_MODE="644" sh -s -

By any chance, if you already had K3s installed with Traefik, don’t uninstall it. Instead, run the same curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --disable=traefik" K3S_KUBECONFIG_MODE="644" sh -s -.

Next use:

  • The sudo rm -rf /var/lib/rancher/k3s/server/manifests/traefik.yaml to remove Traefik files
  • Then run the following commands and remove Traefik pods.
sudo kubectl delete -n kube-system helmcharts.helm.cattle.io traefik
sudo kubectl delete -n kube-system svc traefik
sudo kubectl delete -n kube-system deploy traefik
sudo kubectl delete -n kube-system daemonset traefik
  • Now restart the K3s server with:
sudo systemctl daemon-reload
sudo systemctl restart k3s`

At this point, the K3s systemd service file should indicate an ExecStart argument with --disable traefik. Open /etc/systemd/system/k3s.service file and confirm so:

The K3s systemd service file

Check the available pods with the kubectl get pods -n kube-system command:

Check the available pods

K3s is still okay but Traefik is not running.

Deploying Nginx as K3s Traefik Controller

How you deploy Nginx depends on the environment you are using K3s on. For this case, I will use Bare metal clusters. However, if you need different deployments like clouds and local development, check this Nginx installation guide.

Use the following command to install Nginx:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.12.0-beta.0/deploy/static/provider/baremetal/deploy.yaml

This command should be used if you have K3s installed manually with generic Linux distros like Ubuntu.

Now run the `kubectl get pods -A` to check if Nginx Ingress is READY:
root@kim-G3:~# kubectl get pods -A
ingress-nginx   ingress-nginx-admission-create-2bqq5        0/1     Completed          0          7m31s
ingress-nginx   ingress-nginx-admission-patch-jgjm2         0/1     Completed          0          7m31s
ingress-nginx   ingress-nginx-controller-5676944cc5-b59zk   0/1     Pending            0          7m31s
kube-system     coredns-7b98449c4-rxkwc                     1/1     Running            0          12m
kube-system     local-path-provisioner-595dcfc56f-4bt5b     1/1     Running            0          12m
kube-system     metrics-server-cdcc87586-8ltxh              1/1     Running            0          12m

If you get a Pending or ImagePullBackOff ingress-nginx-controller pod status try rerunning the kubectl apply ... command and check the pods again.

Everything is all ready. Your app deployments should now use Ingress as the Nginx Controller. Here is an example:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  namespace: default
spec:
  rules:
  ## Domain (REMEBER TO SET DNS)
 - host: example.local
      http:
        paths:
 - path: /
            pathType: Prefix
            backend:
              service:
                name: example-service
                port:
                  number: 80

Conclusion

That’s all you need to install Nginx as the K3s Ingress Controller alternative and replace the default Traefik controller. Thank you for checking the guide and I hope it helped you.

Install Nginx As K3s Ingress Controller to Replace Traefik

Written By:

Joseph Chege