【问题标题】:How to programmatically get the network tags for a GKE cluster如何以编程方式获取 GKE 集群的网络标签
【发布时间】:2019-09-02 13:08:30
【问题描述】:

如果我创建一个名为 cluster-1 的新 GKE 集群,集群中的虚拟机都会有一个自动生成的网络标签,例如gke-cluster-1-d4732bcc-node.

是否可以使用gcloud CLI 或其他方式以编程方式使用集群名称检索此网络标签?

【问题讨论】:

    标签: google-cloud-platform google-compute-engine google-kubernetes-engine


    【解决方案1】:

    您只能通过 gcloud 使用命令获取 VM 网络标签

    gcloud compute instances describe INSTANCE-NAME --project=PROJECT-ID --zone=INSTANCE-ZONE
    

    网络标签信息将在底部,输出类似于:

    tags:
      fingerprint: xxxx
      items:
      - tag1
      - tag2
      - tag3
    

    集群创建的所有虚拟机都将具有相同的前缀。 gke-CLUSTER_NAME-NODE_POOL_NAME-RANDOM_STRING。

    例如,我创建了集群“test-cluster”,而我只使用了“default-pool”。我的实例之一是 [gke-test-cluster-default-pool-xxxxxxx-xxxxxxx]

    您可以获取集群创建的所有实例名称,并将它们放入类似于

    的变量中
    name=`gcloud compute instances list --project=PROJECT-ID | grep gke | awk '{print $1}'`
    

    现在你可以使用 FOR 循环来运行命令了

    for tags in $name; do gcloud compute instances describe $tags --project=PROJECT-ID --zone=ZONE; done
    

    您可以在命令末尾添加 GREP 来获取网络标签信息,将输出存储在文件中或根据需要进行解析。

    【讨论】:

      【解决方案2】:

      我通过为 GKE 集群获取自动生成的防火墙规则之一并提取目标标记来实现这一点:

      CLUSTER_NAME=<cluster-name>
      PROJECT_NAME=<project-name>
      
      NODE_NETWORK_TAG=$(gcloud compute firewall-rules list --project $PROJECT_NAME --filter="name~gke-$CLUSTER_NAME-[0-9a-z]*-master" --format="value(targetTags[0])")
      
      echo "$NODE_NETWORK_TAG"
      

      【讨论】:

        猜你喜欢
        • 2019-10-14
        • 2021-02-25
        • 1970-01-01
        • 1970-01-01
        • 2021-06-27
        • 2018-05-04
        • 2016-10-16
        • 2019-01-20
        • 2011-07-20
        相关资源
        最近更新 更多