啟用巢狀虛擬化功能


本文說明如何在虛擬機器 (VM) 執行個體上啟用巢狀虛擬化,以及如何確認是否可以建立巢狀 VM。使用下列其中一種方法,在 VM 上啟用巢狀虛擬化:

  • 建議做法:如要直接在新 VM 或現有 VM 上啟用巢狀虛擬化,請在建立 VM 時將 enableNestedVirtualization 欄位設為 true,或更新 VM。建議採用這種方法,因為您不需要建立自訂映像檔或使用特殊授權金鑰。

  • 如要使用特殊授權金鑰啟用巢狀虛擬化,請建立開機磁碟、使用特殊巢狀虛擬化授權金鑰建立自訂映像檔,然後建立使用該自訂映像檔的 VM。

事前準備

  • 如果尚未設定驗證,請先完成設定。 「驗證」是指驗證身分的程序,確認您有權存取 Google Cloud 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列其中一個選項,向 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    gcloud

      1. After installing the Google Cloud CLI, initialize it by running the following command:

        gcloud init

        If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      2. Set a default region and zone.
      3. REST

        如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

          After installing the Google Cloud CLI, initialize it by running the following command:

          gcloud init

          If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

        詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。

直接在新 VM 上啟用巢狀虛擬化

如要直接在 VM 上啟用巢狀虛擬化功能,請按照下列程序操作。

gcloud

使用下列 gcloud compute instances create 指令建立啟用巢狀虛擬化的 L1 VM:

gcloud compute instances create VM_NAME \
  --enable-nested-virtualization \
  --zone=ZONE \
  --min-cpu-platform="Intel Haswell"

更改下列內容:

  • VM_NAME:啟用巢狀虛擬化的新 L1 VM 名稱

  • ZONE:啟用巢狀虛擬化功能的新 L1 VM 所在區域

REST

使用下列 instances.insert 方法建立啟用巢狀虛擬化的 L1 VM:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances
{
  ...
  "name": "VM_NAME",
  ...
  "minCpuPlatform": "Intel Haswell",
  "advancedMachineFeatures": {
    "enableNestedVirtualization": true
  },
  ...
}

更改下列內容:

  • PROJECT_ID:專案 ID

  • ZONE:啟用巢狀虛擬化功能的新 L1 VM 所在區域

  • VM_NAME:啟用巢狀虛擬化的新 L1 VM 名稱

直接在現有 VM 上啟用巢狀虛擬化

如要在現有 VM 上啟用巢狀虛擬化功能,請按照下列步驟操作。

gcloud

  1. 使用下列 gcloud compute instances export 指令匯出 VM 的屬性:

    gcloud compute instances export VM_NAME \
      --destination=YAML_FILE_PATH \
      --zone=ZONE
    

    更改下列內容:

    • VM_NAME:要匯出屬性的 VM 名稱

    • YAML_FILE_PATH:.yaml 檔案的路徑和檔案名稱,匯出的設定資料會儲存至該檔案

    • ZONE:包含 VM 的可用區

  2. FILE_PATH 中儲存的 VM 設定檔中,更新 enableNestedVirtualization 的值。如果檔案中沒有這個值,請新增下列內容:

    advancedMachineFeatures:
      enableNestedVirtualization: true
    
  3. 使用下列 gcloud compute instances update-from-file 指令,以 enableNestedVirtualization 的值更新 VM:

    gcloud compute instances update-from-file VM_NAME \
      --source=FILE_PATH \
      --most-disruptive-allowed-action=RESTART \
      --zone=ZONE
    

    更改下列內容:

    • VM_NAME:要更新的 VM 名稱

    • FILE_PATH:更新後的 VM 設定檔路徑

    • ZONE:包含要更新 VM 的可用區

REST

使用下列instances.update 方法更新 enableNestedVirtualization 的值:

PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?most_disruptive_allowed_action=RESTART

{
  ⋮
  "advanced_machine_features": {
    ⋮
    "enableNestedVirtualization": "true"
  },
  ⋮
}

更改下列內容:

  • PROJECT_ID:專案 ID

  • ZONE:包含 VM 的可用區

  • VM_NAME:要匯出屬性的 VM 名稱

使用特殊授權金鑰啟用巢狀虛擬化功能

如要在 VM 上啟用巢狀虛擬化,請使用特殊授權金鑰建立自訂映像檔,在 L1 VM 上啟用 VMX。授權金鑰不收取額外費用。

  1. 從公用映像檔或自訂映像檔建立開機磁碟。以下範例使用 debian-cloud 做為映像檔專案,並使用 debian-10 做為映像檔系列。如果您已有含現有磁碟的 VM 執行個體,可以略過這個步驟。

    gcloud

    gcloud compute disks create DISK_NAME \
      --zone=ZONE \
      --image-project=debian-cloud \
      --image-family=debian-10
    

    更改下列內容:

    • DISK_NAME:新磁碟的名稱

    • ZONE:要在其中建立磁碟的可用區

    REST

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks
    
    {
      ...
      "name": "DISK_NAME",
      "sourceImage": "projects/debian-cloud/global/images/family/debian-10",
      ...
    }
    

    更改下列內容:

    • PROJECT_ID:專案 ID

    • ZONE:要在其中建立磁碟的可用區

    • DISK_NAME:新磁碟的名稱

  2. 使用巢狀虛擬化所需的特殊授權金鑰,建立自訂映像檔。

    gcloud

    gcloud compute images create IMAGE_NAME \
      --source-disk DISK_NAME \
      --source-disk-zone ZONE \
      --licenses "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"
    

    更改下列內容:

    • IMAGE_NAME:新圖片的名稱

    • DISK_NAME:先前建立的磁碟名稱

    • ZONE:要在哪個可用區中建立映像檔

    REST

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/images
    
    {
      ...
      "licenses": ["projects/vm-options/global/licenses/enable-vmx"],
      "name": "IMAGE_NAME",
      "sourceDisk": "zones/ZONE/disks/DISK_NAME",
      ...
    }
    

    更改下列內容:

    • PROJECT_ID:專案 ID

    • IMAGE_NAME:新圖片的名稱

    • ZONE:要在哪個可用區中建立映像檔

    • DISK_NAME:先前建立的磁碟名稱

  3. 使用特殊授權建立映像檔後,您可以視需要刪除來源磁碟。

    gcloud

    gcloud compute disks delete DISK_NAME --zone=ZONE
    

    更改下列內容:

    • DISK_NAME:要刪除的磁碟名稱

    • ZONE:包含要刪除磁碟的可用區

    REST

    DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME
    

    更改下列內容:

    • PROJECT_ID:專案 ID

    • ZONE:包含要刪除磁碟的可用區

    • DISK_NAME:要刪除的磁碟名稱

  4. 建立使用新映像檔和特殊授權的 VM。最低 CPU 平台必須為 "Intel Haswell"

    gcloud

    gcloud compute instances create VM_NAME \
        --zone=ZONE \
        --min-cpu-platform "Intel Haswell" \
        --image IMAGE_NAME
    

    更改下列內容:

    • VM_NAME:VM 名稱

    • ZONE:要在其中建立 VM 的可用區

    • IMAGE_NAME:先前建立的映像檔名稱

    REST

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances
    
    {
      ...
      "name": "VM_NAME",
      "minCpuPlatform": "Intel Haswell",
      "disks": [
        {
          "initializeParams": {
            "sourceImage": "IMAGE_NAME"
          }
        }
      ]
      ...
    }
    
    

    更改下列內容:

    • PROJECT_ID:專案 ID

    • VM_NAME:VM 名稱

    • ZONE:要在其中建立 VM 的可用區

    • IMAGE_NAME:先前建立的映像檔名稱

確認 VM 已啟用巢狀虛擬化

  1. 連結至 VM 執行個體。

    gcloud compute ssh VM_NAME
    

    VM_NAME 替換為要連線的 VM 名稱。

  2. 確認已啟用巢狀虛擬化。如果回應不是 0,表示已確認啟用巢狀虛擬化。

    grep -cw vmx /proc/cpuinfo
    

後續步驟