Smith
Smith

Reputation: 33

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

I am using google cloud compute engin for running a small web server as well as some other servers I currently am using the free trial I will be upgrading to a paid account when that expires but I would like to have another server on a different wan IP address I know I can use a different port and do forwarding/redirects but my question is can one instance ger multiple external IPs or would I need to create/pay for another instance to get a 2nd external IP address?

Upvotes: 2

Views: 1652

Answers (1)

Serhii
Serhii

Reputation: 4461

Yes, you can create VM instance with multiple external IPs.

Have a look at the documentation 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.

and at the section 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.

and

  • Every interface can optionally have an external IP address.

I've tried to create such VM:

  1. create 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. create 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 (optional):

    $ gcloud compute addresses create test-static-ip-1 --region=europe-west3
    $ gcloud compute addresses create test-static-ip-2 --region=europe-west3
    
  4. create VM instance:

    $ 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
    

    and here it is VM instance with 2 external IPs:

    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. don't forget to 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
    

    rules above are for example only.

In addition, have look at 3rd party example.

Also, as a possible alternative you can try 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.

Upvotes: 3

Related Questions