本页面介绍了如何使用 Google Kubernetes Engine (GKE) 配置 Service Directory,以及如何自动向 Service Directory 注册 GKE 集群。您可以为 Autopilot GKE 集群或 Standard GKE 集群配置 Service Directory for GKE 。 如需了解这两种模式,请参阅 选择 GKE 运维模式。
如需详细了解 Service Directory for GKE,请参阅 Service Directory for GKE 概览。
限制
Service Directory for GKE 具有以下限制:
- 您只能使用 Service Directory-GKE 集成注册服务。您必须使用 Service Directory API 手动注册 Ingress、网关和其他资源。
- 您必须先使用 舰队注册 GKE 集群,然后才能将这些集群中的服务同步到 Service Directory。如需详细了解如何注册 GKE 集群,请参阅注册集群。
- 所有 Service Directory 资源都在舰队项目中注册,而不是在集群的项目中注册。
- 您无法修改使用此集成注册的 Service Directory 实体。您必须对 GKE 服务资源本身进行所有更改,以便这些更改传播到 Service Directory。
准备工作
- 登录您的 Google Cloud 账号。 如果您是新手,请创建一个账号来评估我们的产品在实际场景中的表现。 Google Cloud新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
- 您必须将 GKE 集群注册到舰队。 如需注册 GKE 集群,请按照 注册 集群中的说明操作。
设置 GKE 集群
如需设置 GKE 集群,请按以下步骤操作。
- 启用 Service Directory GKE 舰队功能。
- 配置 Service Directory 注册政策。
- 部署 GKE 服务。
启用 Service Directory GKE 舰队功能
如需启用 Service Directory GKE 舰队
功能,请运行
gcloud container hub 命令。
gcloud
gcloud alpha container hub service-directory enable
为服务配置 Service Directory 注册政策
注册政策定义了 Service Directory 会自动导入 GKE 中的哪些类型的服务。以下列表显示了此政策的可配置选项:
resources:要导入的 GKE 资源的类型。您可以指定多种资源类型。资源由Group和Kind标识。默认情况下,Group为core。只有Kind: service可用。selector:一个 GKE 标签选择器,用于匹配应用于 GKE 资源的 key:value 标签。默认情况下,系统不会选择任何资源。annotationsToSync:要同步到 Service Directory 的资源上的注解键。如果此列表为空,则资源会同步到 Service Directory,但没有注释。
每个命名空间都应定义一项政策。此政策必须具有 name:
"default"。控制器不允许或协调其他名称。
配置 ServiceDirectoryRegistrationPolicy。
以下清单描述了一个 ServiceDirectoryRegistrationPolicy:
apiVersion: networking.gke.io/v1alpha1 kind: ServiceDirectoryRegistrationPolicy metadata: # Only the name "default" is allowed. name: default # The ServiceDirectoryRegistrationPolicy is a namespaced resource namespace: NAMESPACE spec: resources: # Kind specifies the types of Kubernetes resources that can be synced into Service Directory. - kind: Service # Selector is a label selector for the resource types specified in Kind. selector: matchLabels: LABELS # annotationsToSync specifies the annotations that are matched and imported. # Any annotations that do not match this set of keys will not be imported into Service Directory. annotationsToSync: - ANNOTATIONS
替换以下内容:
NAMESPACE:命名空间的名称。一个命名空间中只能有 一个ServiceDirectoryRegistrationPolicy。如果单个命名空间中有 多个政策,GKE 会选择 一项政策作为有效政策,并将其他政策标记为无效。LABELS:标签选择器,例如sd-import: "true"。 此值适用于种类中指定的资源。默认情况下,系统不会选择任何 资源。ANNOTATIONS:要匹配和 导入的注解列表,例如- cloud.google.com/load-balancer-type。如果所选 GKE 资源具有列表中的某个注解,则该注解及其值会与资源一起同步到 Service Directory 。如果此列表为空,则资源会同步到 Service Directory,但没有注释。
将此清单保存为 registration_policy.yaml。
将清单应用到您的集群
如需将清单应用到您的集群,请运行 kubectl apply -f 命令:
kubectl apply -f registration_policy.yaml
部署 GKE 服务
在您所在区域的 GKE 集群中部署 GKE 区域级外部直通网络负载平衡器服务和
应用,例如,如果
您的区域是 us-central1,则将其部署在 GKE 集群
gke-us-central1 中。
这会将您的 GKE 服务导入到 Service Directory。
创建一个 YAML 文件,并像应用任何其他 GKE 资源一样应用它。以下是 LoadBalancer 类型的服务的清单:
apiVersion: v1
kind: Service
metadata:
name: hello-app-service
labels:
sd-import: "true"
annotations:
cloud.google.com/load-balancer-type: "Internal"
spec:
type: LoadBalancer
selector:
app: hello
ports:
- name: tcp-port
protocol: TCP
port: 80
targetPort: 8080
以下是 Deployment 的清单:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: hello
name: hello-app
spec:
replicas: 2
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- image: gcr.io/google-samples/hello-app:1.0
name: hello
ports:
- containerPort: 8080
protocol: TCP
如需进行验证,请使用
gcloud service-directory services resolve
命令对您的服务运行 Service Directory 查询:
gcloud
gcloud beta service-directory services resolve SERVICE_NAME \ --location=LOCATION \ --namespace=NAMESPACE
替换以下内容:
SERVICE_NAME:服务的名称或完全限定标识符,例如hello-app-serviceLOCATION:服务的区域名称,例如us-central1NAMESPACE:服务的命名空间名称,例如default
您的输出会显示您的服务,类似于以下内容:
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-service/endpoints/my-cluster-12345
port: 80
network: projects/123456789/locations/global/networks/default
name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app-service
结算
Service Directory 不会对通过 GKE 集成创建的资源收费。不过,API 调用会收费。如需了解价格详情,请参阅 Service Directory 价格。
后续步骤
- 如需大致了解 Service Directory,请参阅 Service Directory 概览。
- 如需了解您在使用 Service Directory 时可能会遇到的常见问题的解决方案,请参阅 问题排查。