aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Computing
  • Engineering

How To Migrate A Group Of Individual Instances To A Stateful MIG Using Python Script

  • aster.cloud
  • July 11, 2022
  • 4 minute read

A GCP Compute Engine managed instance group (MIG) supports any VM configuration that you need, and it helps manage the VMs for you. For example, when a VM in a MIG unexpectedly stops running, the MIG recreates that VM according to the configuration that you set. You can also set up an application-based health check to verify that your application responds as expected on each VM.

In addition to that, MIGs also allows you to deploy complex stateful applications, such as databases (Cassandra, ElasticSearch) or data processing applications (Kafka, Flink), where preservation of individual VM state (for example, a database shard, or app configuration) is important.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

If you are just starting to work with GCP and want to deploy a stateful application, then it’s highly recommended to use a stateful MIG configuration. A stateful MIG allows you to preserve the unique state of each instance (including instance name, attached persistent disks, IP addresses – available in Preview, and metadata) on VM restart, recreation, auto-healing, or update. You can learn more about Stateful MIGs by reading the docs.

The goal of this blog post is to help those who already have an existing stateful application running on standalone (unmanaged) Compute Engine instances and who want to automatically migrate them to a stateful MIG using a Python script. The script code and detailed instructions for its use and installation can be found here. In case you want to do this whole process manually, you can go through the Migrate an existing workload to a stateful managed instance group tutorial.

Let’s dive into the logic of the script and see how it can be executed.

Read More  Unlocking The Value Of Unstructured Data At Scale Using BigQuery ML And Object Tables

Script requirements

Before running the script, make sure your existing instances meet the following criteria:

  • All source instances must have the same instance configuration (for example, machine type). Remember that all instances in a MIG are created according to the same instance template. The script creates a template for you based on one of your existing instances.
  • Your boot disks should be stateless. In case you must preserve state on your boot disks, then you can still create a stateful MIG, but you cannot update operating systems or software by rolling out boot image updates.
  • The script will stop your source VM instances. The script leaves all source VMs stopped with their disks intact, for easy reverting if the MIG doesn’t work as expected. This results in additional costs, for the following reasons:
  1. The script doesn’t detach or delete the original disks.
  2. The script creates images from existing disks.
  • You do not depend on your VM names. Source VM names remain unchanged. The new VMs in the MIG will have new names. Pay attention to this if you are somehow tied to the names of your VMs.

Script steps

  1. Stop all instances
  2. If needed, create a boot disk image
  3. Create an instance template based on the properties of a chosen instance, while ignoring attached data disks.
  4. Create an empty MIG.
  5. For each instance in the original group, perform the following steps:
    1. Clone all instance disks except the boot disk.
    2. Create an instance in the MIG based on the instance template, and include the cloned disks from the source instance.
  6. Print commands for cleaning up the source instances after you have verified that the stateful MIG serves your needs.
Read More  SuperGaming And Google Cloud Collaborate To Empower Game Developers With A SuperPlatform

If you are ready to use the script, follow the link for installation and execution.

Usage example

Let’s now imagine some instance configuration and show how the script proceeds. Suppose we have three standalone instances with names “alpha”, “beta”, and “gamma” in the us-central1-a zone and we want to migrate them to a MIG with the name “greek”.

Instance “alpha” has two additional disks: “alpha-disk-1” and “alpha-disk-2”, instance “beta” has one additional disk “beta-disk-1”, and “gamma” has zero additional disks. At the same time we would like to create a disk image for the boot image.

Run the following command:

$ python3 migrate_script.py -s alpha beta gamma -z us-central1-a -m greek --image_for_boot_disk

Let’s take a look on the output step by step:

  1. All the instances are stopped at the very beginning.

 

Instance alpha is not stopped. Stopping …
Instance alpha stopped

==========

Instance beta is not stopped. Stopping ...

Instance beta stopped

==========

Instance gamma is not stopped. Stopping ...

Instance gamma stopped

==========

 

2. Because we set the “image_for_boot_disk” flag, an image for the boot disk is created.

 

Creating disk image for boot image alpha …
Disk image alpha-image-ed0d24 created
==========

 

3. By default, alpha is chosen to specify machine configuration in the instance template (it’s possible to set another instance explicitly).

 

Creating base instance template ...
Instance template alpha-template-004cb8 created
==========

 

4. An empty MIG “gamma” is created.

 

Creating empty MIG gamma...
MIG gamma created
==========

 

5. For each individual instance, we create copies of all its disks (not to damage source instances data) and attach them to VM in the MIG.

Read More  Infuse AI into IBM Power mission critical workloads on the cloud – at our site or yours

 

Creating disk alpha-disk-1-bb2a30 from disk alpha-disk-1
==========
Creating disk alpha-disk-2-f7344b from disk alpha-disk-2
==========
Adding instance alpha-b7273d to greek MIG
==========
Creating disk beta-disk-1--7498a2 from disk beta-disk-1
==========
Adding instance beta-6475c6 to greek MIG
==========
Adding instance gamma-17d7c7 to greek MIG
==========
Migration successfully finished. Time spent: 457 seconds.

 

If you are convinced that everything is fine with the newly created MIG, you can delete your source instances. Use the following command:

 

$ gcloud compute instances delete alpha beta gamma

 

If instead, for any reason, you want to return to your initial configuration, use the following clean-up commands in the terminal or in the Cloud Shell:

 

$ gcloud compute instance-groups managed delete greek
$ gcloud compute instance-templates delete alpha-template-004cb8
$ gcloud compute disks delete alpha-disk-1-bb2a30
$ gcloud compute disks delete alpha-disk-2-f7344b
$ gcloud compute disks delete beta-disk-1-7498a2
$ gcloud compute images delete alpha-image-ed0d24

 

Congratulations! Your MIG is set up! What’s next?

– Configure autohealing

– Use a stateful policy instead of per-instance configurations

– Add more VMs

 

 

By: Fedor Isakov (Developer Relations Engineer)
Source: Google Cloud Blog


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • Compute Engine
  • Google Cloud
  • Python
  • Tutorials
You May Also Like
View Post
  • Computing
  • Multi-Cloud
  • Technology

Wiz: 80% of cloud breaches are caused by basic mistakes

  • April 13, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

Contact center monitoring best practices for CX leaders

  • April 9, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

Cloud vs. local backup: Which is right for your organization?

  • April 9, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

Why channel partners must design for tech sovereignty

  • April 7, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

“A lot of other cloud vendors have been let off the hook”: Oracle leans hard on one-size-fits-all appeal of OCI for enterprises

  • March 30, 2026
View Post
  • Computing
  • Technology

Google Cloud and NVIDIA expand AI innovation across industries at GTC 2026

  • March 17, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

Last year in AWS with Corey Quinn

  • March 9, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

A guide to contact center security best practices

  • March 6, 2026

Stay Connected!
LATEST
  • 1
    Expectations vs. Reality: The AI We Thought We’d Have in 10 Years
    • June 19, 2026
  • digital-nomad-freelancer-worker-2151205464 2
    One paperwork problem – Get your Digital Nomad Visa employment documents fast from UK, EU or Singapore
    • June 16, 2026
  • 3
    Samsung Art Store Brings Art Basel to Homes Worldwide With New Curated Collection
    • June 15, 2026
  • 4
    You Do Not Need to Invest in the IPO of SpaceX, Anthropic, and OpenAI
    • June 10, 2026
  • 5
    The consequences of relying on AI for accurate news
    • June 10, 2026
  • 6
    Connecting AI agents with unstructured data using Google Cloud Storage MCP Servers
    • June 10, 2026
  • 7
    WWDC26: Apple unveils next generation of Apple Intelligence, Siri AI, powerful parental controls, and an expansive set of software improvements
    • June 8, 2026
  • 8
    IBM and Google Cloud Announce Strategic Partnership to Scale AI with Human Expertise and AI‑Powered Delivery
    • June 4, 2026
  • Data center 9
    Data Sovereignty in Spain. It’s Not Just About the Law, It’s About Efficiency
    • June 3, 2026
  • 10
    Ink vs Pixels. What you miss versus what you are actually missing.
    • June 1, 2026
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    Banks race to patch new cyber vulnerabilities, and other cybersecurity news
    • May 25, 2026
  • pope-leo-xiv-cq5dam-1500.844 2
    Pope Leo XIV to Publish First Encyclical on Artificial Intelligence and Human Dignity on 25 May
    • May 22, 2026
  • 3
    Portfolio to Clients, and is Strengthened by Ongoing Project Glasswing Work
    • May 20, 2026
  • reMarkable Paper Pure 4
    Everything The reMarkable Paper Pure Actually Does
    • May 14, 2026
  • 5
    Scaling cloud and AI: Microsoft Azure’s commitment to Europe’s digital future
    • May 11, 2026
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.