Multiple Ip Addresses on Google Cloud Compute

Multiple IP addresses on a single Google Compute Engine instance

You can get multiple external IPs for one VM instance with forwarding rules.

  1. By default, VM will be assigned with an ephemeral external IP, you can promote it to static external IP, which will remain unchanged after stop and restart.
  2. Extra external IPs have to be attached to forwarding rules which point to the VM. You can use (or promote to) static IPs as well.

The command you may want to use:

  1. Create a TargetInstance for your VM instance:

    gcloud compute target-instances create <target-instance-name> --instance <instance-name> --zone=<zone>
  2. Create a ForwardingRule pointing to the TargetInstance:

    gcloud compute forwarding-rules create <forwarding-rule-name> --target-instance=<target-instance-name> --ip-protocol=TCP --ports=<ports>

See Protocol Forwarding.

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

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.

Multiple IP addresses on Google Cloud Compute

You can add internal IP for VM using routes. (https://cloud.google.com/compute/docs/reference/beta/routes)

Here an example of setting another static internal IP to VM:
https://cloud.google.com/compute/docs/networking#staticnetworkaddress

Create GCP Multiple VM's - Output IP address

Here is the output you can use if you have multiple VMs, where instance count and count.index is used

output "ip" {
value = "${google_compute_instance.default[*].network_interface.0.access_config.0.nat_ip}"
}

main.tf file

resource "google_compute_instance" "default" {
name = "virtual-machine-from-terraform-${count.index}"
machine_type = "e2-micro"
zone = "us-central1-a"
count = 2
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}

network_interface {
network = "default"

access_config {
// Include this section to give the VM an external ip address
}
}

metadata_startup_script = "sudo apt-get update && sudo apt-get install apache2 -y && echo '<!doctype html><html><body><h1>Hello World!${count.index}</h1></body></html>' | sudo tee /var/www/html/index.html"

// Apply the firewall rule to allow external IPs to access this instance
tags = [element(var.instance_tag,count.index)]
}

variable "instance_tag" {
type = list
default = ["http-one", "http-two"]
}

resource "google_compute_firewall" "http-server" {
name = "default-allow-http-terraform"
network = "default"

allow {
protocol = "tcp"
ports = ["80"]
}

// Allow traffic from everywhere to instances with an http-server tag
source_ranges = ["0.0.0.0/0"]
target_tags = ["http-server"]
}
output "ip" {
value = "${google_compute_instance.default[*].network_interface.0.access_config.0.nat_ip}"
}



Related Topics



Leave a reply



Submit