Lesson 4 - Install and use ingress in the Microk8s integrator

07/01/2023 - 2 phút

Follow  on Google News

HostnamevCPURAMDISK
127.0.0.1Host8 core32GSSD 500G
192.168.56.2microk8s-master-11 core2G50G
192.168.56.3microk8s-master-21 core2G50G
192.168.56.4microk8s-master-31 core2G50G
192.168.56.5microk8s-worker-11 core2G50G
192.168.56.6microk8s-worker-21 core2G50G
192.168.56.7microk8s-worker-31 core2G50G
192.168.56.8microk8s-worker-41 core2G50G

Install Nginx on Ubuntu 22.04

Bước 1 – Cài đặt Nginx

Step 1 - Install Nginx

Update the apt installation packages

sudo apt update

Install Nginx

sudo apt install nginx -y

Step 2 – Authorize HTTP Firewall

sudo ufw allow 'Nginx HTTP'

Step 3 – Test your Web Server

Kiểm tra service nginx có hoạt đông không?

To facilitate code administration on the master machine and testing, I will install the code-server on the microk8s-master-01 machine

To install, run the command:

curl -fsSL https://code-server.dev/install.sh | sh

sudo systemctl enable --now code-server@$USER

After installation is complete, go to the config folder

nano ~/.config/code-server/config.yaml

bind-addr: 0.0.0.0:9999
auth: password
password: 123456
cert: false

sudo service code-server@$USER restart

Kích hoạt INGRESS

To enable Inpress Controller of Microk8s we use the command:

microk8s enable ingress

After successful activation, we will try to create a dashboard ingress file: ingress-dashboard.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: https-ingress-dashboard
  namespace: kube-system
  annotations:
    kubernetes.io/ingress.class: public
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kubernetes-dashboard
                port:
                  number: 443

Run command microk8s kubectl apply -f ingress-dashboard.yaml

When successful, go to https://10.19.2.92/ to check

Configure nginx Reverse Proxy

Step 1: go to the hosts file to add a line at the end of the machine you are using, here I use Ubuntu (Host Machine)

Windows Path: C:\Windows\System32\drivers\etc\hosts

MacOS & Linux Path: /etc/hosts

127.0.0.1 microk8s-dashboard.localhost

Step 2: Configure nginx

đi đến thư mục: /etc/nginx/sites-enabled

cd /etc/nginx/sites-enabled

create file microk8s-dashboard.localhost

sudo nano microk8s-dashboard.localhost

Copy the config and add it to the file microk8s-dashboard.localhost.conf


upstream host_mircok8s_worker {
    server 192.168.56.5;
    server 192.168.56.6;
    server 192.168.56.7;
    server 192.168.56.8;
}


server {

    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name microk8s-dashboard.localhost;

    ssl_certificate /etc/nginx/ssl/localhost.crt;
    ssl_certificate_key /etc/nginx/ssl/localhost.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://host_mircok8s_worker;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Save and check the config to see if it is correct: sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the service:

sudo systemctl restart nginx

Cấu hình ingress-dashboard.yaml

Add the line host: kubernetes-dashboard.localhost

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: https-ingress-dashboard
  namespace: kube-system
  annotations:
    kubernetes.io/ingress.class: public
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
  rules:
    - host: kubernetes-dashboard.localhost
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kubernetes-dashboard
                port:
                  number: 443

Run the config again: microk8s kubectl apply -f ingress-dashboard.yaml

If you find this sharing article interesting, please give me a like and subscribe to support me. Thank you so much ♥️♥️♥️♥️

Reference articles: