No menu items!
No menu items!
More

    [Kubernetes 13] Sử dụng Ingress trong Kubernetes

    Ingress là thành phần được dùng để điều hướng các yêu cầu traffic giao thức HTTP và HTTPS từ bên ngoài (interneet) vào các dịch vụ bên trong Cluster.

    Ingress chỉ để phục vụ các cổng, yêu cầu HTTP, HTTPS còn các loại cổng khác, giao thức khác để truy cập được từ bên ngoài thì dùng Service với kiểu NodePort và LoadBalancer. Để Ingress hoặt động, hệ thồng cần một điều khiển ingress trước. Ở bài này mình dùng NGINX Kubernetes Ingress Controller.

    NGINX Kubernetes Ingress Controller là một ingress hỗ trợ khả năng căn bằng tải, SSL, URI rewrite …

    1. Cài đặt NGINX Ingress Controller

    Mình sử dụng NGINX Ingress Controller từ kho chứa helm, vậy đầu tiên mình sẽ cài helm trước

    root@k8s-standalone:/home# curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 && chmod 700 get_helm.sh && ./get_helm.sh
    Downloading https://get.helm.sh/helm-v3.10.0-linux-amd64.tar.gz
    Verifying checksum... Done.
    Preparing to install helm into /usr/local/bin
    helm installed into /usr/local/bin/helm

    Helm verify

    root@k8s-standalone:/home# helm version
    version.BuildInfo{Version:"v3.10.0", GitCommit:"ce66412a723e4d89555dc67217607c6579ffcb21", GitTreeState:"clean", GoVersion:"go1.18.6"}

    Thêm helm repo nginx-stable vào

    root@k8s-standalone:/home# helm repo add nginx-stable https://helm.nginx.com/stable
    "nginx-stable" has been added to your repositories

    Tạo namespace nginx-ingress

    root@k8s-standalone:/home# kubectl create namespace nginx-ingress

    Triển khai Ingress Nginx

    root@k8s-standalone:/home# helm install ingress nginx-stable/nginx-ingress --namespace nginx-ingress
    NAME: ingress
    LAST DEPLOYED: Tue Oct  4 06:39:52 2022
    NAMESPACE: nginx-ingress
    STATUS: deployed
    REVISION: 1
    TEST SUITE: None
    NOTES:
    The NGINX Ingress Controller has been installed.

    Vì có một số trường mình cần thay đổi nên mình sẽ clone source code của Ingress Nginx về và chỉnh sửa một số trường theo ý của mình

    root@k8s-standalone:/home# git clone https://github.com/nginxinc/kubernetes-ingress.git
    Cloning into 'kubernetes-ingress'...
    remote: Enumerating objects: 46021, done.
    remote: Counting objects: 100% (637/637), done.
    remote: Compressing objects: 100% (429/429), done.
    remote: Total 46021 (delta 361), reused 429 (delta 208), pack-reused 45384
    Receiving objects: 100% (46021/46021), 60.86 MiB | 3.98 MiB/s, done.
    Resolving deltas: 100% (27238/27238), done.

    Sau khi clone source code về ta có thư mục chứa các file như dưới

    root@k8s-standalone:/home# ll ./kubernetes-ingress/deployments/helm-chart/crds/
    total 204
    drwxr-xr-x 2 root root  4096 Oct  4 06:43 ./
    drwxr-xr-x 4 root root  4096 Oct  4 06:43 ../
    -rw-r--r-- 1 root root  2760 Oct  4 06:43 appprotectdos.f5.com_apdoslogconfs.yaml
    -rw-r--r-- 1 root root  2506 Oct  4 06:43 appprotectdos.f5.com_apdospolicy.yaml
    -rw-r--r-- 1 root root  4080 Oct  4 06:43 appprotectdos.f5.com_dosprotectedresources.yaml
    -rw-r--r-- 1 root root  3088 Oct  4 06:43 appprotect.f5.com_aplogconfs.yaml
    -rw-r--r-- 1 root root 81315 Oct  4 06:43 appprotect.f5.com_appolicies.yaml
    -rw-r--r-- 1 root root  3456 Oct  4 06:43 appprotect.f5.com_apusersigs.yaml
    -rw-r--r-- 1 root root  3730 Oct  4 06:43 externaldns.nginx.org_dnsendpoints.yaml
    -rw-r--r-- 1 root root  2037 Oct  4 06:43 k8s.nginx.org_globalconfigurations.yaml
    -rw-r--r-- 1 root root 11286 Oct  4 06:43 k8s.nginx.org_policies.yaml
    -rw-r--r-- 1 root root  5846 Oct  4 06:43 k8s.nginx.org_transportservers.yaml
    -rw-r--r-- 1 root root 30511 Oct  4 06:43 k8s.nginx.org_virtualserverroutes.yaml
    -rw-r--r-- 1 root root 33716 Oct  4 06:43 k8s.nginx.org_virtualservers.yaml

    Triển khai nó

    root@k8s-standalone:/home# kubectl apply -f ./kubernetes-ingress/deployments/helm-chart/crds/

    Hiện tại Ingress Nginx đang triển khai ở dạng deployment bạn có thể giữ nguyên hoặc chuyển qua dạng khác như daemonset (tuỳ nhu cầu).

    root@k8s-standalone:/home# kubectl get deploy -n nginx-ingress
    NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
    ingress-nginx-ingress   1/1     1            1           11m

    Ví dụ chuyển sang daemonset, ta vào file values.yaml chỉnh sửa kind từ deployment sang daemonset như dưới.

    sed -i 's|kind: deployment|kind: daemonset|g' ./kubernetes-ingress/deployments/helm-chart/values.yaml

    Sau khi sửa xong values.yaml, ta dùng helm để cập nhật lại giá trị, nhớ trỏ đến file values.yaml vừa mới sửa xong.

    root@k8s-standalone:/home# helm upgrade ingress nginx-stable/nginx-ingress -f ./kubernetes-ingress/deployments/helm-chart/values.yaml -n nginx-ingress
    Release "ingress" has been upgraded. Happy Helming!
    NAME: ingress
    LAST DEPLOYED: Tue Oct  4 06:51:31 2022
    NAMESPACE: nginx-ingress
    STATUS: deployed
    REVISION: 2
    TEST SUITE: None
    NOTES:
    The NGINX Ingress Controller has been installed.

    Verify lại bên daemonset ta có kết quả như dưới

    root@k8s-standalone:/home# kubectl get ds -n nginx-ingress
    NAME                    DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
    ingress-nginx-ingress   1         1         1       1            1           <none>          11s

    Chúng ta tạo file yaml config-map với nội dung như dưới, mục đích là trỏ địa chỉ ip 192.168.13.238 chính là địa chỉ máy master node (vì do mình chỉ có 1 node). Sau này nếu có nguyên cụm và có cả node loadbalancer chúng ta có thể trỏ ip của loadbalancer vào đây.

    Sửa đổi giá trị use-proxy-protocol từ false sang true để cho phép sử dụng nginx như 1 proxy.

    echo 'kind: ConfigMap
    apiVersion: v1
    metadata:
      name: ingress-nginx-ingress
      namespace: nginx-ingress
    data:
      use-proxy-protocol: "true"
      proxy-connect-timeout: "10s"
      proxy-read-timeout: "10s"
      client-max-body-size: "2m"
      external-status-address: "192.168.13.238"' > ./nginx-patch-configmap.yaml

    Sau khi sửa xong, ta tiến hành patch nó

    root@k8s-standalone:/home# kubectl -n nginx-ingress patch configmap ingress-nginx-ingress --patch-file ./nginx-patch-configmap.yaml
    configmap/ingress-nginx-ingress patched

    Tạo file yaml với nội dung như dưới

    cat > ./prometheus-ingress.yaml << OEF
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-prometheus
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /
    spec:
      ingressClassName: nginx
      rules:
        - host: prometheus.hoanghd.com
          http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: svc-prometheus
                    port:
                      number: 80
    OEF

    Triển khai file manifest

    root@k8s-standalone:/home# kubectl apply -f prometheus-ingress.yaml 
    ingress.networking.k8s.io/ingress-prometheus created

    Verify ingress đã được tạo

    root@k8s-standalone:/home# kubectl get ingress -A
    NAMESPACE   NAME                 CLASS   HOSTS                    ADDRESS          PORTS   AGE
    default     ingress-prometheus   nginx   prometheus.hoanghd.com   192.168.13.238   80      7s

    Nếu không có domain thì bạn hãy trỏ file host như mình

    hoanghd@mb-hoanghd ~ % cat /etc/hosts | grep prometheus.hoanghd.com
    192.168.13.238 prometheus.hoanghd.com

    Kiểm tra kết nối đến master node

    hoanghd@mb-hoanghd ~ % ping prometheus.hoanghd.com
    PING prometheus.hoanghd.com (192.168.13.238): 56 data bytes
    64 bytes from 192.168.13.238: icmp_seq=0 ttl=64 time=0.442 ms
    64 bytes from 192.168.13.238: icmp_seq=1 ttl=64 time=0.495 ms
    64 bytes from 192.168.13.238: icmp_seq=2 ttl=64 time=0.530 ms
    64 bytes from 192.168.13.238: icmp_seq=3 ttl=64 time=0.550 ms
    
    --- prometheus.hoanghd.com ping statistics ---
    4 packets transmitted, 4 packets received, 0.0% packet loss
    round-trip min/avg/max/stddev = 0.442/0.504/0.550/0.041 ms

    Và đây là kết quả

    Bài viết gần đây

    spot_img

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Đăng ký nhận thông tin bài viết qua email