列出子网中的实例

时间:2016-10-10 22:02:14

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

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

~  gcloud compute instances list --filter='network:prod-net'
Listed 0 items.

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

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

在这种情况下,您正在寻找的是:

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

(我将:切换为=,因为前者表示"包含"后者表示完全匹配。有关详情,请参阅gcloud topic filters

答案 1 :(得分:2)

您确实可以使用gcloud按子网过滤GCE实例。

您需要按networkInterfaces.subnetwork进行过滤,并且要与之比较的文字值是完整的子网资源网址,而不仅仅是子网名称。

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

gcloud compute networks subnets list <YOUR_SUBNET_NAME> --format=flattened

示例:

$ 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

现在我有subnet_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