This page looks best with JavaScript enabled

How to install nginx as ingress controller for k3s

 ·  ☕ 4 min read  ·  🐧 sysadmin

I will walk you through the NGINX ingress controller installation and configuration steps in this article.

  1. Here is a video tutorial; continue reading for a list of written instructions.

Exercises to complete:

  1. install k3s securely without traefik and servicelb
  2. install nginx as ingress controller in k3s
  3. Check nodes and pods status
  4. Create a load balancer to expose NGINX ingress controller ports
  5. Create a namespace test
  6. Create an example for testing
  7. Test the configuration
  8. Copy k3s config file
  9. Change owner to user for k3s config file
  10. Install k9s
  11. Run k9s
  12. k9s usage explanation
  13. Check k3s services
  14. Check the example test application

Install k3s securely without the traefik and servicelb

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

Add cgroup entries into the cmdline.txt

1
sudo vim /boot/cmdline.txt
  • Add at the end of the line that starts with console= the below entries:
1
cgroup_memory=1 cgroup_enable=memory
  • Save the file and exit.

Reboot the server

1
sudo reboot

What is NGINX ingress controller?

See the documentation: NGINX ingress controller

Install NGINX as ingress controller in k3s

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

See the documentation: NGINX ingress controller Installation Guide

Check node status

1
kubectl get nodes

Check pods

1
kubectl get pods -A

Create a load balancer to expose NGINX ingress controller ports

1
vim ingress-controller-load-balancer.yaml
  • Put the below content into the file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
---

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx-controller-loadbalancer
  namespace: ingress-nginx
spec:
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
    - name: https
      port: 443
      protocol: TCP
      targetPort: 443
  type: LoadBalancer
  • Save and exit

Apply the load balancer file

1
kubectl apply -f ingress-controller-load-balancer.yaml

Create a namespace test

1
kubectl  create namespace test

Create an example for testing

The sample below uses the NGINX ingress controller to establish a deployment and expose it. Because SSL will be utilized by default and produce an error for a nonexistent certificate, it’s critical to pay attention to the annotation nginx.ingress.kubernetes.io/ssl-redirect: “false”.

The domain name that is used is another crucial factor. I’m going to use the domain name test.localhost, but you should obviously change it to your own and direct it to your k3s instance node.

Create a file called my-example.yaml and use the following syntax to implement this example for Ingress testing:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-nginx-app
  namespace: test
spec:
  selector:
    matchLabels:
      name: test-nginx-backend
  template:
    metadata:
      labels:
        name: test-nginx-backend
    spec:
      containers:
        - name: backend
          image: docker.io/nginx:alpine
          imagePullPolicy: Always
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: test-nginx-service
  namespace: test
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    name: test-nginx-backend
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-nginx-ingress
  namespace: test
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - host: test.localhost
    http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: test-nginx-service
              port:
                number: 80

Apply the example for testing

1
kubectl apply -f my-example.yaml --namespace test

Test the configuration

1
2
3
4
5
kubectl cluster-info
kubectl get nodes
kubectl get pods -A
kubectl config get-contexts
kubectl get all --all-namespaces

Copy k3s config file

1
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config

Change owner to user for k3s config file

1
sudo chown -R $USER:$USER /home/$USER

Install k9s

In order to communicate with your Kubernetes clusters, K9s offers a terminal UI. This project’s goal is to make it simpler to use, monitor, and administer your apps in the field. K9s continuously scans Kubernetes for modifications and provides follow-up commands to interact with the resources you have selected.

1
curl -sS https://webinstall.dev/k9s | bash

Run k9s

1
k9s

Quit by pressing ctrl+c

k9s usage

1
2
3
k9s info
k9s help
k9s -A

Check k3s services

1
kubectl get svc --all-namespaces -o wide 

Check the example test application

1
curl http://10.43.13.55
Support the author with

sysadmin
WRITTEN BY
sysadmin
QA & Linux Specialist