使用服務來存取叢集中的應用程式

本頁面說明如何建立 Kubernetes Service 物件,讓外部用戶端可以使用它來存取在叢集中執行的應用程式。Service 為具有兩個執行中執行個體的應用程式提供負載平衡。

準備開始

您需要有一個 Kubernetes 叢集,並且必須設定 kubectl 命令列工具以與您的叢集通訊。建議在至少有兩個節點且不充當控制平面主機的叢集上執行本教學課程。如果您還沒有叢集,可以使用 minikube 建立一個,或者您可以使用這些 Kubernetes 實驗環境之一

目標

  • 執行 Hello World 應用程式的兩個執行個體。
  • 建立公開節點埠的 Service 物件。
  • 使用 Service 物件存取正在執行的應用程式。

為在兩個 Pod 中執行的應用程式建立服務

以下是應用程式 Deployment 的組態檔

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  selector:
    matchLabels:
      run: load-balancer-example
  replicas: 2
  template:
    metadata:
      labels:
        run: load-balancer-example
    spec:
      containers:
        - name: hello-world
          image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:2.0
          ports:
            - containerPort: 8080
              protocol: TCP
  1. 在您的叢集中執行 Hello World 應用程式:使用上述檔案建立應用程式 Deployment

    kubectl apply -f https://k8s.io/examples/service/access/hello-application.yaml
    

    上述命令建立一個 Deployment 和一個關聯的 ReplicaSet。ReplicaSet 有兩個 Pod,每個 Pod 都執行 Hello World 應用程式。

  2. 顯示關於 Deployment 的資訊

    kubectl get deployments hello-world
    kubectl describe deployments hello-world
    
  3. 顯示關於您的 ReplicaSet 物件的資訊

    kubectl get replicasets
    kubectl describe replicasets
    
  4. 建立公開 Deployment 的 Service 物件

    kubectl expose deployment hello-world --type=NodePort --name=example-service
    
  5. 顯示關於 Service 的資訊

    kubectl describe services example-service
    

    輸出結果類似於這樣

    Name:                   example-service
    Namespace:              default
    Labels:                 run=load-balancer-example
    Annotations:            <none>
    Selector:               run=load-balancer-example
    Type:                   NodePort
    IP:                     10.32.0.16
    Port:                   <unset> 8080/TCP
    TargetPort:             8080/TCP
    NodePort:               <unset> 31496/TCP
    Endpoints:              10.200.1.4:8080,10.200.2.5:8080
    Session Affinity:       None
    Events:                 <none>
    

    記下 Service 的 NodePort 值。例如,在上述輸出中,NodePort 值為 31496。

  6. 列出正在執行 Hello World 應用程式的 Pod

    kubectl get pods --selector="run=load-balancer-example" --output=wide
    

    輸出結果類似於這樣

    NAME                           READY   STATUS    ...  IP           NODE
    hello-world-2895499144-bsbk5   1/1     Running   ...  10.200.1.4   worker1
    hello-world-2895499144-m1pwt   1/1     Running   ...  10.200.2.5   worker2
    
  7. 取得正在執行 Hello World Pod 的其中一個節點的公用 IP 位址。您如何取得此位址取決於您如何設定叢集。例如,如果您使用 Minikube,您可以執行 kubectl cluster-info 來查看節點位址。如果您使用 Google Compute Engine 執行個體,您可以使用 gcloud compute instances list 命令來查看節點的公用位址。

  8. 在您選擇的節點上,建立一個防火牆規則,允許節點埠上的 TCP 流量。例如,如果您的 Service 的 NodePort 值為 31568,請建立一個防火牆規則,允許埠 31568 上的 TCP 流量。不同的雲端供應商提供不同的防火牆規則設定方式。

  9. 使用節點位址和節點埠來存取 Hello World 應用程式

    curl http://<public-node-ip>:<node-port>
    

    其中 <public-node-ip> 是您節點的公用 IP 位址,而 <node-port> 是您服務的 NodePort 值。對成功請求的回應是 Hello 訊息

    Hello, world!
    Version: 2.0.0
    Hostname: hello-world-cdd4458f4-m47c8
    

使用服務組態檔

作為使用 kubectl expose 的替代方案,您可以使用服務組態檔來建立 Service。

清理

若要刪除 Service,請輸入此命令

kubectl delete services example-service

若要刪除 Deployment、ReplicaSet 和正在執行 Hello World 應用程式的 Pod,請輸入此命令

kubectl delete deployment hello-world

下一步

依照使用服務連線應用程式教學課程進行操作。

上次修改時間:2024 年 5 月 28 日上午 9:50 PST:更新 node-hello 映像檔為 Google 的較新映像檔 (fa033cd15f)