使用 Service 访问集群中的应用程序

本页面展示了如何创建一个 Kubernetes Service 对象,供外部客户端访问集群中运行的应用。该 Service 为拥有两个运行实例的应用提供负载均衡。

开始之前

你需要有一个 Kubernetes 集群,并且必须配置 kubectl 命令行工具以与你的集群通信。建议在至少有两个节点的集群上运行本教程,且这些节点不能作为控制平面主机。如果你还没有集群,可以通过 minikube 创建一个,或者使用以下 Kubernetes 演练场之一。

目标

  • 运行两个 Hello World 应用实例。
  • 创建一个公开节点端口(NodePort)的 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,则创建一条允许在 31568 端口上进行 TCP 通信的防火墙规则。不同的云服务提供商配置防火墙规则的方法各不相同。

  9. 使用节点地址和节点端口访问 Hello World 应用

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

    其中 <public-node-ip> 是节点的公共 IP 地址,<node-port> 是服务的 NodePort 值。成功请求的响应将是一个问候消息。

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

使用 Service 配置文件

作为使用 kubectl expose 的替代方案,您可以使用 Service 配置文件来创建 Service。

清理

要删除该 Service,请输入此命令

kubectl delete services example-service

要删除运行 Hello World 应用的 Deployment、ReplicaSet 和 Pod,请输入此命令

kubectl delete deployment hello-world

接下来

请参考 使用 Service 连接应用 教程。


最后修改时间:2024 年 5 月 28 日上午 9:50 PST:将 node-hello 镜像更新为 Google 的较新镜像 (fa033cd15f)