使用 gcloud CLI 查看 Config Sync 状态

本页面介绍了如何使用 Google Cloud CLI 命令来监控 Config Sync:

  • 使用 gcloud alpha anthos config sync repo 列出多个集群中的代码库状态。

  • 使用 gcloud alpha anthos config sync resources 查看 Config Sync 管理的资源。

您还可以在 Config Sync 信息中心中查看类似信息。

准备工作

在使用本页面上的命令之前,请完成以下步骤:

  1. 更新 Google Cloud CLI 组件:

    gcloud components update
    
  2. 注册集群(如果您尚未注册)。如果您已通过 Google Cloud 控制台安装和配置 Config Sync,则可以跳过此步骤。

  3. 如果您的集群不是 GKE 集群,平台管理员需要为已注册的集群设置连接网关

  4. 设置项目:

    gcloud config set project PROJECT_ID
    

    PROJECT_ID 替换为设置和运行 Config Sync 的项目 ID。

查看多个集群中的 Config Sync 状态

如果您在项目中的多个已注册集群上安装和配置 Config Sync,则可以使用 gcloud alpha anthos config sync repo 列出跨集群同步的代码库的状态并调试 Config Sync 问题。

nomos status 相比,gcloud alpha anthos config sync repo 按代码库对状态进行分组,而 nomos status 命令按集群对状态进行分组。

列出所有代码库

如需列出已同步到当前项目中任何已注册集群的所有 Git 代码库状态,请运行以下命令:

gcloud alpha anthos config sync repo list

输出类似于以下内容:

┌─────────────────────────────────────────────────────────────────┬───────┬────────┬─────────┬───────┬─────────┬─────────────┐
│                       SOURCE                                    │ TOTAL │ SYNCED │ PENDING │ ERROR │ STALLED │ RECONCILING │
├─────────────────────────────────────────────────────────────────┼───────┼────────┼─────────┼───────┼─────────┼─────────────┤
│ https://source.developers.google.com/p/test/r/test//@main       │ 2     │ 2      │ 0       │ 0     │ 0       │ 0           │
│ https://source.developers.google.com/p/test/r/dev//@main        │ 2     │ 1      │ 0       │ 0     │ 1       │ 0           │
│ https://source.developers.google.com/p/test/r/staging//@main    │ 1     │ 0      │ 0       │ 1     │ 0       │ 0           │
└─────────────────────────────────────────────────────────────────┴───────┴────────┴─────────┴───────┴─────────┴─────────────┘

对于每个代码库,您可以查看各个集群的相应状态:

  • TOTAL 是代码库同步到的集群数量。
  • SYNCED 是此代码库已成功同步的集群数量。
  • PENDING 是此代码库正在等待同步的集群数量。
  • ERROR 是此代码库在同步期间遇到某种错误的集群的数量。
  • STALLED 是同步此代码库的 Deployment 已停止的集群数量。
  • RECONCILING 是协调此代码库的 Deployment 所在的集群的数量。

列出具有特定状态的代码库

您还可以使用 --status 标志列出具有特定状态的代码库。支持的值为 syncedpendingerrorstalledreconciling

例如,通过运行以下命令列出存在错误的代码库:

gcloud alpha anthos config sync repo list --status=error

输出类似于以下内容:

┌─────────────────────────────────────────────────────────────────┬───────┬────────┬─────────┬───────┬─────────┬─────────────┐
│                       SOURCE                                    │ TOTAL │ SYNCED │ PENDING │ ERROR │ STALLED │ RECONCILING │
├─────────────────────────────────────────────────────────────────┼───────┼────────┼─────────┼───────┼─────────┼─────────────┤
│ https://source.developers.google.com/p/test/r/staging//@main    │ 1     │ 0      │ 0       │ 1     │ 0       │ 0           │
└─────────────────────────────────────────────────────────────────┴───────┴────────┴─────────┴───────┴─────────┴─────────────┘

描述仓库

使用 describe 命令来查看代码库状态的详细信息:

   gcloud alpha anthos config sync repo describe \
     --source URL

URL 替换为上一部分中所述的 repo list 命令输出中的网址。

您还可以使用 --namespace--name 指定要描述的代码库。

   gcloud alpha anthos config sync repo describe \
     --sync-namespace config-management-system --sync-name root-sync

默认情况下,该命令会输出代码库状态以及已应用但协调失败的任何代管资源。例如:

   [
     {
       "clusters": [
         "cluster-1",
         "cluster-2",
       ],
       "commit": "32293c90a63efba3121c7648048fcd3e85043a34",
       "errors": [],
       "source": "https://source.developers.google.com/p/test/r/test//@main",
       "status": "SYNCED"
     }
   ]

您还可以使用 --managed-resources 标志来控制要查看的代管式资源。支持的值包括 allcurrentinprogressnotfoundfailed(默认值)或 unknown。您可以使用以下命令查看所有代管式资源:

   gcloud alpha anthos config sync repo describe \
     --namespace config-management-system --name root-sync \
     --managed-resources all

输出类似于以下内容:

   [
     {
       "clusters": [
         "cluster-1",
         "cluster-2",
       ],
       "commit": "32293c90a63efba3121c7648048fcd3e85043a34",
       "errors": [],
       "source": "https://source.developers.google.com/p/test/r/test//@main",
       "status": "SYNCED"
     }
   ]
   ┌────────────────────────────────────────────────────────────────┐
   │                       managed_resources                        │
   ├───────┬───────────┬─────────┬───────────┬─────────┬────────────┤
   │ GROUP │    KIND   │   NAME  │ NAMESPACE │  STATUS │ CONDITIONS │
   ├───────┼───────────┼─────────┼───────────┼─────────┼────────────┤
   │       │ Namespace │ default │           │ Current │            │
   │       │ ConfigMap │ foo     │ default   │ Current │            │
   └───────┴───────────┴─────────┴───────────┴─────────┴────────────┘

查看 Config Sync 管理的资源

您可以使用 gcloud alpha anthos config sync resources 按集群、群组、种类、命名空间、名称、状态或这些特性的组合来过滤代管式资源。

列出所有代管式资源

   gcloud alpha anthos config sync resources list

此命令会列出同步到当前项目中任何已注册的集群的所有代管式资源。输出类似于以下内容:

┌───────────────────┬───────────────────────────┬──────────────────────────┬──────────────────┬───────────────────────────────────────────┬─────────┬───────────┐
│    CLUSTER_NAME   │           GROUP           │           KIND           │    NAMESPACE     │                    NAME                   │  STATUS │ CONDITION │
├───────────────────┼───────────────────────────┼──────────────────────────┼──────────────────┼───────────────────────────────────────────┼─────────┼───────────┤
│ cluster-1         │                           │ Namespace                │                  │ default                                   │ Current │           │
│ cluster-1         │ rbac.authorization.k8s.io │ RoleBinding              │ default          │ viewers                                   │ Current │           │
│ cluster-2         │                           │ Namespace                │                  │ gamestore                                 │ Current │           │
│ cluster-2         │ rbac.authorization.k8s.io │ RoleBinding              │ gamestore        │ gamestore-admin                           │ Current │           │
│ cluster-2         │ rbac.authorization.k8s.io │ RoleBinding              │ gamestore        │ gamestore-webstore-admin                  │ Current │           │
└───────────────────┴───────────────────────────┴──────────────────────────┴──────────────────┴───────────────────────────────────────────┴─────────┴───────────┘

输出的每一行描述了代管式资源及其相应的状态:

  • CLUSTER_NAME 是代管式资源所属的集群的名称。
  • GROUP 是代管式资源的组特性。
  • KIND 是代管式资源的种类特性。
  • NAMESPACE 是代管式资源的命名空间。
  • NAME 是托管资源的名称。
  • STATUS 是代管式资源的协调状态。您可以在 Kubernetes Sig 文档中找到可能状态的列表。
  • CONDITION 是说明代管式资源的状态的消息。

列出单个集群中的代管式资源

您可以使用 gcloud 命令或在 Google Cloud 控制台中查看每个集群的代管式资源。

您可以使用 --cluster 标志列出特定集群中的代管式资源。例如,使用以下命令列出名为 cluster-1 的集群中的代管式资源:

gcloud alpha anthos config sync resources list --cluster=cluster-1

输出内容类似如下:

┌───────────────────┬───────────────────────────┬──────────────────────────┬──────────────────┬───────────────────────────────────────────┬─────────┬───────────┐
│    CLUSTER_NAME   │           GROUP           │           KIND           │    NAMESPACE     │                    NAME                   │  STATUS │ CONDITION │
├───────────────────┼───────────────────────────┼──────────────────────────┼──────────────────┼───────────────────────────────────────────┼─────────┼───────────┤
│ cluster-1         │                           │ Namespace                │                  │ default                                   │ Current │           │
│ cluster-1         │ rbac.authorization.k8s.io │ RoleBinding              │ default          │ viewers                                   │ Current │           │
└───────────────────┴───────────────────────────┴──────────────────────────┴──────────────────┴───────────────────────────────────────────┴─────────┴───────────┘

后续步骤