Service Directory for GKE 概览

Google Kubernetes Engine (GKE) 的 Service Directory 提供您所有 GKE 部署及其他部署中的所有服务的单一视图。 您可以添加各种类型的 GKE 服务,对其进行注释,然后在 Service Directory 中查看这些服务。

Service Directory for GKE 提供以下功能:

  • 它允许您通过 gRPC 和 HTTP 发现服务,并使用 Service Directory DNS 区域通过 DNS 解析服务。Service Directory 和 Cloud DNS 集成可让您配置 Service Directory DNS 区域
  • Service Directory 会注册 GKE 外部的服务,从而让 GKE 服务和非 GKE 服务能够轻松发现彼此。
  • Service Directory 会强制执行 IAM 权限和 VPC Service Controls 检查,以确保客户数据的适当安全性和隐私性。
  • 您可以为 Autopilot GKE 集群或 Standard GKE 集群配置 Service Directory for GKE。如需了解这两种模式,请参阅 选择 GKE 运维模式

如需了解如何配置 Service Directory 以自动向 Service Directory 注册 您的 GKE 服务,请参阅 为 GKE 配置 Service Directory

服务映射

向 Service Directory 注册服务时,系统会在 Google Cloud 舰队项目中创建一个服务,其名称为 Kubernetes 命名空间和 GKE 集群的区域。下表介绍了对于所有 GKE 服务类型,GKE 服务中的属性如何映射到 Service Directory 中创建的服务中的属性。

顶层属性

Service Directory 属性 GKE 属性
项目 舰队项目
区域 集群的区域
命名空间 Kubernetes 命名空间

LoadBalancer 服务类型

以下示例展示了 GKE LoadBalancer 服务如何映射到 Service Directory。Service Directory 服务存在于一个命名空间中,该命名空间的名称与舰队项目和 GKE 集群区域中的 Kubernetes 命名空间相同。

Service Directory 属性 GKE 属性
服务名称 服务名称
端点名称 由 Service Directory 控制器自动生成
端点 IP 地址 LoadBalancer 入口
端点端口 LoadBalancer 服务端口
端点网络 集群的 VPC 网络

以下是 GKE LoadBalancer 服务的一个示例:

apiVersion: v1
kind: Service
metadata:
  name: hello-app
  annotations:
    cloud.google.com/load-balancer-type: "Internal"
  labels:
    app: hello-app
    sd-import: "true"
spec:
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: hello-app
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
    - ip: 10.140.0.5

以下是 Service Directory 服务的一个示例:

service:
  endpoints:
  - address: 10.140.0.5
    metadata:
       cloud.google.com/load-balancer-type: "Internal"
    name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app/endpoints/gke-hello-cluster-12345
    port: 80
    network: projects/my-project/locations/global/networks/default
  name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app

NodePort 服务类型

以下示例展示了 GKE NodePort 服务如何映射到 Service Directory。Service Directory 服务存在于一个命名空间中,该命名空间的名称与中心项目和 GKE 集群区域中的 Kubernetes 命名空间相同。

Service Directory 属性 GKE 属性
服务名称 服务名称
端点名称 由 Service Directory 控制器自动生成
端点 IP 地址 为服务运行 pod 的每个节点的节点内部 IP 地址
端点端口 静态节点端口
端点网络 集群的 VPC 网络

以下是 GKE NodePort 服务的一个示例:

apiVersion: v1
kind: Service
metadata:
  name: hello-app
  annotations:
    key1: "value1"
spec:
  ports:
  - nodePort: 30007
    port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: hello-app
  type: NodePort
  

以下是 GKE 节点的一个示例:

apiVersion: v1
items:
- apiVersion: v1
  kind: Node
  metadata:
    name: gke-hello-cluster-default-pool-a40cbab6-d2rr
  status:
    addresses:
    - address: 10.142.0.27
      type: InternalIP
    - address: 34.73.248.170
- apiVersion: v1
  kind: Node
  metadata:
    name: gke-hello-cluster-default-pool-a40cbab6-kb38
  status:
    addresses:
    - address: 10.142.0.29
      type: InternalIP
    - address: 35.196.219.215
      type: ExternalIP
- apiVersion: v1
  kind: Node
  metadata:
    name: gke-hello-cluster-default-pool-a40cbab6-x34q
  status:
    addresses:
    - address: 10.142.0.28
      type: InternalIP
    - address: 34.75.202.26
      type: ExternalIP
    

以下是 Service Directory 服务的一个示例:

service:
  endpoints:
  - address: 10.142.0.27
    metadata:
      key1: value1
    name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app/endpoints/gke-hello-cluster-12345
    port: 30007
    network: projects/my-project/locations/global/networks/default
  - address: 10.142.0.28
    metadata:
      key1: value1
    name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app/endpoints/gke-hello-cluster-23456
    port: 30007
    network: projects/my-project/locations/global/networks/default
  - address: 10.142.0.29
    metadata:
      key1: value1
    name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app/endpoints/gke-hello-cluster-34567
    port: 30007
    network: projects/my-project/locations/global/networks/default
  name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app
  

ClusterIP 服务

以下示例展示了 GKE ClusterIP 服务如何映射到 Service Directory。Service Directory 服务存在于一个命名空间中,该命名空间的名称与舰队项目和 GKE 集群区域中的 Kubernetes 命名空间相同。

Service Directory 属性 GKE 属性
服务名称 服务名称
端点名称 由 Service Directory 控制器自动生成
端点 IP 地址 ClusterIP
端点端口 服务端口
端点网络 集群的 VPC 网络

以下是 GKE ClusterIP 服务的一个示例:

apiVersion: v1
kind: Service
metadata:
  name: hello-app
  annotations:
    key1: "value1"
  labels:
    app: hello-app
    sd-import: "true"
spec:
  clusterIP: 10.15.254.17
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: hello-app
  type: ClusterIP
  

以下是 Service Directory 服务的一个示例:

service:
  endpoints:
  - address: 10.15.254.17
    metadata:
      key1: value1
    name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app/endpoints/gke-hello-cluster-12345
    port: 80
    network: projects/my-project/locations/global/networks/default
  name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app
  

后续步骤

  • 如需大致了解 Service Directory,请参阅 Service Directory 概览
  • 如需了解您在使用 Service Directory 时可能会遇到的常见问题的解决方案,请参阅 问题排查