为第三方集成生成资产清单

本页面介绍了如何为现有舰队生成集群清单。借助舰队的集群清单,您可以使用采用 ClusterProfile 规范的开源集成和第三方集成。

集群清单是舰队的开源等效项:即 Kubernetes 集群及其他可共同管理的资源的逻辑分组。ClusterProfile 规范包含的信息是舰队中的部分信息。

ClusterProfile 同步功能是舰队与集群清单之间的单向同步。启用 ClusterProfile 同步后,舰队是可信来源,对舰队的任何更改都会通过 ClusterProfile 同步自动应用于集群清单。直接对集群配置文件资源所做的更改不会应用于舰队。

准备工作

  1. 确保您已按照一般前提条件使用舰队。

  2. 创建舰队或确定要使用的现有舰队。

启用 ClusterProfile 同步

ClusterProfile 同步针对舰队中的一个集群运行。此集群称为 hub 集群,可用于运行需要舰队中其余集群相关信息的多集群控制器。多集群控制器的示例包括 Argo CD 控制器多集群编排程序。我们建议您不要在 hub 集群中运行其他工作负载。

通过添加标签 fleet-clusterinventory-management-cluster=true,在集群上启用 ClusterProfile 同步。您可以执行以下操作:

为舰队启用 ClusterProfile 同步功能后,系统会自动使用 ClusterProfile API 为舰队中的每个集群创建集群配置文件。对舰队进行更改时,系统会自动创建、更新和删除这些集群配置文件。

在新集群上启用 ClusterProfile 同步

如需为 hub 集群创建集群并启用 ClusterProfile 同步,请使用以下命令:

gcloud container clusters create CLUSTER --location LOCATION --enable-fleet
    --labels=fleet-clusterinventory-management-cluster=true

替换以下内容:

  • CLUSTER:您的 google_container_cluster 资源的名称。
  • LOCATION:在其中创建集群的位置。

默认情况下,集群配置文件是在 fleet-cluster-inventory 命名空间中创建的。如需更改命名空间,请指定 fleet-clusterinventory-namespace 标签。

gcloud container clusters update CLUSTER --location LOCATION
    --update-labels=fleet-clusterinventory-namespace=NAMESPACE

替换以下内容:

  • CLUSTER:您的 google_container_cluster 资源的名称。
  • LOCATION:在其中创建集群的位置。
  • NAMESPACE:在其中生成集群配置文件的命名空间。 如果未指定,则会在 fleet-cluster-inventory 命名空间中生成集群配置文件。

在现有集群上启用 ClusterProfile 同步

如需在现有集群上启用 ClusterProfile 同步,请使用以下命令:

gcloud container clusters update CLUSTER --location LOCATION
    --update-labels=fleet-clusterinventory-management-cluster=true

替换以下内容:

  • CLUSTER:您的 google_container_cluster 资源的名称。
  • LOCATION:在其中创建集群的位置。

默认情况下,集群配置文件是在 fleet-cluster-inventory 命名空间中创建的。如需更改命名空间,请指定 fleet-clusterinventory-namespace 标签。

gcloud container clusters update CLUSTER --location LOCATION
    --update-labels=fleet-clusterinventory-namespace=NAMESPACE

替换以下内容:

  • CLUSTER:您的 google_container_cluster 资源的名称。
  • LOCATION:在其中创建集群的位置。
  • NAMESPACE:在其中生成集群配置文件的命名空间。 如果未指定,则会在 fleet-cluster-inventory 命名空间中生成集群配置文件。

验证是否已启用 ClusterProfile 同步

通过验证 hub 集群是否包含 fleet-clusterinventory-management-cluster = true 标签,来验证是否已启用 ClusterProfile 同步。为此,请使用以下命令:

gcloud container clusters describe CLUSTER --location LOCATION

替换以下内容:

  • CLUSTER:您的 google_container_cluster 资源的名称。
  • LOCATION:在其中创建集群的位置。

输出应类似如下所示:

resourceLabels:
  fleet-clusterinventory-management-cluster: 'true'

验证生成的集群配置文件资源

使用以下命令验证是否已在 hub 集群中生成集群配置文件资源:

gcloud container clusters get-credentials CLUSTER --location LOCATION

kubectl get clusterprofiles -n fleet-cluster-inventory

替换以下内容:

  • CLUSTER:您的 google_container_cluster 资源的名称。
  • LOCATION:在其中创建集群的位置。

输出应类似于以下内容:

NAME                           AGE
cluster-1-us-west1             10s
cluster-2-us-west1             20s

此输出应包含舰队中每个集群的集群配置文件。集群配置文件名称采用 MEMBERSHIP-LOCATION 格式。

查看集群配置文件的内容

如需查看特定集群的集群配置文件的内容,请使用以下命令:

kubectl get clusterprofile CLUSTER-LOCATION -n fleet-cluster-inventory -o yaml

替换以下内容:

  • CLUSTER:您的 google_container_cluster 资源的名称。
  • LOCATION:在其中创建集群的位置。

输出应类似于以下内容:

apiVersion: multicluster.x-k8s.io/v1alpha1
kind: ClusterProfile
metadata:
  annotations:
    fleet.gke.io/membershipName: projects/123456789/locations/us-west1/memberships/cluster-1
  labels:
    x-k8s.io/cluster-manager: gke-fleet
  name: cluster-1-us-west1
  namespace: fleet-cluster-inventory

停用 ClusterProfile 同步

通过移除 fleet-clusterinventory-management-cluster 标签,在舰队的 hub 集群上停用 ClusterProfile 同步:

gcloud container clusters update CLUSTER --location LOCATION \
    --remove-labels=fleet-clusterinventory-management-cluster

替换以下内容:

  • CLUSTER:您的 google_container_cluster 资源的名称。
  • LOCATION:在其中创建集群的位置。

后续步骤