在 GCP 中创建没有 public IP 的虚拟机

Creating VMs without public IP in GCP

GCP at Subnet 中限制 external(Public) IP 的任何方式。似乎如果我们想创建一个没有 external IPVM,我们必须在 VM 创建期间 select External IP- None。我们不能在子网配置中设置 External IP- None 并且在此子网中创建的任何主机都不会具有 public/external IP

您可以设置组织策略约束,以便 define allowed external IPs for VM instances. With this constraint your could restrict configuration of external IPs to a list of instances. Leaving the allowedValues list empty will make no longer possible the configuration of external IP addresses to VMs within the organization projects. Find all the relevant information on the following section of the documentation

如果您发现策略约束方法过于严格(请注意 many other products that rely on VMs will be affected) another strategy that you could set in place would be to automate the deployments of VMs with the gcloud compute instances create command and take advantage of the --no-address 标记以避免将外部 IP 地址分配给实例。

对于那些正在使用 Google 的 GCP REST API 寻找 per-instance 解决方案的人,请确保您的 networkInterfaces 中没有 accessConfigs ] 您请求的部分 JSON body:

        "machineType": "zones/{my_zone}/machineTypes/{my_machine_type}",
        "name": "my_instance_name",
        "networkInterfaces": [{
            "network": "my_vpc_network_ref",
            "subnetwork": "my_subnet_ref"
            # Don't include these commented lines if you only want a private IP 
            # "accessConfigs": [{
            #     "name": "External NAT",
            #     "type": "ONE_TO_ONE_NAT"
            # }]
        }],

所有以“my”开头的部分都是placeholders/variables。

我意识到实际问题是关于如何在子网级别执行此操作,提供的答案有助于在组织或项目级别执行此操作,但问题的标题没有具体说明我们禁止在哪个级别私有IP,并且从 GCP 文档中不清楚 --no-address 标志的等效 REST API 调用是什么,因此我想在此处记录它以供将来的编码人员使用。