Migrating from Compute Engine to GKE

You can migrate your VMs from Compute Engine to containerized VMs on Google Kubernetes Engine (GKE). Through the steps in this topic, you'll create YAML to configure GKE objects to support migration to your own GKE cluster.


Building configuration files

Open Cloud Shell or a terminal

  1. Open Cloud Shell

Continue with installation

  1. Install the required dependencies.

    pip3 install --user pyyaml

  2. Open the VM Instances page on the Cloud Console and Stop the Compute Engine instance you are migrating.

  3. Use the clone_vm_disks script to clone the VM's disks and generate a deployment YAML file:

    python3 /google/migrate/anthos/gce-to-gke/clone_vm_disks.py \
    -p project-name \
    -z zone-name \
    -T target-zone-name \
    -i vm-name \
    -A app-name \
    -o yaml-file-name

    Use options as described in the following table:

    Option Description
    -p Your Google Cloud project name.
    -z Google Cloud zone that hosts the VM. For example, 'us-central1-a'.
    -T Google Cloud zone that hosts the GKE cluster. For example, 'us-central-b'.

    Be sure to specify a zone that is also the location of a GKE node to which you're migrating.

    -i Name of the VM. For example, 'myapp-vm'.
    -A Name of workload that will be launched in GKE. This will be used as the StatefulSet name in the generated YAML, and as a prefix for the PersistentVolumeClaim name.
    -o Filename of resulting YAML configuration.

    For example:

    python3 /google/migrate/anthos/gce-to-gke/clone_vm_disks.py \
    -p 'my-project' \
    -z 'us-central-a' \
    -T 'us-central-b' \
    -i 'myapp-vm' \
    -A 'my-workload' \
    -o 'deployment.yaml'

Apply configuration to your cluster

Apply the generated configuration YAML to the cluster you created. This will migrate your VM to the cluster.

To apply the deployment to your cluster, run the following command:

kubectl apply -f yaml-file-name

where yaml-file-name is the file you generated earlier when building a configuration file.

To confirm that your workload is running on the cluster, open the GKE workloads page on the Google Cloud Console. Also, click Storage to view the Persistent volume claims created for you. Applying the generated YAML has created two storage classes, one with a storage class name and one without. The named storage class is bound to the cloned disk.

Your VM is now migrated to GKE.

Executing bash commands on your container

You can access a container through a bash shell using the kubectl exec command.

  1. Use kubectl describe pods to find the name of the Pod in your cluster that you want to connect to.

    In the following example, the command lists the suitecrm-0 pod.

    kubectl describe pods | grep Name
    Name:               suitecrm-0
  2. Execute shell commands using one of the following methods:
    • Use kubectl exec to open a bash command shell where you can execute commands.
      kubectl exec -it pod-name -- /bin/bash

      The following example gets a shell to the suitecrm-0 pod:

      kubectl exec -it suitecrm-0 -- /bin/bash
    • Use kubectl exec to execute commands directly.
      kubectl exec -it pod-name -- /bin/bash -c "command(s)"

      The following example lists the root directory of the suitecrm-0 pod:

      kubectl exec -it suitecrm-0 -- /bin/bash -c "ls /"

For more information, see the Kubernetes documentation.