通过 Service 访问集群中的应用

本页介绍如何创建 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,每个都运行 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> 是 Service 的 NodePort 值。成功请求的响应是一个 hello 消息

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

使用服务配置文件

作为使用 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 AM: 更新 node-hello 镜像至 Google 的新镜像 (fa033cd15f)