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

本页展示了如何创建一个 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 具有两个 Pods,每个 Pod 运行 Hello World 应用程序。

  2. 显示 Deployment 的信息

    kubectl get deployments hello-world
    kubectl describe deployments hello-world
    
  3. 显示您的 ReplicaSet 对象的信息

    kubectl get replicasets
    kubectl describe replicasets
    
  4. 创建一个 Service 对象,暴露 Deployment

    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, 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 PST:更新 node-hello 镜像到 Google 的更新镜像 (fa033cd15f)