使用 Migrate to Containers CLI 遷移 Linux VM
在本快速入門導覽課程中,您將建立 Compute Engine 虛擬機器 (VM) 執行個體,然後使用 Migrate to Containers CLI 將 VM 遷移至 Google Kubernetes Engine (GKE)。
事前準備
在 Google Cloud 主控台的專案選擇器頁面中,選取或建立 Google Cloud 專案。
請確認您已為 Google Cloud 專案啟用計費功能。瞭解如何檢查專案是否已啟用計費功能。
啟用 Compute Engine API。
啟用 GKE API。
建立含有網路伺服器的 Compute Engine VM,做為來源 VM
本節說明如何建立 Compute Engine VM,以便在遷移作業完成後,提供可用於測試的 Hello World! 網頁。
在 Google Cloud 控制台中,按一下
「啟用 Cloud Shell」。
建立 VM:
gcloud compute instances create quickstart-source-vm \ --zone=us-central1-a --machine-type=e2-medium \ --subnet=default --scopes="cloud-platform" \ --tags=http-server,https-server --image=ubuntu-2204-jammy-v20240208 \ --image-project=ubuntu-os-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard \ --boot-disk-device-name=quickstart-source-vm
請稍候幾分鐘,讓 VM 建立作業完成。
建立防火牆規則,允許透過 HTTP 傳送至執行個體的要求:
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
前往「VM instances」(VM 執行個體) 頁面。
如要連線至 VM
quickstart-source-vm
,請找出 VM 所在的資料列,然後點選「SSH」SSH。在新分頁中建立連線。
在
quickstart-source-vm
指令分頁中,安裝apache2
套件:sudo apt-get update && sudo apt-get install apache2 -y
安裝 Apache 之後,作業系統會自動啟動 Apache 伺服器。
在同一個分頁中,使用新頁面覆寫 Apache 網路伺服器的預設網頁:
echo '<!doctype html><html><body><h1>Hello World!</h1></body></html>' | sudo tee /var/www/html/index.html
返回「VM instances」頁面,然後複製
quickstart-source-vm
的「External IP」位址。在瀏覽器網址列中貼上 VM 的 IP 位址。請在名稱前方加上
http://
。系統會顯示「Hello World!」頁面。
建立 Compute Engine VM 做為本機機器
在本節中,您將建立 Linux VM,並將其用作本機機器,以便執行不同的 Migrate to Containers 作業,用於遷移來源 VM。
在 Google Cloud 控制台中,按一下
「啟用 Cloud Shell」。
建立 VM:
gcloud compute instances create quickstart-local-vm \ --zone=us-central1-a --machine-type=e2-medium \ --subnet=default --scopes="cloud-platform" \ --tags=http-server,https-server --image=ubuntu-2204-jammy-v20240208 \ --image-project=ubuntu-os-cloud --boot-disk-size=50GB --boot-disk-type=pd-standard \ --boot-disk-device-name=quickstart-local-vm
請等候幾分鐘,讓 VM 建立作業完成。
建立 GKE 叢集
在本節中,您將在 Google Cloud中建立 GKE 叢集,並在稍後的快速入門課程中,在該叢集中部署容器化工作負載。
在 Cloud Shell 中建立新的 Kubernetes 叢集:
gcloud container clusters create quickstart-cluster \ --zone=us-central1-a --machine-type=e2-medium \ --image-type=ubuntu_containerd --num-nodes=1 \ --logging=SYSTEM,WORKLOAD,API_SERVER,SCHEDULER,CONTROLLER_MANAGER
叢集建立作業需要幾分鐘才能完成。
準備本機 VM 以便執行遷移作業
前往「VM instances」(VM 執行個體) 頁面。
如要連線至 VM
quickstart-local-vm
,請找出 VM 所在的資料列,然後按一下「SSH」SSH。在新分頁中建立連線。
在
quickstart-local-vm
指令分頁中,安裝 Google Cloud CLI:# Import the Google Cloud public key. curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg # Add the gcloud CLI distribution URI as a package source echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list # Update and install gcloud sudo apt-get update && sudo apt-get install google-cloud-cli # Get started gcloud init
如要完成 gcloud CLI 設定,請按照
quickstart-local-vm
指令分頁中顯示的指示操作:- 如要選取要用來執行各種作業的帳戶,請輸入
1
。 - 如要選取建立 VM 的專案,請輸入
1
。 - 如要略過設定預設地區和區域,請輸入
n
。
- 如要選取要用來執行各種作業的帳戶,請輸入
安裝 Docker 並允許非 root 使用者執行容器:
# Download the convenience script curl -fsSL https://get.docker.com -o install-docker.sh # Install Docker sudo sh install-docker.sh # Allow the non-root user to access Docker sudo usermod -aG docker $USER # Activate the group changes newgrp docker
安裝 Skaffold:
curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-amd64 && \ sudo install skaffold /usr/local/bin/
下載 Migrate to Containers CLI:
curl -O "https://m2c-cli-release.storage.googleapis.com/$(curl -s https://m2c-cli-release.storage.googleapis.com/latest)/linux/amd64/m2c" chmod +x ./m2c
遷移來源 VM
遷移作業通常包含三個步驟:複製來源 VM 檔案系統、分析檔案系統以建立遷移計畫,以及產生遷移構件。
以下各節將說明如何使用 Migrate to Containers CLI,透過本機 Linux VM 遷移來源 VM。
複製來源 VM 檔案系統
當您複製來源 VM 檔案系統時,Migrate to Containers CLI 會使用預設篩選器來縮減複製檔案系統的大小。不過,為了讓 Apache 伺服器可供啟動,伺服器需要 /var/log
目錄,而這個目錄會遭到預設篩選器移除。
本節將說明如何編輯預設篩選器,確保 /var/log/*
會與來源 VM 檔案系統一併複製。
在
quickstart-local-vm
指令分頁中,取得名為filters.txt
的檔案中預設的「Migrate to Containers」篩選器清單:./m2c copy default-filters > filters.txt
如要編輯預設篩選器,請按照下列步驟操作:
在文字編輯器中開啟
filters.txt
檔案:vi filters.txt
如要變更檔案內容,請按下
i
。移除以下陳述式:
- /var/log/*
如要儲存檔案並退出文字編輯器,請按下
Esc
鍵,然後輸入:wq
。
複製來源機器的檔案系統:
./m2c copy gcloud \ --project PROJECT_ID --zone us-central1-a \ --vm-name quickstart-source-vm --output quickstart-vm-filesystem \ --filters filters.txt
將 PROJECT_ID 替換為專案 ID。
來源機器的檔案系統副本可在
quickstart-vm-filesystem
目錄中找到。
建立遷移計畫
在
quickstart-local-vm
指令分頁中建立遷移計畫:./m2c analyze \ --source quickstart-vm-filesystem --plugin linux-vm-container \ --output analysis-output
分析完成後,系統會建立名為
analysis-output
的新目錄,其中包含遷移計畫config.yaml
。
產生遷移構件
在
quickstart-local-vm
指令分頁中產生遷移構件:./m2c generate --input analysis-output --output migration-artifacts
產生的遷移構件會新增至
migration-artifacts
目錄。
部署已遷移的工作負載
在本節中,您會將已遷移的工作負載從本機 VM quickstart-local-vm
部署至在Google Cloud上執行的 GKE 叢集。
在
quickstart-local-vm
指令分頁中,安裝gke-gcloud-auth-plugin
外掛程式:sudo apt-get install google-cloud-sdk-gke-gcloud-auth-plugin
安裝
kubectl
:sudo apt-get install kubectl
連線至叢集
quickstart-cluster
:gcloud container clusters get-credentials quickstart-cluster \ --zone us-central1-a --project PROJECT_ID
開啟
migration-artifacts
資料夾:cd migration-artifacts
如要將工作負載公開至網際網路,請在
deployment_spec.yaml
檔案中新增LoadBalancer
服務。如要編輯deployment_spec.yaml
檔案,請執行下列操作:在文字編輯器中開啟
deployment_spec.yaml
檔案:vi deployment_spec.yaml
如要變更檔案內容,請按下
i
。找出名稱為
linux-system
的Service
物件。如下所示:apiVersion: v1 kind: Service metadata: labels: anthos-migrate.cloud.google.com/type: linux-container migrate-for-anthos-optimization: "true" migrate-for-anthos-version: m2c-cli-1.2.2 name: linux-system spec: clusterIP: None selector: app: linux-system type: ClusterIP
在
linux-system
後方新增另一個名為hello-service
的Service
物件:apiVersion: v1 kind: Service metadata: labels: anthos-migrate.cloud.google.com/type: linux-container migrate-for-anthos-optimization: "true" migrate-for-anthos-version: m2c-cli-1.2.2 name: linux-system spec: clusterIP: None selector: app: linux-system type: ClusterIP --- # Add the hello-service object apiVersion: v1 kind: Service metadata: name: hello-service spec: selector: app: linux-system ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer
如要儲存檔案並退出文字編輯器,請按下
Esc
鍵,然後輸入:wq
。
部署已遷移的 VM:
skaffold run -d eu.gcr.io/PROJECT_ID
取得已遷移 VM 的外部 IP 位址:
kubectl get service hello-service
網路伺服器準備就緒後,您會看到所新增
hello-service
的外部 IP 位址。NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-service LoadBalancer 10.23.241.124 EXTERNAL_IP 80:32611/TCP 5m4s
如要確認遷移作業是否成功,請開啟新的瀏覽器分頁,並前往外部 IP 位址的網頁。
http://EXTERNAL_IP
請務必使用
HTTP
,而非HTTPS
。如果畫面上顯示「Hello World!」文字,表示虛擬機器人遷移作業已成功。
如果您無法存取已遷移的工作負載,請參閱如何排解已知問題。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取您在本頁所用資源的費用,請按照下列步驟操作。
刪除 GKE 叢集
- 前往 Google Cloud 控制台的「GKE」GKE頁面。
- 選取
quickstart-cluster
,然後按一下 「刪除」。 - 系統提示您進行確認時,再按一下 [Delete] (刪除)。
刪除 VM
- 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
- 按一下名稱
quickstart-source-vm
。 - 在執行個體詳細資料頁面的頂端,按一下 「Delete」。
- 按一下名稱
quickstart-local-vm
。 - 在執行個體詳細資料頁面的頂端,按一下 「Delete」。
後續步驟
- 瞭解如何批次處理訪客探索和資料收集作業。
- 如要瞭解如何遷移更複雜的應用程式,請參閱「Migrate to Containers」GitHub 存放區中的遷移 Spring 架構應用程式教學課程。