使用 NGINX Ingress Controller 在 Minikube 上設定 Ingress

Ingress 是一個 API 物件,它定義了允許外部訪問叢集中服務的規則。 Ingress 控制器實現 Ingress 中設定的規則。

此頁面展示瞭如何設定一個簡單的 Ingress,該 Ingress 根據 HTTP URI 將請求路由到 Service 'web' 或 'web2'。

準備工作

本教程假設你正在使用 minikube 執行本地 Kubernetes 叢集。 請訪問安裝工具,瞭解如何安裝 minikube

你需要擁有一個 Kubernetes 叢集,並且 kubectl 命令列工具已配置為與你的叢集通訊。 建議在至少有兩個不充當控制平面主機的節點上執行本教程。 如果你還沒有叢集,可以使用 minikube 建立一個,或者你可以使用以下 Kubernetes 演練場之一。

你的 Kubernetes 伺服器版本必須是 1.19 或更高。

要檢查版本,請輸入 kubectl version

如果使用的是較舊的 Kubernetes 版本,請切換到該版本的文件。

建立 minikube 叢集

如果你尚未在本地設定叢集,請執行 minikube start 來建立叢集。

啟用 Ingress 控制器

  1. 要啟用 NGINX Ingress 控制器,請執行以下命令

    minikube addons enable ingress
    
  2. 驗證 NGINX Ingress 控制器是否正在執行

    kubectl get pods -n ingress-nginx
    

    輸出類似於:

    NAME                                        READY   STATUS      RESTARTS    AGE
    ingress-nginx-admission-create-g9g49        0/1     Completed   0          11m
    ingress-nginx-admission-patch-rqp78         0/1     Completed   1          11m
    ingress-nginx-controller-59b45fb494-26npt   1/1     Running     0          11m
    

部署一個 Hello, World 應用

  1. 使用以下命令建立一個 Deployment

    kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0
    

    輸出應該是

    deployment.apps/web created
    

    驗證 Deployment 處於 Ready 狀態

    kubectl get deployment web 
    

    輸出應該類似於

    NAME   READY   UP-TO-DATE   AVAILABLE   AGE
    web    1/1     1            1           53s
    
  2. 暴露 Deployment

    kubectl expose deployment web --type=NodePort --port=8080
    

    輸出應該是

    service/web exposed
    
  3. 驗證 Service 已建立並可在節點埠上使用

    kubectl get service web
    

    輸出類似於:

    NAME      TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
    web       NodePort   10.104.133.249   <none>        8080:31637/TCP   12m
    
  4. 使用 minikube service 命令透過 NodePort 訪問 Service。請按照你平臺的說明操作。

    minikube service web --url
    

    輸出類似於:

    http://172.17.0.15:31637
    

    呼叫上一步輸出中獲得的 URL

    curl http://172.17.0.15:31637 
    

    # The command must be run in a separate terminal.
    minikube service web --url 
    

    輸出類似於:

    http://127.0.0.1:62445
    ! Because you are using a Docker driver on darwin, the terminal needs to be open to run it.
    

    從另一個終端,呼叫上一步輸出中獲得的 URL

    curl http://127.0.0.1:62445 
    

    輸出類似於:

    Hello, world!
    Version: 1.0.0
    Hostname: web-55b8c6998d-8k564
    

    現在你可以透過 Minikube IP 地址和 NodePort 訪問示例應用程式。下一步將讓你使用 Ingress 資源訪問應用程式。

建立 Ingress

以下清單定義了一個 Ingress,它透過 hello-world.example 將流量傳送到你的 Service。

  1. 從以下檔案建立 example-ingress.yaml

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: example-ingress
    spec:
      ingressClassName: nginx
      rules:
        - host: hello-world.example
          http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: web
                    port:
                      number: 8080
  2. 透過執行以下命令建立 Ingress 物件

    kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yaml
    

    輸出應該是

    ingress.networking.k8s.io/example-ingress created
    
  3. 驗證 IP 地址已設定

    kubectl get ingress
    

    你應該在 ADDRESS 列中看到一個 IPv4 地址;例如

    NAME              CLASS   HOSTS                 ADDRESS        PORTS   AGE
    example-ingress   nginx   hello-world.example   172.17.0.15    80      38s
    
  4. 按照你平臺的說明,驗證 Ingress 控制器正在引導流量

    curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example
    

    minikube tunnel
    

    輸出類似於:

    Tunnel successfully started
    
    NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ...
    
    The service/ingress example-ingress requires privileged ports to be exposed: [80 443]
    sudo permission will be asked for it.
    Starting tunnel for service example-ingress.
    

    在新終端中,呼叫以下命令

    curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example
    

    你應該看到

    Hello, world!
    Version: 1.0.0
    Hostname: web-55b8c6998d-8k564
    
  5. (可選)你也可以從瀏覽器訪問 hello-world.example

    在你的計算機的 /etc/hosts 檔案末尾新增一行(你需要管理員許可權)

    查詢 minikube 報告的外部 IP 地址

      minikube ip 
    

      172.17.0.15 hello-world.example
    

    127.0.0.1 hello-world.example
    

    進行此更改後,你的 Web 瀏覽器會將對 hello-world.example URL 的請求傳送到 Minikube。

建立第二個 Deployment

  1. 使用以下命令建立另一個 Deployment

    kubectl create deployment web2 --image=gcr.io/google-samples/hello-app:2.0
    

    輸出應該是

    deployment.apps/web2 created
    

    驗證 Deployment 處於 Ready 狀態

    kubectl get deployment web2 
    

    輸出應該類似於

    NAME   READY   UP-TO-DATE   AVAILABLE   AGE
    web2   1/1     1            1           16s
    
  2. 暴露第二個 Deployment

    kubectl expose deployment web2 --port=8080 --type=NodePort
    

    輸出應該是

    service/web2 exposed
    

編輯現有 Ingress

  1. 編輯現有的 example-ingress.yaml 清單,並在末尾新增以下行

    - path: /v2
      pathType: Prefix
      backend:
        service:
          name: web2
          port:
            number: 8080
    
  2. 應用更改

    kubectl apply -f example-ingress.yaml
    

    你應該看到

    ingress.networking/example-ingress configured
    

測試你的 Ingress

  1. 訪問 Hello World 應用程式的第一個版本。

    curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example
    

    minikube tunnel
    

    輸出類似於:

    Tunnel successfully started
    
    NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ...
    
    The service/ingress example-ingress requires privileged ports to be exposed: [80 443]
    sudo permission will be asked for it.
    Starting tunnel for service example-ingress.
    

    在新終端中,呼叫以下命令

    curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example
    

    輸出類似於:

    Hello, world!
    Version: 1.0.0
    Hostname: web-55b8c6998d-8k564
    
  2. 訪問 Hello World 應用程式的第二個版本。

    curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example/v2
    

    minikube tunnel
    

    輸出類似於:

    Tunnel successfully started
    
    NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ...
    
    The service/ingress example-ingress requires privileged ports to be exposed: [80 443]
    sudo permission will be asked for it.
    Starting tunnel for service example-ingress.
    

    在新終端中,呼叫以下命令

    curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example/v2
    

    輸出類似於:

    Hello, world!
    Version: 2.0.0
    Hostname: web2-75cd47646f-t8cjk
    

下一步

上次修改時間為 2024 年 5 月 7 日太平洋標準時間下午 1:50:更改 ingress 教程的主機,以減輕安全風險。(cd6148bc97)