透過 Ingress 使用容器原生負載平衡功能

本頁說明如何在 Google Kubernetes Engine (GKE) 中使用容器原生負載平衡。容器原生負載平衡功能可讓負載平衡器直接指定 Kubernetes Pod,並將流量平均傳送給 Pod。

如要進一步瞭解容器原生負載平衡的優點、需求和限制,請參閱容器原生負載平衡

事前準備

開始之前,請確認你已完成下列工作:

  • 啟用 Google Kubernetes Engine API。
  • 啟用 Google Kubernetes Engine API
  • 如要使用 Google Cloud CLI 執行這項工作,請安裝初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行 gcloud components update 指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文件中的指令。
  • 確認您有現有的 VPC 原生叢集。如需叢集,請建立叢集。GKE 叢集預設為 VPC 原生叢集。

使用容器原生負載平衡

下列各節會逐步引導您在 GKE 上設定容器原生負載平衡。

可建立部署作業

下列的部署範例 neg-demo-app 會執行容器化 HTTP 伺服器的單一執行個體。建議使用會使用 Pod 完備性反饋的工作負載。

使用 Pod 完備性反饋

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: neg-demo-app # Label for the Deployment
  name: neg-demo-app # Name of Deployment
spec:
  selector:
    matchLabels:
      run: neg-demo-app
  template: # Pod template
    metadata:
      labels:
        run: neg-demo-app # Labels Pods from this Deployment
    spec: # Pod specification; each Pod created by this Deployment has this specification
      containers:
      - image: registry.k8s.io/serve_hostname:v1.4 # Application to run in Deployment's Pods
        name: hostname # Container name
        ports:
        - containerPort: 9376
          protocol: TCP
  

使用硬式編碼延遲

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: neg-demo-app # Label for the Deployment
  name: neg-demo-app # Name of Deployment
spec:
  minReadySeconds: 60 # Number of seconds to wait after a Pod is created and its status is Ready
  selector:
    matchLabels:
      run: neg-demo-app
  template: # Pod template
    metadata:
      labels:
        run: neg-demo-app # Labels Pods from this Deployment
    spec: # Pod specification; each Pod created by this Deployment has this specification
      containers:
      - image: registry.k8s.io/serve_hostname:v1.4 # Application to run in Deployment's Pods
        name: hostname # Container name
      # Note: The following line is necessary only on clusters running GKE v1.11 and lower.
      # For details, see https://cloud.google.com/kubernetes-engine/docs/how-to/container-native-load-balancing#align_rollouts
        ports:
        - containerPort: 9376
          protocol: TCP
      terminationGracePeriodSeconds: 60 # Number of seconds to wait for connections to terminate before shutting down Pods
  

在此部署中,各容器都會執行一個 HTTP 伺服器。HTTP 伺服器會傳回應用程式伺服器的主機名稱 (亦即伺服器執行所在的 Pod 名稱) 做為回應。

將此資訊清單儲存為 neg-demo-app.yaml,然後建立 Deployment:

kubectl apply -f neg-demo-app.yaml

為容器原生負載平衡器建立 Service

建立 Deployment 後,必須將部署的多個 Pod 組成一個 Service

下列的 Service 範例 neg-demo-svc 是以前一節建立的 Deployment 範例為目標:

apiVersion: v1
kind: Service
metadata:
  name: neg-demo-svc # Name of Service
spec: # Service's specification
  type: ClusterIP
  selector:
    run: neg-demo-app # Selects Pods labelled run: neg-demo-app
  ports:
  - name: http
    port: 80 # Service's port
    protocol: TCP
    targetPort: 9376

要等到您為 Service 建立 Ingress 後,才會建立負載平衡器。

將此資訊清單儲存為 neg-demo-svc.yaml,然後建立 Service:

kubectl apply -f neg-demo-svc.yaml

為 Service 建立 Ingress

下列的 Ingress 範例 neg-demo-ing 會以您建立的 Service 為目標:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: neg-demo-ing
spec:
  defaultBackend:
    service:
      name: neg-demo-svc # Name of the Service targeted by the Ingress
      port:
        number: 80 # Should match the port used by the Service

將此資訊清單儲存為 neg-demo-ing.yaml,然後建立 Ingress:

kubectl apply -f neg-demo-ing.yaml

建立 Ingress 後,應用程式負載平衡器隨即會在專案裡建立,網路端點群組(NEG) 會在叢集執行所在的每個區域裡建立。NEG 裡的端點和 Service 端點會保持同步。

驗證 Ingress

在部署工作負載、將工作負載的多個 Pod 組成一個 Service,並為 Service 建立 Ingress 之後,應該要確認 Ingress 已成功佈建容器原生負載平衡器。

擷取 Ingress 的狀態:

kubectl describe ingress neg-demo-ing

輸出內容包含 ADDCREATE 事件:

Events:
Type     Reason   Age                From                     Message
----     ------   ----               ----                     -------
Normal   ADD      16m                loadbalancer-controller  default/neg-demo-ing
Normal   Service  4s                 loadbalancer-controller  default backend set to neg-demo-svc:32524
Normal   CREATE   2s                 loadbalancer-controller  ip: 192.0.2.0

測試負載平衡器

下列各節說明如何測試容器原生負載平衡器的功能。

造訪 Ingress IP 位址

請等候數分鐘,以等待應用程式負載平衡器完成設定。

您可以造訪 Ingress 的 IP 位址,確認容器原生負載平衡器功能運作正常。

如要取得 Ingress IP 位址,請執行下列指令:

kubectl get ingress neg-demo-ing

在指令列輸出中,Ingress 的 IP 位址會顯示於 ADDRESS 欄。使用網路瀏覽器造訪 IP 位址。

檢查後端服務健康狀態

您也可以取得負載平衡器的後端服務健康狀態。

  1. 取得專案中執行的後端服務清單:

    gcloud compute backend-services list
    

    記錄含有 Service 名稱的後端服務名稱,例如 neg-demo-svc

  2. 取得後端服務的健康狀態:

    gcloud compute backend-services get-health BACKEND_SERVICE_NAME --global
    

    BACKEND_SERVICE_NAME 改為後端服務名稱。

測試 Ingress

要如預期般測試負載平衡器功能,還有另外一種方法:調度 Deployment 範例的資源,將測試要求傳送到 Ingress,然後驗證回應的備用資源數量正確無誤。

  1. 將一個執行個體的 neg-demo-app Deployment 資源擴充到兩個執行個體:

    kubectl scale deployment neg-demo-app --replicas 2
    

    這個指令可能需要幾分鐘才能完成。

  2. 確認發布作業已完成:

    kubectl get deployment neg-demo-app
    

    輸出內容應包含兩個可用的備用資源:

    NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    neg-demo-app   2         2         2            2           26m
    
  3. 取得 Ingress IP 位址:

    kubectl describe ingress neg-demo-ing
    

    如果此指令傳回 404 錯誤,請稍待幾分鐘,等負載平衡器啟動後再試一次。

  4. 計算負載平衡器傳出的不重複回應之數量:

    for i in `seq 1 100`; do \
      curl --connect-timeout 1 -s IP_ADDRESS && echo; \
    done  | sort | uniq -c
    

    IP_ADDRESS 替換為 Ingress IP 位址。

    輸出結果會與下列內容相似:

    44 neg-demo-app-7f7dfd7bc6-dcn95
    56 neg-demo-app-7f7dfd7bc6-jrmzf
    

    在這個輸出內容中,不重複回應的數量與副本數量相同,表示所有後端 Pod 都在處理流量。

清除所用資源

完成本頁面的工作之後,為避免您的帳戶中產生不必要的費用,請按照下列步驟移除資源:

刪除叢集

gcloud

gcloud container clusters delete neg-demo-cluster

控制台

  1. 前往 Google Cloud 控制台的「Google Kubernetes Engine」頁面。

    前往「Google Kubernetes Engine」

  2. 選取「neg-demo-cluster」,然後按一下 「Delete」

  3. 當系統提示時,按一下「Delete」(刪除)

後續步驟