Permanently binding static IP to preemptible google cloud VM

10/18/2017

For our project we need a static IP binding to our Google Cloud VM instance due to IP whitelisting. Since it's a managed group preemptible, the VM will terminate once in a while.

However, when it terminates I see in the operations log compute.instances.preempted directly followed by compute.instances.repair.recreateInstance with the note:

Instance Group Manager 'xxx' initiated recreateInstance on instance 'xxx'. Reason: instance's intent is RUNNING but instance's status is STOPPING.

After that follows a delete and a insert operation in order to restore the instance.

The documentation states:

You can simulate an instance preemption by stopping the instance.

In which case the IP address will stay attached when the VM is started again.

A) So my question, is it possible to have the instance group manager stop and start the VM in the event of preemption, instead of recreating? Since recreating means that the static IP will be detached and needs to be manually attached each time.

B) If option A is not possible, how can I attach the static IP address automatically so that I don't have to attach it manually when the VM is recreated? I'd rather not have an extra NAT VM instance to take care of this problem.

Thanks in advance!

-- Jurrian
google-cloud-platform
google-compute-engine
google-kubernetes-engine

3 Answers

10/20/2017

One solution is to let the instances have dynamically chosen ephemeral IPs, but set the group as the target of a Load Balancer with a static IP. This way even when instances are created or destroyed, the LB acts as a frontend keeping the IP continious over time.

-- David
Source: StackOverflow

4/23/2018

Answering your questions:

(A) It is not possible at the moment, and I am not sure if it will ever be possible. By design preemptible VMs are deleted to make space for normal VMs (if there are capacity constraints in the given zone) or regularly to differentiate them from normal VMs. In the latter case preemption might seem like a start/stop event, but in the former it may take a substantial amount of time before the VM is recreated.

(B) At the moment there is not good way to achieve it in generality.

  • If you have a special case where your group has only one instance you can hardcode the IP address in the Instance Template
  • Otherwise at the moment the only solution I can think of (other than using a Load Balancer) is to write a startup script that would attach the NAT IP.
-- Grzenio
Source: StackOverflow

10/30/2018

I've found one way that ensures that all VM's in your network have the same outgoing IP address. Using Cloud NAT you can assign a static IP which all VM's will use, there is a downside though:

GCP forwards traffic using Cloud NAT only when there are no other matching routes or paths for the traffic. Cloud NAT is not used in the following cases, even if it is configured:

  • You configure an external IP on a VM's interface.

    If you configure an external IP on a VM's interface, IP packets with the VM's internal IP as the source IP will use the VM's external IP to reach the Internet. NAT will not be performed on such packets. However, alias IP ranges assigned to the interface can still use NAT because they cannot use the external IP to reach the Internet. With this configuration, you can connect directly to a GKE VM via SSH, and yet have the GKE pods/containers use Cloud NAT to reach the Internet.

    Note that making a VM accessible via a load balancer external IP does not prevent a VM from using NAT, as long as the VM network interface itself does not have an external IP address.

Removing the VM's external IP also prevents you from direct SSH access to the VM, even SSH access from the gcloud console itself. The quote above shows an alternative with a load balancer, another way is a bastion, but doesn't directly solve access from for example Kubernetes/kubectl.

If that's no problem for you, this is the way to go.

-- Jurrian
Source: StackOverflow