Migrate Your VMs with Migrate for Anthos and GKE!

Harieshkumar
The Cloudside View
Published in
5 min readAug 7, 2023

--

Migrating Virtual Machines (VMs) to containerized environments is becoming increasingly popular as organizations seek to modernize their applications and take advantage of the scalability, agility, and ease of management provided by container orchestration platforms like Google Kubernetes Engine (GKE). Google Cloud offers an effective solution for this transformation through Migrate for Anthos, which simplifies the migration of VMs to Kubernetes clusters.

In this blog post, we will walk you through a step-by-step guide on how to use Migrate for Anthos and GKE to seamlessly migrate a VM to a containerized environment. We will cover all the necessary prerequisites, setup procedures, and the actual migration process. By the end of this, you will be able to successfully containerize your VM-based workloads, unleashing the full potential of Kubernetes for your applications.

Step 1: Prerequisites

Before we begin the migration process, ensure you have completed the following prerequisites:

  1. Create a Compute Engine instance.
  2. Create a GKE cluster.

Step 2: Creating a Service Account with the necessary permissions

A service account is required to interact with Google Cloud Platform (GCP) resources and perform actions on behalf of the user. The commands create a service account, grant it the “roles/storage.admin” role (required for reading and writing data to storage buckets), and generate a JSON key for authentication.

In the Google Cloud Shell, run the following:

gcloud iam service-accounts create sa-name\
--project=project-name
gcloud projects add-iam-policy-binding project-name\
--member="serviceAccount:sa-name@project-name.iam.gserviceaccount.com" \
--role="roles/storage.admin"
gcloud iam service-accounts keys create sa-name.json \
--iam-account=sa-name@project-name.iam.gserviceaccount.com \
--project=project-name
gcloud container clusters get-credentials cluster-name\
--zone <zone>

Make sure to replace <zone> with the appropriate zone for your GKE cluster.

Step 3: Install mgctl for Migrate for Anthos and GKE

Next, Install mgctl. It is a command-line tool provided by Migrate for Anthos. This step installs mgctl and runs a doctor command to verify the installation and the readiness of your environment.

migctl setup install --json-key=sa-name.json
migctl doctor

Step 4: Create the Migration Source

In this step, you create a migration source for Migrate for Anthos. The migctl source create command is used to create a migration source for a Google Compute Engine VM. The ce parameter specifies the source type as Compute Engine. Replace source-name with a name for your migration source, and provide the GCP project name and the JSON key file previously generated.

migctl source create ce source-name --project project-name --json-key=m4a-ce-src.json

Step 5: Create the Migration Plan

A migration plan defines what needs to be migrated from the source VM to the target GKE cluster. The migctl migration create command creates a migration plan with the specified source (source-name), VM identifier (instance-name), and migration intent (Image, which means container image migration).

migctl migration create my-migration --source source-name --vm-id instance-name --intent Image

Wait for the migration status to show “complete” before proceeding to the next step:

migctl migration status my-migration

Step 6: Review the Migration Plan

You can review the migration plan by visiting the “Migrate to Containers” page in the Google Cloud Console to retrieve the migration YAML file and inspect the details. You can edit the migration plan YAML if needed using a text editor and then update it using the migctl migration update command.

By selecting the migration name. You can check the Monitoring tab for status details, the Events tab for migration events, or the YAML tab to view the YAML files.

Download the migration YAML file:

migctl migration get my-migration

If you made any changes, upload the edited migration plan:

migctl migration update my-migration --file my-migration.yaml

Step 7: Generate Artifacts

Generating artifacts involves generating Kubernetes manifests and configuration files required for deploying the workload on GKE. The migctl migration generate-artifacts command creates these artifacts based on the migration plan.

migctl migration generate-artifacts my-migration

Wait for the migration to complete:

migctl migration status my-migration

Step 8: Download and Apply Artifacts

Download the generated YAML artifacts:

migctl migration get-artifacts my-migration

Update the deployment_spec.yaml file to include the downloaded artifacts.

---# Headless Service specification for the migrated application
apiVersion: v1
kind: Service
metadata:
# Add your metadata here
spec:
# Add your specification here---# Service specification for the migrated application
apiVersion: v1
kind: Service
metadata:
# Add your metadata here
spec:
# Add your specification here

Apply the deployment spec YAML to deploy the workload:

kubectl apply -f deployment_spec.yaml

Step 9: Check the Application

After deploying the workload, you can check the external IP address assigned to the service using the kubectl get service command. This verifies that the application is successfully running on GKE and accessible externally.

kubectl get service hello-service

Hope this helped! Keep smiling, until next time :)

--

--