Google compute engine 列出子网中的实例

Google compute engine 列出子网中的实例,google-compute-engine,google-cloud-platform,Google Compute Engine,Google Cloud Platform,您好,我正在尝试列出特定网络和子网络中的计算实例,但似乎无法获得正确的筛选。例如,我有一个名为“prod net”的网络和一个名为“app central”的子网络。当我运行搜索时,我只得到“列出的0个项目” 有什么建议吗?过滤器--filter标志不操作表数据,而是操作底层的富资源对象。要查看此对象,请运行gcloud compute instances list--format=json 在这种情况下,您需要的是: $ gcloud compute instances list --filt

您好,我正在尝试列出特定网络和子网络中的计算实例,但似乎无法获得正确的筛选。例如,我有一个名为“prod net”的网络和一个名为“app central”的子网络。当我运行搜索时,我只得到“列出的0个项目”

有什么建议吗?

过滤器--filter标志不操作表数据,而是操作底层的富资源对象。要查看此对象,请运行
gcloud compute instances list--format=json

在这种情况下,您需要的是:

$ gcloud compute instances list --filter='networkInterfaces.network=prod-net'

(我将
切换为
=
,因为前者表示“包含”,而后者表示完全匹配。有关更多信息,请参阅
gcloud主题筛选器

您确实可以使用
gcloud
按子网络筛选GCE实例

您需要按
网络接口进行筛选。子网
和要比较的文本值是完整的子网资源url,而不仅仅是子网名称

您的子网的“资源url”可通过以下方式获得:

gcloud计算网络子网列表--格式=扁平化

例如:

$ gcloud compute networks subnets list sg-zk-1 --project my-gcp-project --format=flattened
---
creationTimestamp:     2017-04-20T02:22:17.853-07:00
gatewayAddress:        10.9.19.33
id:                    6783412628763296550
ipCidrRange:           10.9.19.32/28
kind:                  compute#subnetwork
name:                  sg-zk-1
network:               valkyrie
privateIpGoogleAccess: True
region:                asia-southeast1
selfLink:              https://www.googleapis.com/compute/v1/projects/my-gcp-project/regions/asia-southeast1/subnetworks/sg-zk-1
在上面的示例中,子网名称是
sg-zk-1

子网对应的资源URL是
selfLink
的值,即
https://www.googleapis.com/compute/v1/projects/my-gcp-project/regions/asia-southeast1/subnetworks/sg-zk-1

现在我有了
子网\u url
,我可以过滤属于它的实例:

$ subnet_url="https://www.googleapis.com/compute/v1/projects/my-gcp-project/regions/asia-southeast1/subnetworks/sg-zk-1"

$ gcloud compute instances list --filter="networkInterfaces.subnetwork=${subnet_url}"
NAME            ZONE               MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
sg-zookeeper-4  asia-southeast1-b  n1-standard-2               10.9.19.37                RUNNING
sg-zookeeper-5  asia-southeast1-b  n1-standard-2               10.9.19.38                RUNNING
sg-zookeeper-1  asia-southeast1-a  n1-standard-2               10.9.19.34                RUNNING
sg-zookeeper-2  asia-southeast1-a  n1-standard-2               10.9.19.35                RUNNING
sg-zookeeper-3  asia-southeast1-a  n1-standard-2               10.9.19.36                RUNNING

似乎无法按网络进行筛选,因为不应用筛选器时,没有网络列。例如,您可以使用--filter=ZONE~us来过滤us区域中的实例。正如@Dagang所提到的,-filter标志会考虑列出的列。但是,我建议在这个公共问题跟踪程序[1]中提交一个功能请求,以便在将来的版本中提供此功能。[1] :我需要执行
gcloud compute instances list--filter='networkInterfaces[0].network=prod-net'
以使其工作。有趣的是,我刚刚验证了我的原始答案仍然有效。您是否使用最新版本的gcloud(138.0.0)?啊,您是对的。在我的Ubuntu虚拟机上,
apt
告诉我“谷歌云sdk已经是最新版本(111.0.0-0ubuntu1~16.04.0)”。在GCE web控制台上使用Google Cloud Shell时,我使用的是138版,您的命令运行良好。谢谢
$ subnet_url="https://www.googleapis.com/compute/v1/projects/my-gcp-project/regions/asia-southeast1/subnetworks/sg-zk-1"

$ gcloud compute instances list --filter="networkInterfaces.subnetwork=${subnet_url}"
NAME            ZONE               MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
sg-zookeeper-4  asia-southeast1-b  n1-standard-2               10.9.19.37                RUNNING
sg-zookeeper-5  asia-southeast1-b  n1-standard-2               10.9.19.38                RUNNING
sg-zookeeper-1  asia-southeast1-a  n1-standard-2               10.9.19.34                RUNNING
sg-zookeeper-2  asia-southeast1-a  n1-standard-2               10.9.19.35                RUNNING
sg-zookeeper-3  asia-southeast1-a  n1-standard-2               10.9.19.36                RUNNING