How to Install K3s with NGINX as Ingress Controller

In one of our last posts, we saw how quickly and easy can spin up a Kubernetes cluster using the K3s distribution provided by Rancher. One of the cool things of K3s is that the default installation comes with Traefik ingress controller, which is very useful, as we don't have to go through the process of installing an ingress controller. We get all that we want for our Runtime Fabric with just one command.

However, what if we need to use another ingress controller that is not Traefik? What if we wanted to have our K3s cluster with NGINX? In this post we're going to see how we can install K3s without Traefik and then install NGINX as ingress controller.

Installation command with Traefik disabled

As we saw in this post,  we only need to run one command to install K3s and we can add environment variables and flags to that command to customize the installation. Remember, this was the command we came up for a custom installation of K3s for a Runtime Fabric cluster:


curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" INSTALL_K3S_VERSION=<VERSION> INSTALL_K3S_EXEC="--tls-san <public instance hostname>" sh -s -

Where:
  • INSTALL_K3S_EXEC="—tls-san <public instance hostname>" - This will add the hostname of our server to the certificate. I normally use an AWS EC2 instance, so this is the public DNS name of the instance.
  • K3S_KUBECONFIG_MODE="644" - which is equivalent to add —write-kubeconfig-mode 644 to the previous env variable.
  • INSTALL_K3S_VERSION - with this we specify the K3s version. We need to use a Kubernetes version supported by Runtime Fabric. Remember that the latest K8s version is usually not supported straight away. In order to get the equivalent K3s version we'll check this compatibility matrix in the official docs. For example, we can see that for the K8s version v1.30.0 we'll be using the version v1.30.0+k3s1 in K3s

To skip the Traefik installation we just need to add the --disable traefik flag to the INSTALL_K3S_EXEC env variable. 

Putting all together, the command we are after is:
curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" INSTALL_K3S_VERSION=<VERSION> INSTALL_K3S_EXEC="--disable traefik --tls-san <public instance hostname>" sh -s -

After a few seconds, your new K3s cluster would be ready. Verify the cluster is running with kubectl get nodes


And lastly, verify that traefik is not installed with kubectl get pods -n kube-system. You should not see any pod related to traefik in the kube-system namespace.

Install NGINX Ingress Controller

There are multiple ways of installing NGINX as ingress controller for our K8s cluster. The quickest way is to run the following command:

curl kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/cloud/deploy.yaml

(Note: this will do a default installation. If we need to customize NGINX a better way to do it is using Helm, where we can change the values of some of the installation parameters)

After that,  verify everything was installed correctly
kubectl get all -n ingress-nginx


Verify the LoadBalancer service has been created correctly (it may take a few minutes):


Even though the EXTERNAL-IP shows a Private IP, internally all the requests directed to the public DNS name of your VM will be redirected to this endpoint, that is, to your NGINX ingress service. That means you can use this public DNS name for your RTF ingress resource template and it will work.
Previous Post Next Post