hasnayn
hasnayn

Reputation: 356

How can I swap internal IP address between two GCP VMs?

I have a running VM1 (internal IP is: xxx.xxx.0.7). I will create another new VM2 and I want to change the IP of VM1 to any other IP and set the IP of VM2 to "xxx.xxx.0.7". OS of both VM is windows server.

I have found some way to change the IP of a running VM. But I am not sure if that works. Is there any straightforward way to do this?

Thanks in advance!

Upvotes: 5

Views: 3039

Answers (3)

John Hanley
John Hanley

Reputation: 81464

You can assign a static internal IP address when the instance is created and not once the VM is running.

To accomplish your goal of creating a new instance using an existing internal IP address:

  1. Backup the existing virtual machine (create an image, snaphot, etc.)
  2. Promote the internal ephemeral IP address.
  3. Delete the virtual machine.
  4. Create a new virtual machine specifying the static internal IP address.
  5. Recreate the original virtual machine from the image or snapshot.

References:

Upvotes: 2

Wojtek_B
Wojtek_B

Reputation: 4443

First - for this operation you will need to stop your VM1. But I believe this is not a concern since you are planning to make VM2 work as VM1.

Second - It's impossible to attach new or existing static internal IP to an existing VM.

If you want to reserve internal IP address for VM2 then the only way I know is the first method I described.

First method - switch disks. Simple but may require some configuration after the switch.

  • create VM2 (may be identical but you may add processors or RAM if you need)
  • configure VM2 with all you need to do the job of VM1
  • clone VM2's boot disk (lets call it disk22)
  • stop VM1
  • detach a boot disk from VM1
  • attach disk22 to VM2 (just like you would switch disks on a regular PC)
  • start VM1 (but running off of a VM2's cloned disk)

Done. Very simple and elegant. You can then Create another VM using VM1's old disk (which is detached at the moment).

I don't know if there will be any issues with booting Windows or any other running services (that may have depend on the old VM'2 IP) but this is your part.

Second method - switch addresses. More complicated but you may loose your original IP - but if you're the only person having administration rights to this VPC (or a project) then you should be OK.

Ephemeral internal IP addresses: these addresses are available to VM instances and forwarding rules. Ephemeral internal IP addresses remain attached to VM instances and forwarding rules until the instance or forwarding rule is deleted.

Procedure is as fallows (for this example my VPC network name was default - if yours is names otherwise please make proper changes).

  • create new VM2 with all needed configuration
  • stop VM2
  • create new VPC and call it lan1 (or whatever you like)
  • change Network on VM2's primary network interface to lan1 (select ephemeral internal IP address):

enter image description here

  • stop VM1
  • note VM1's IP - lets call it ip1
  • change Network on VM1's primary network interface to lan1 and select "Ephemeral (automatic) as Primary Internal IP:

enter image description here

  • change Network on VM2's primary network interface to default and select as "Ephemeral (custom)" Primary Internal IP and type the noted earlied IP in the "address" field. enter image description here
  • start VM2

Why going through lan1 ? For some reason GCP console refuses to change even ephemeral (custom) address to different one: enter image description here

This way you end up with VM2 having address you wanted.

If you want it to be a static one you need to promote it.


Both methods minimise the downtime to a couple of minutes.

If you're OK with a longer downtime and can create&configure you new VM quick then you can turn off VM1, change it's IP, reserve it as a new static internal IP and then create VM2 with that IP.

Upvotes: 1

Martin Zeitler
Martin Zeitler

Reputation: 76859

This should be possible, while you'd have to use a temporary third one IP address, in order not to produce a duplicate address conflict among one of the other two addresses, while changing them. They MUST be unique at any time, else you'll have a routing problem. The most straightforward might be to use both server's serial consoles, because these would not care about temporary IP address conflicts. The crux is, that one cannot change both at precisely the same time, and as soon as having produced the duplicate IP address, one may want to resort to the serial console anyway.

Upvotes: 0

Related Questions