Migrate for Anthos: Modernized approach for migrating Compute Engine to Kubernetes Engine

By February 27, 2020 March 16th, 2020 Blogs

Written by Madan Mohan K, Associate Cloud Architect

ANTHOS-One Management Solution for a hybrid cloud and multi-cloud world

The growing importance of hybrid cloud and multi-cloud environments is transforming the entire computing industry as well as the way businesses can leverage technology to innovate. Economics and speed are the two greatest issues driving this market change. Using a hybrid cloud/multi-cloud not only allows companies to scale computing resources, but it also eliminates the need to make massive capital expenditures to handle short-term spikes in demand as well as when the business needs to free up local resources for more sensitive data or applications.

Anthos:

Anthos is an open-source application platform that enables an enterprise to modernize their existing applications on hybrid or multi-cloud environments. You can build new VMs and run them anywhere in a secure manner. Anthos is built on open source technologies pioneered by Google—including Kubernetes, Istio, and Knative—and enables consistency between on-premises and cloud environments.

When workloads are upgraded to containers, IT departments can eliminate OS-level maintenance and security patching for VMs and automate policy and security updates at scale. Monitoring across on-premises and cloud environments are done through a single interface in the Google Cloud Console.

Scenario:

Rewriting existing applications to Kubernetes isn’t always possible or feasible to do manually. That’s where Migrate for Anthos can help, by modernizing the existing applications and getting them to run in Kubernetes.

Migrate for Anthos:

Migrate for Anthos provides an almost real-time solution to take an existing VM and make it available as a Kubernetes hosted pod with all the values associated with executing the applications in a Kubernetes cluster.

Let’s look at an example, migrating a Compute Engine instance to a Kubernetes Engine cluster running Migrate for Anthos to start with the basics.

Prerequisites:

https://cloud.google.com/migrate/anthos/docs/gce-to-gke-prerequisites

Compatible VM operating systems:

https://cloud.google.com/migrate/anthos/docs/compatible-os-versions

Instance Creation:

  • From the Console go to Compute Engine > VM Instances, then click the Create button
  • Name the instance “migrate-vm-anthos” or whichever preferred, check the box for “Allow HTTP traffic“, and accept all the other defaults. Click Create.
  • Once the VM is created, SSH

Install the Apache web server by running the following commands:

sudo apt-get update
 sudo apt-get install apache2 -y
 echo "Hello World" > index.html
 sudo mv index.html /var/www/html

A sample Hello World page is displayed.

Note: To migrate the VM, first stop it from running

We need a Kubernetes cluster to migrate the virtual machine into. The Migrate for Anthos app can be deployed to an existing cluster as well. Let’s install Migrate for Anthos through the Google Cloud Marketplace.

Deploying Migrate for Anthos

Navigate to Market place and search Migrate for Anthos.

Click the configure button.

For this lab, we can accept the default settings. Click the Create cluster button.

Once the cluster is created, check the box to accept the Terms of Service, then click the Deploy button. The migration for the Anthos environment will now be set up.

Migrating your VM to your new container:

Open cloud shell and run the following

pip3 install --user pyyaml 

This installs a Python prerequisite that will process YAML files.

Execute the following command

python3 /google/migrate/anthos/gce-to-gke/clone_vm_disks.py \
  -p $GOOGLE_CLOUD_PROJECT \
  -z us-central1-a \
  -T us-central1-a \
  -i migrate-vm-anthos \
  -A myworkload \
  -o myYaml.yaml

This command will take a few minutes to complete. The migration file is a YAML file called myYaml.yaml, created by Migrate for Anthos. When deployed to Kubernetes, it will perform the migration.

A successful yaml generation is seen in the below screenshot

Next we must initialize the kubectl environment and perform the migration by running the following command

kubectl apply -f myYaml.yaml

The execution result is obtained as shown

In the Console, from the Navigation menu, browse to Kubernetes Engine > Workloads. You will see a workload called myworkload. Wait for its status to change to OK

Validate the Migrated Instance:

In Cloud Shell, log in to the Kubernetes pod that is running the workload that has been migrated:

kubectl exec -it myworkload-0 -- /bin/bash
curl localhost

Well, the application works as expected.

Open the code editor in cloud shell and edit the myYaml.yaml file and do add the following at the top.

apiVersion: v1
kind: Service
metadata:
  name: myworkload-svc
  labels:
    app: myworkload
spec:
  type: LoadBalancer
  ports:
  - port: 80
    name: web
  selector:
    app: myworkload
---

Now find the entry for the StatefulSet definition. Find the entry for containers that is nested at. Add the following two lines directly below myworkload

ports:
- containerPort: 80

Make sure the indentation places the ports,element as child of name: myworkload. Save the file.

Apply the Kubernetes changes:

kubectl apply -f myYaml.yaml

There you go the service is exposed and we can validate it by navigating to the Service & Ingress section.

A browser tab will appear, and you will see the web page which is the simple text “Hello World”. This illustrates that you have successfully migrated the web server that was running in the Compute Engine to be running in a Kubernetes cluster.

Inference:

Anthos unites all of Google Cloud Platform’s powerful tools under one roof, and in doing so it delivers unprecedented efficiency, scalability, and cost-effectiveness to IT operations. With an introduction to Anthos, an organization can enjoy the full benefits of managing its multi and hybrid cloud environment at ease and it also offers the ability to innovate using cloud technologies.

Leave a Reply