Ở bài này thì mình cần thêm 1 VM cài Nginx để loadbalancer ở đây mình sẽ tạo thêm 1 VM ubuntu 22.04
IP | Hostname | vCPU | RAM | DISK |
---|---|---|---|---|
10.19.2.91 | nginx | 2 core | 8G | 100G |
10.19.2.92 | stg-02 | 6 core | 12G | 100G |
10.19.2.93 | stg-03 | 6 core | 12G | 100G |
10.19.2.94 | stg-03 | 6 core | 12G | 100G |
Ta cài đặt theo mô hình
Cài đặt Nginx trên Ubuntu 22.04
Kết nối vào VM nginx (10.19.2.91)
1 2 3 |
ssh ubuntu@10.19.2.91 Nhập Pass |
Bước 1 – Cài đặt Nginx
Cập nhật các gói cài đặt apt
1 2 |
sudo apt update |
Cài đặt Nginx
1 2 |
sudo apt install nginx -y |
Bước 2 – Cấp quyền HTTP Firewall
1 2 |
sudo ufw allow 'Nginx HTTP' |
Bước 3 – Kiểm tra Máy chủ Web của bạn
Kiểm tra service nginx có hoạt đông không?
Kích hoạt INGRESS
Để kích hoạt Inpress Controller của Microk8s chúng ta sử dụng lệnh:
1 2 |
microk8s enable ingress |
Sau khi kích hoạt thành công chúng ta sẽ thử tạo tiệp ingress của dashboard
Tiệp: ingress-dashboard.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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 |
Chạy lệnh microk8s kubectl apply -f ingress-dashboard.yaml
Khi thành công ta vào đường dẫn https://10.19.2.92/ để kiểm tra
Cấu hình nginx Reverse Proxy
Bước 1: bạn vào file hosts để thêm 1 dòng ở cuối ở máy bạn đang sử dụng, ở đây mình sử dụng Windows
Windows Path: C:WindowsSystem32driversetchosts
MacOS & Linux Path: /etc/hosts
1 2 |
10.19.2.91 kubernetes-dashboard.localhost |
Bước 2: Cấu hình nginx
Kết nối vào VM nginx (10.19.2.91)
1 2 3 |
ssh ubuntu@10.19.2.91 Nhập Pass |
sau đó đi đến thư mục: /etc/nginx/sites-enabled
1 2 |
cd /etc/nginx/sites-enabled |
tạo file kubernetes-dashboard.localhost.conf
1 2 |
sudo nano kubernetes-dashboard.localhost.conf |
copy đoạn config thêm vào file kubernetes-dashboard.localhost.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
upstream host_stg { server 10.19.2.92; server 10.19.2.93; server 10.19.2.94; } server { listen 80 ; server_name kubernetes-dashboard.localhost; location / { limit_req zone=speedbump burst=20 nodelay; proxy_pass http://host_stg; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } |
Lưu lại và kiểm tra config xem đã chính xác chưa : sudo nginx -t
1 2 3 |
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful |
Khởi động lại service:
1 2 |
sudo systemctl restart nginx |
Cấu hình ingress-dashboard.yaml
Thêm dòng host: kubernetes-dashboard.localhost vào
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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 |
Chạy lại config : microk8s kubectl apply -f ingress-dashboard.yaml
Nếu bạn thấy bài chia sẽ này hay xin hãy cho mình một like và đăng ký để ủng hộ mình nhé. Cảm ơn các bạn nhiều ♥️♥️♥️♥️
Các bài tham khảo: