Multiple IP addresses on a single Google Compute Engine instance
You can get multiple external IPs for one VM instance with forwarding rules.
- 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.
- 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:
Create a TargetInstance for your VM instance:
gcloud compute target-instances create <target-instance-name> --instance <instance-name> --zone=<zone>
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:
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=customcreate 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/24reserve 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-west3create 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 RUNNINGdon'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-2iprules 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
Prompting for User Input in Assembly Ci20 Seg Fault
Diff to Output Only the File Names
How to Have Tcpdump Write to File and Standard Output the Appropriate Data
Linux: Set Permission Only to Directories
Linux: How to Know the Module That Exports a Device Node
How to Set Up Autocompletion for Git Commands
How to Write a Linux Daemon with .Net Core
In a Linux Shell How to Process Each Line of a Multiline String
How to Conveniently Sync a File Between Two Git Repositories
Write-Through Ram Disk, or Massive Caching of File System
How to Install Cargo on a Rhel Linux Server
Can Linux Cat Command Be Used for Writing Text to File
Find Files in Created Between a Date Range
How to Calculate System Memory Usage from /Proc/Meminfo (Like Htop)
Can't Install Python-Dev on Centos 6.5
Tcp: When Is Epollhup Generated
How to Automatically Pipe to Less If the Result Is More Than a Page on My Shell