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
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
Enable the Compute Engine API.
Enable the GKE API.
To use the command-line samples in this guide, do the following:
- Install or update to the latest version of the Google Cloud CLI.
- 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.
In the Google Cloud console, click
Activate Cloud Shell.
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
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
In the Google Cloud console, go to the VM instances page.
To connect to the VM
quickstart-instance
, locate the row for the VM, then click SSH.The connection is established in a new tab.
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
After installing Apache, the operating system automatically starts the Apache server.
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
Go back to the VM instances page and copy the External IP address displayed for
quickstart-instance
.Paste the IP address of the VM in your browser address bar. Prefix it with
http://
.You should now see the "Hello World!" page.
On the VM instances page, select the checkbox at the left end of the row for
quickstart-instance
.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.
In the Google Cloud console, click
Activate Cloud Shell.
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
In the Google Cloud console, go to the Migrate to Containers page.
Click the Processing Clusters tab, then click Add processing cluster.
From the Workloads OS type list, select Linux, and click Next.
From the Select a cluster list, select
migration-processing
, and click Next.In the Configuration section, leave the default values as is and click Next.
In the Service account section, select Create a new service account.
In the Service account name field, enter
quickstart-sa1
.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
In the Google Cloud console, go to the Migrate to Containers page.
Click the Sources & Candidates tab and then click Add Source.
From the Select a processing cluster list, select
migration-processing
, and click Next.In the Name field, enter
quickstart-source
.Leave the Source type as Compute Engine, and click Next.
In the Project field, specify the project ID of the project that contains the VM you want to migrate.
Select Create a new service account to create an account that enables you to use Compute Engine as a migration source.
In the Service account name field, enter
quickstart-sa2
.Click Continue and then click Add Source.
Create a migration
In the Google Cloud console, go to the Migrate to Containers page.
Click Create Migration.
In the Migration name field, enter
quickstart-migration
.From the Select source list, select
quickstart-source
.From the Workload type list, select Linux system container.
In the Instance Name field, enter
quickstart-instance
.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:
In the Google Cloud console, go to the Migrate to Containers page.
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.Once the migration is complete, click
Activate Cloud Shell.
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.
To open the Cloud Shell Editor, click Open editor.
Open the
deployment_spec.yaml
file and locate theService
object with the namequickstart-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: {} ---
Beneath the
Service
definition shown above, paste theService
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.Save the file.
Click Open Terminal.
To apply the updated deployment spec YAML to deploy the workload, run the following command:
kubectl apply -f deployment_spec.yaml
It may take a few minutes for the deployment to finish.
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
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
- in the Google Cloud console, go to the Migrate to Containers page.
- Click the Migrations tab to display a table containing the available migrations.
- For
quickstart-migration
, click Delete, on the right side of the table, and then select Delete migration.
Delete the migration source
- In the Google Cloud console, go to the Migrate to Containers page.
- Click the Manage sources to display the list of available sources.
- For
quickstart-source
, click Delete, on the right side of the table. - Confirm deletion by typing the displayed text and click Delete again.
Delete the GKE cluster
- In the Google Cloud console, go to the Google Kubernetes Engine page.
- Select
migration-processing
, and then click Delete. - When prompted to confirm, click Delete again.
Delete the service accounts
- In the Google Cloud console, go to the Service accounts page.
- Select the project where you created the service accounts.
- Select
quickstart-sa1
andquickstart-sa2
and then click Delete. - When prompted to confirm, click Delete again.
Delete the VM
- In the Google Cloud console, go to the VM instances page.
- Click the name
quickstart-instance
. - At the top of the instance's details page, click Delete.
What's next
- Learn how to batch mfit guest data collection.