Migrate a Linux VM using Migrate to Containers

In this quickstart, you create a Compute Engine virtual machine (VM), then use Migrate to Containers to migrate the VM to Google Kubernetes Engine (GKE).

Before you begin

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.

  3. Enable the Compute Engine API.

    Enable Compute Engine API

  4. Enable the GKE API.

    Enable GKE API

  5. To use the command-line samples in this guide, do the following:

    1. Install or update to the latest version of the Google Cloud CLI.
    2. Set a default region and zone.

Create a Compute Engine VM with a web server

This section describes how to create a simple Compute Engine VM that serves a "Hello World!" web page that you can use for testing after migration is complete.

  1. In the Google Cloud console, click Activate Shell ButtonActivate Cloud Shell.

    Go to Google Cloud console

  2. To create a VM, run the following command:

    gcloud compute instances create quickstart-instance \
      --zone=us-central1-a --machine-type=e2-standard-2 \
      --subnet=default --scopes="cloud-platform" \
      --tags=http-server,https-server --image=ubuntu-minimal-1604-xenial-v20210119a \
      --image-project=ubuntu-os-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard \
      --boot-disk-device-name=quickstart-instance
    
  3. To create a firewall rule that allows requests to the instance over HTTP, run the following command:

    gcloud compute firewall-rules create default-allow-http \
     --direction=INGRESS --priority=1000 --network=default --action=ALLOW \
     --rules=tcp:80 --source-ranges=0.0.0.0/0 --target-tags=http-server
    
  4. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  5. To connect to the VM quickstart-instance, locate the row for the VM, then click SSH.

    The connection is established in a new tab.

  6. To install the apache2 package, in the new quickstart-instance command tab, run the following command:

    sudo apt-get update && sudo apt-get install apache2 -y
    
  7. After installing Apache, the operating system automatically starts the Apache server.

  8. In the same tab, to overwrite the Apache web server default web page with a new page, run the following command:

    echo '<!doctype html><html><body><h1>Hello World!</h1></body></html>' | sudo tee /var/www/html/index.html
    
  9. Go back to the VM instances page and copy the External IP address displayed for quickstart-instance.

  10. Paste the IP address of the VM in your browser address bar. Prefix it with http://.

    You should now see the "Hello World!" page.

  11. On the VM instances page, select the checkbox at the left end of the row for quickstart-instance.

  12. To stop the VM, at the top of the page, click Stop.

    You need to stop the VM before starting a migration. You can start it again after the migration is complete.

Create a processing cluster

In this section, you create a GKE cluster in Google Cloud that you use as a processing cluster.

  1. In the Google Cloud console, click Activate Shell ButtonActivate Cloud Shell.

    Go to Google Cloud console

  2. To create a new Kubernetes cluster to use as a processing cluster, run the following command:

    gcloud container clusters create migration-processing \
     --zone=us-central1-a --machine-type e2-standard-4 \
     --image-type ubuntu_containerd --num-nodes 1 \
     --enable-stackdriver-kubernetes \
     --subnetwork "projects/PROJECT_ID/regions/us-central1/subnetworks/default"
    

    Replace PROJECT_ID with your project ID.

    Allow a few minutes for the cluster creation to complete.

Install Migrate to Containers

  1. In the Google Cloud console, go to the Migrate to Containers page.

    Go to Migrate to Containers

  2. Click the Processing Clusters tab, then click Add processing cluster.

  3. From the Workloads OS type list, select Linux, and click Next.

  4. From the Select a cluster list, select migration-processing, and click Next.

  5. In the Configuration section, leave the default values as is and click Next.

  6. In the Service account section, select Create a new service account.

  7. In the Service account name field, enter quickstart-sa1.

  8. Click Continue and then click Deploy.

    Allow a few minutes for the processing cluster setup to complete.

Migrate the Compute Engine VM

In the following sections, you specify the migration source as a Compute Engine VM. This step adds the source specification to the migration plan. Then, you create a migration plan with migration details and use it to migrate the VM.

Specify the migration source

  1. In the Google Cloud console, go to the Migrate to Containers page.

    Go to Migrate to Containers

  2. Click Add Source.

  3. From the Select a processing cluster list, select migration-processing, and click Next.

  4. In the Name field, enter quickstart-source.

  5. Leave the Source type as Compute Engine, and click Next.

  6. In the Project field, specify the project ID of the project that contains the VM you want to migrate.

  7. Select Create a new service account to create an account that lets you use Compute Engine as a migration source.

  8. In the Service account name field, enter quickstart-sa2.

  9. Click Continue and then click Add Source.

Create a migration

  1. In the Google Cloud console, go to the Migrate to Containers page.

    Go to Migrate to Containers

  2. Click Create Migration.

  3. In the Migration name field, enter quickstart-migration.

  4. From the Select source list, select quickstart-source.

  5. From the Workload type list, select Linux system container.

  6. In the Instance Name field, enter quickstart-instance.

  7. Click Create Migration.

    The migration is added to the Migrations table. However, it can take several minutes for migration creation to complete. When it completes, the Status column is updated in the Migrations table.

Deploy the migrated workload

To generate the target container artifacts as part of processing a VM for migration, do the following:

  1. In the Google Cloud console, go to the Migrate to Containers page.

    Go to Migrate to Containers

  2. For quickstart-migration, in the Next steps column, select Options - Generate artifacts. When the artifacts are generated, the Status column for the migration is updated.

  3. Once the migration is complete, click Activate Shell ButtonActivate Cloud Shell.

  4. To download the generated YAML artifacts, run the following command:

    migctl migration get-artifacts quickstart-migration
    

    The command downloads the following files that were generated during the migration:

    • deployment_spec.yaml - The YAML file that configures your workload.
    • Dockerfile - The Dockerfile used to build the image for your migrated VM.
    • migration.yaml - A copy of the migration plan.
    • blocklist.yaml - The list of container services to disable based on your settings in the migration plan.
  5. To open the Cloud Shell Editor, click Open editor.

  6. Open the deployment_spec.yaml file and locate the Service object with the name quickstart-instance. It should appear as shown below:

    apiVersion: v1
    kind: Service
    metadata:
      creationTimestamp: null
      name: quickstart-instance
    spec:
      clusterIP: None
      selector:
        app: quickstart-instance
      type: ClusterIP
    status:
      loadBalancer: {}
    
    ---
    
  7. Beneath the Service definition shown above, paste the Service definition highlighted in blue below that exposes port 80 for access to your web server over HTTP:

    apiVersion: v1
    kind: Service
    metadata:
      creationTimestamp: null
      name: quickstart-instance
    spec:
      clusterIP: None
      selector:
        app: quickstart-instance
      type: ClusterIP
    status:
      loadBalancer: {}
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: hello-service
    spec:
      selector:
        app: quickstart-instance
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: LoadBalancer
    
    ---
    

    If required, fix the indentation of the Service definition in the file.

  8. Save the file.

  9. Click Open Terminal.

  10. To deploy the migrated VM, run the following command:

    skaffold run -d eu.gcr.io/PROJECT_ID
    

    Replace PROJECT_ID with your project ID.

  11. To get the external IP address of the migrated VM, run the following command:

    kubectl get service hello-service
    

    When the web server is ready, you see an external IP address for the hello-service you added.

    kubectl get service hello-service
    NAME            TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
    hello-service   LoadBalancer   10.23.241.124   ##.##.###.##   80:32611/TCP   5m4s
    
  12. To check if the migration was successful, open a new browser tab and visit the web page at the external IP address (make sure to use HTTP and not HTTPS).

    http://##.##.###.##
    

    If you see the text Hello World! displayed on your screen, your VM migration was successful.

    If you're not able to access your migrated workload, see how to troubleshoot known issues.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

Delete the migration

  1. in the Google Cloud console, go to the Migrate to Containers page.

    Go to Migrate to Containers

  2. Click the Migrations tab to display a table containing the available migrations.
  3. For quickstart-migration, click Delete, on the right side of the table, and then select Delete migration.

Delete the migration source

  1. In the Google Cloud console, go to the Migrate to Containers page.

    Go to Migrate to Containers

  2. Click the Manage sources to display the list of available sources.
  3. For quickstart-source, click Delete, on the right side of the table.
  4. Confirm deletion by typing the displayed text and click Delete again.

Delete the GKE cluster

  1. In the Google Cloud console, go to the Google Kubernetes Engine page.

    Go to Google Kubernetes Engine

  2. Select migration-processing, and then click Delete.
  3. When prompted to confirm, click Delete again.

Delete the service accounts

  1. In the Google Cloud console, go to the Service accounts page.

    Go to Service accounts

  2. Select the project where you created the service accounts.
  3. Select quickstart-sa1 and quickstart-sa2 and then click Delete.
  4. When prompted to confirm, click Delete again.

Delete the VM

  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. Click the name quickstart-instance.
  3. At the top of the instance's details page, click Delete.

What's next