Install NGINX Ingress on K3S

1. Install K3S

To install K3S, execute the following command with an explanation for INSTALL_K3S_EXEC:

  • --disable traefik, servicelb: Remove traefik and servicelb

  • --tls-san: Add additional hostnames or IPv4/IPv6 addresses as Subject Alternative Names on the TLS cert, so the controller plane can connect using IP of the server

sudo curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--disable traefik,servicelb --tls-san $IP_SERVER" K3S_KUBECONFIG_MODE="644" sh -

2. Install NGINX Ingress Controller

There are several ways to install but we use helm for simplicity.

We will use nginx-ingress-release as the name of the Chart and installing it on nginx-ingress namespace. Since we installed it on a Virtual Private Server (VPS), we're changing the service type to NodePort. This is necessary because the default setting uses LoadBalancer type, which may not be suitable for our VPS environment. Additionally, set the hostNetwork to true so that we can access it via a domain.

$ kubectl create namespace nginx-ingress
namespace/nginx-ingress created

$ helm install nginx-ingress-release oci://ghcr.io/nginxinc/charts/nginx-ingress --version 1.1.0 --namespace nginx-ingress --set controller.service.type=NodePort --set controller.kind=daemonset --set controller.hostNetwork=true
Pulled: ghcr.io/nginxinc/charts/nginx-ingress:1.1.0
Digest: sha256:47eddf60256ad1b0f98596f41d767d9d4cd6ec81f54a284391a4763261c78d3b
NAME: nginx-ingress-release
LAST DEPLOYED: Wed Jan  3 17:20:47 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The NGINX Ingress Controller has been installed.

3. Verify the installation

$ kubectl get daemonset,pods -n nginx-ingress
NAME                                              DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
daemonset.apps/nginx-ingress-release-controller   2         2         2       2            2           <none>          34m

NAME                                         READY   STATUS    RESTARTS   AGE
pod/nginx-ingress-release-controller-7szw5   1/1     Running   0          34m
pod/nginx-ingress-release-controller-csl79   1/1     Running   0          34m

4. Install simple application

In this example, we will install simple Apache deployment.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apache-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apache
  template:
    metadata:
      labels:
        app: apache
    spec:
      containers:
      - name: apache-container
        image: httpd:latest
        ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: apache-service
spec:
  selector:
    app: apache
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

5. Expose the application using Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: apache-ingress
  namespace: app
spec:
  rules:
  - host: apache.example.id  # Replace with your actual domain
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: apache-service
            port:
              number: 80

6. Access the application

To access the application, go to apache.example.id.