11

I'm trying to have my GCE instance listen on multiple IP addresses (for SEO reasons - to host multiple low traffic sites on the same instance).

Final objective: mydomain.com points to IP1, myotherdomain.es points to IP2, the GCE instance will listen on both IP1 and IP2 and serve content accordingly.

I added a target instance pointing to my main instance and managed to create a forwarding rule like this:

gcloud compute forwarding-rules create another-ip --port 80 --target-instance MY_TARGET_INSTANCE_URL

It actually created an ephemeral IP address; I tried to promote it to static but I exceeded my quota (I'm currently on my 2 months free trial).

Is this correct though? Will I be able to create any number of static IPs and point them to my only instance once the trial ends? I also couldn't find anything about pricing: I know an IP assigned to an active instance is free, but what about additional ones?

Since this is a necessary configuration for a site I'm managing, I'd like to be sure it works before committing to moving everything on GCE.

koichirose
  • 1,815
  • 4
  • 22
  • 30

3 Answers3

13

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.

Dagang
  • 24,586
  • 26
  • 88
  • 133
  • yes, this is what I did, the problem is that the forwardingRule creates an ephemeral IP and I can't promote it to static. I don't understand if this is a limitation of the free trial and will be removed once I become a paying customer. – koichirose Oct 11 '16 at 09:24
  • No, you can definitely promote it to static. There's no difference for external IP used by VM instance and forwarding rule. They're the same type of resource. – Dagang Oct 11 '16 at 16:01
  • This method does limit the additional IP to a single protocol TCP or UDP – intika Jan 27 '20 at 17:19
  • thanks, you saved my day. just a correction: you don't need (and often can't use) the `--ports` option on the forwarding-rule creation (edit: i also use the `--address` option, maybe that's the deal breaker that doesn't let ports be specified) – ivcandela Oct 28 '21 at 10:46
1

I am also need 2 static ips for one compute engine instance but google's quota is not allow this.

You can see your quotas from https://console.cloud.google.com/iam-admin/quotas

enter image description here

Savas Adar
  • 4,083
  • 3
  • 46
  • 54
0

An other possibility is to have multiple network interface on the VM

This require adding a new VPC network, the ip 10.130.0.0/20 is not used on the current infrastructure and can be used as an additional network, you would add the proper firewall rules and the proper routing rules (you can copy the default one to avoid any miss-configuration)

Note that you can not add a network interface to an existing machine, you would need to

  • Turn off the current machine
  • Detach disk and network (without deleting them !!!)
  • Create a new machine with 2 network cards or more
  • Attach the old disk and network to the new machine

Finally you would need to pay attention to the default gateway, the classic network behavior would make everything go through the first network interface the second won't be accessible until you change the default gateway and or create the proper routing rules.

Typically you have eth0 and eth1 this example makes eth1 available to services that bind to eth1

ip addr add 10.130.0.2/32 broadcast 10.130.0.2 dev eth1
ip link set eth1 up
ip route add 10.130.0.1 src 10.130.0.2 dev eth1
ip route add 10.130.0.1 src 10.130.0.2 dev eth1 table 100
ip route add default via 10.130.0.1 dev eth1 metric 10
ip route add default via 10.130.0.1 dev eth1 table 100
ip rule add from 10.130.0.2/32 table 100
ip rule add to 10.130.0.2/32 table 100
curl --interface eth1 ifconfig.co
curl --interface eth0 ifconfig.co
ping -I eth1 8.8.8.8

Here is the documentation, alternatively this guide may help.

intika
  • 8,448
  • 5
  • 36
  • 55