google 云计算引擎的一个实例可以有多个外部 IP 地址吗

can one instance of google clouds compute engine have multiple external ip addresses

我正在为 运行 小型 Web 服务器以及其他一些服务器使用 google 云计算引擎 我目前正在使用免费试用版 我将在到期后升级到付费帐户但我想在不同的 wan IP 地址上有另一台服务器我知道我可以使用不同的端口并执行 forwarding/redirects 但我的问题是一个实例可以 ger 多个外部 IP 还是我需要 create/pay另一个实例获取第二个外部 IP 地址?

是的,您可以创建具有多个外部 IP 的 VM 实例。

查看文档 Creating instances with multiple network interfaces:

By default, every instance in a VPC network has a single default network interface. Use these instructions to create additional network interfaces. Each interface is attached to a different VPC network, giving that instance access to different VPC networks in Google Cloud Platform (GCP). You cannot attach multiple network interfaces to the same VPC network.

Requirements 部分:

  • You can only configure a network interface when you create an instance.
  • Each network interface configured in a single instance must be attached to a different VPC network, and each interface must belong to a subnet whose IP range does not overlap with the subnets of any other interfaces.
  • The additional VPC networks that the multiple interfaces will attach to must exist before you create the instance. See Using VPC Networks for instructions on creating additional VPC networks.
  • You cannot delete a network interface without deleting the instance.

  • Every interface can optionally have an external IP address.

我试过创建这样的虚拟机:

  1. 创建custom VPC networks:

    $ gcloud compute networks create test-vpc-network-1 --subnet-mode=custom
    $ gcloud compute networks create test-vpc-network-2 --subnet-mode=custom
    
  2. 创建custom VPC subnets:

    $ gcloud compute networks subnets create test-subnet-1 --network=test-vpc-network-1 --region=europe-west3 --range=172.16.1.0/24
    $ gcloud compute networks subnets create test-subnet-2 --network=test-vpc-network-2 --region=europe-west3 --range=172.16.2.0/24
    
  3. reserve static external IPs(可选):

    $ gcloud compute addresses create test-static-ip-1 --region=europe-west3
    $ gcloud compute addresses create test-static-ip-2 --region=europe-west3
    
  4. 创建虚拟机实例:

    $ gcloud compute instances create test-instance-2ip --zone=europe-west3-a --machine-type=n1-standard-1 --network-interface subnet=test-subnet-1,address=34.89.215.180 --network-interface subnet=test-subnet-2,address=35.234.123.210 --tags=test-instance-2ip --image=ubuntu-1804-bionic-v20200430 --image-project=ubuntu-os-cloud --boot-disk-device-name=test-instance-2ip
    

    这里是具有 2 个外部 IP 的 VM 实例:

    NAME               ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP            EXTERNAL_IP                   STATUS
    test-instance-2ip  europe-west3-a  n1-standard-1               172.16.1.3,172.16.2.2  XXX.89.XXX.180,XXX.234.XXX.210  RUNNING
    
  5. 别忘了create firewall rules:

    $ gcloud compute firewall-rules create test-instance-2ip-vpc-1 --direction=INGRESS --priority=900 --network=test-vpc-network-1 --action=ALLOW --rules=tcp,udp --source-ranges=0.0.0.0/0 --target-tags=test-instance-2ip
    $ gcloud compute firewall-rules create test-instance-2ip-vpc-2 --direction=INGRESS --priority=900 --network=test-vpc-network-2 --action=ALLOW --rules=tcp,udp --source-ranges=0.0.0.0/0 --target-tags=test-instance-2ip
    

    以上规则仅供参考。

此外,看看第3方example

此外,作为一种可能的替代方法,您可以尝试 Protocol forwarding:

You can set up multiple forwarding rules to point to a single target instance, allowing you to use multiple external IP addresses with one VM instance. You can use this in scenarios where you may want to serve data from just one VM instance, but through different external IP addresses. This is especially useful for setting up SSL virtual hosting.