使用服務訪問叢集中的應用程式

本頁面展示瞭如何建立一個 Kubernetes Service 物件,外部客戶端可以使用該物件訪問在叢集中執行的應用程式。該 Service 為具有兩個執行例項的應用程式提供負載均衡。

準備工作

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

目標

  • 執行 Hello World 應用程式的兩個例項。
  • 建立一個公開節點埠的 Service 物件。
  • 使用 Service 物件訪問正在執行的應用程式。

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

以下是應用程式 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,則建立一條防火牆規則,允許 TCP 流量透過埠 31568。不同的雲提供商提供不同的配置防火牆規則的方式。

  9. 使用節點地址和節點埠訪問 Hello World 應用程式

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

    其中 <public-node-ip> 是你節點的公共 IP 地址,<node-port> 是你 Service 的 NodePort 值。成功請求的響應是 hello 訊息

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

使用 Service 配置檔案

作為使用 kubectl expose 的替代方案,你可以使用 Service 配置檔案 來建立 Service。

清理

要刪除 Service,輸入以下命令

kubectl delete services example-service

要刪除 Deployment、ReplicaSet 以及執行 Hello World 應用程式的 Pod,請輸入此命令

kubectl delete deployment hello-world

下一步

遵循 使用 Service 連線應用程式 教程。

上次修改時間:2024 年 5 月 28 日,太平洋標準時間上午 9:50:將 node-hello 映象更新為 Google 的新映象 (fa033cd15f)