編輯運算執行個體的機器類型

如果虛擬機器 (VM) 執行個體未附加本機 SSD,且不屬於代管執行個體群組 (MIG),您可以在停止執行個體後變更機器類型

如果現有機器類型不適合 VM 執行的工作負載,請變更 VM 的機器類型。舉例來說,您可以在設定、開發及測試期間以較小的機器啟動 VM,並在準備好處理實際工作環境工作負載時,將 VM 變更為使用較大的機器類型。

您可以在下列情況使用此程序:

  • 如要變更為類似的機器類型,但 vCPU 數量或記憶體不同,且屬於相同機器系列,請按照下列步驟操作:
    • 你可以將 n2-highcpu-4 變更為 n2-standard-4
    • 你可以將 c3d-standard-30 變更為 c3d-highmem-30
  • 如要變更機器類型,請使用其他機器系列。 只有第一代和第二代機器系列支援這項功能,T2A 和 A2 機器系列除外。例如:
    • 你可以將 n2-standard-4 變更為 c2-standard-8
    • 你可以將 n1-highmem-4 變更為 n2d-standard-16

如要將使用第一代或第二代機器系列 (N1、N2、M1 等) 的 VM 機器類型,變更為第三代或更新機器系列 (M3、C3、N4 等) 的機器類型,請按照「將工作負載移至新的運算執行個體」一文所述的程序操作。

您可以變更機器類型,但不會影響下列資源:

  • VM 的 SSH 金鑰
  • VM 設定,例如 VM 中繼資料
  • 附加的 Persistent Disk 或 Hyperdisk 中的資料,包括已安裝的應用程式和應用程式資料

如要變更 MIG 中 VM 的機器類型,請參閱「在 MIG 中自動套用 VM 設定更新」。

事前準備

  • 瞭解如何停止 VM
  • 瞭解機器類型
  • 如果尚未設定驗證,請先完成設定。 「驗證」是指驗證身分的程序,確認您有權存取 Trusted Cloud by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,向 Compute Engine 進行驗證:

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

    Console

    When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.

    Java

    如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

      安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI

      Create local authentication credentials for your user account:

      gcloud auth application-default login

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    詳情請參閱 Set up authentication for a local development environment

    Python

    如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

      安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI

      Create local authentication credentials for your user account:

      gcloud auth application-default login

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    詳情請參閱 Set up authentication for a local development environment

    REST

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

      安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI

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

必要的角色

如要取得變更 VM 機型所需的權限,請要求管理員授予您專案的下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

這些預先定義角色具備變更 VM 機型所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:

所需權限

如要變更 VM 的機器類型,您必須具備下列權限:

  • compute.instances.setMachineType 在 VM 上
  • compute.instances.start 在 VM 上
  • compute.instances.stop 在 VM 上

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

限制

帳單相關注意事項

每個機器類型都是以不同的費率計費,因此請務必瞭解變更機器類型對於計費方式的影響。舉例來說,e2-standard-2 機器類型的費用比 e2-micro 機器類型高。

變更機器類型可能也會影響該 VM 的續用折扣。系統會針對相同地區中的不同類別個別計算續用折扣。如果您變更機器類型,且新的機器類型屬於不同的類別,VM 的後續執行時間便會列入新類別的續用折扣。

舉例來說,假設您有一個已執行半個月的 n2-standard-2 機器類型 VM,然後您決定要將機器類型變更為 m1-ultramem-40。一旦完成這項變更,Compute Engine 就會開始將 VM 的執行時間,算到記憶體最佳化 vCPU 和記憶體類別的續用折扣中。

帳單上會列出您變更機器類型前已套用至 n2-standard-2 機器類型的續用折扣,以及 m1-ultramem-40 的個別續用折扣 (前提是您的 VM 在 m1-ultramem-40 上的執行時間至少達到當月剩餘時間的 25%)。

變更為規模較小的機器類型

如果您從具有較多資源的機器類型變更為資源較少的機器類型 (例如從 e2-standard-8 機器類型改為使用 e2-standard-2),可能會遭遇硬體資源問題或效能限制情形,這是因為規模較小的機器類型其功能不如規模較大的機器類型強大。請確認新的機器類型可支援目前在 VM 上執行的所有應用程式和服務;或者,您也可以更新服務和應用程式,確保這些項目能在規模較小的機器類型上正常運作。

變更機器類型前,請先查看適當大小建議。如要瞭解 Compute Engine 規模調整建議,請參閱「對 VM 執行個體套用機器類型建議」。

最佳做法

以下提供幾項最佳做法,協助您順利變更 VM 機型。

  • 使用快照定期備份永久磁碟資料。 請考慮在變更機器類型前,為您的永久磁碟資料建立快照。如要確保新機器類型能夠支援現有 VM 上的資料,您可以先建立永久磁碟的快照,然後透過這份快照另外啟動一個採用新機器類型的 VM,藉此確認該 VM 是否能順利啟動。

  • 將其他磁碟新增至 /etc/fstab 檔案。 如果 VM 連結了其他磁碟,請確認您已將這些磁碟新增至 /etc/fstab 檔案,讓系統能在 VM 重新啟動時自動掛接這些磁碟。

  • 先建立預留項目,再變更機器類型。為避免發生資源可用性相關錯誤,請在新機型推出時建立 Compute Engine 預留項目,在可用區中預留這些機型。預留資源可確保資源在您需要時可用。

如要建立預訂,請完成下列步驟:

  1. 建立保留項目 (或找出現有保留項目),並確保屬性與預計使用的 VM 相同。預留的 VM 數量必須大於或等於要變更的 VM 數量。如要防止其他 VM 使用這個預留項目,請使用 specificReservationRequired 選項。

  2. 確認預計使用的 VM 能否使用預留項目:

    1. 確認 VM 是否具有正確的預留項目關聯性
    2. 變更 VM 前,請確認預留項目有足夠的容量

變更機型

您只能變更已停止 VM 的機器類型。系統只會將 TERMINATED 狀態的 VM 視為已停止。您無法變更執行中 VM 的機器類型。

如要將機器類型升級至最新一代,請先參閱「評估 VM 遷移選項」。

主控台

  1. 前往 Trusted Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 在「Name」(名稱) 欄中,按一下要變更機器類型的 VM 名稱。

  3. 在「VM 執行個體詳細資料」頁面上,完成下列步驟:

    1. 如果 VM 正在執行,請按一下「停止」停止 VM。如果沒有「停止」選項,請依序點選 「更多動作」>「停止」
    2. 如要編輯 VM,請按一下 「編輯」
    3. 在「Machine configuration」(機器設定) 區段中,選取要使用的機器類型,或建立自訂機器類型

    4. 若要儲存變更,請按一下 [儲存]

    5. 重新啟動 VM。

gcloud

  1. 使用 gcloud compute instances stop 指令停止 VM:

    gcloud compute instances stop VM_NAME
    

    VM_NAME 替換為要變更機器類型的 VM。

  2. 使用 gcloud compute instances set-machine-type 指令變更機器類型:

    gcloud compute instances set-machine-type VM_NAME \
        --machine-type NEW_MACHINE_TYPE
    

    NEW_MACHINE_TYPE 替換為 VM 的新機器類型。機器類型可以是下列任一類型:

  3. 使用 gcloud compute instances start 指令啟動 VM:

    gcloud compute instances start VM_NAME
    

    VM_NAME 替換為您變更的 VM 名稱。

Java

Java

在試用這個範例之前,請先按照Java使用用戶端程式庫的 Compute Engine 快速入門」中的操作說明進行設定。詳情請參閱 Compute Engine Java API 參考說明文件

如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr


import com.google.cloud.compute.v1.Instance;
import com.google.cloud.compute.v1.Instance.Status;
import com.google.cloud.compute.v1.InstancesClient;
import com.google.cloud.compute.v1.InstancesSetMachineTypeRequest;
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ChangeInstanceMachineType {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "your-project-id";
    // Name of the zone your instance belongs to.
    String zone = "zone-name";
    // Name of the VM you want to modify.
    String instanceName = "instance-name";
    // The new machine type you want to use for the VM.
    // For example: "e2-standard-8", "e2-custom-4-2048" or "m1-ultramem-40"
    // More about machine types: https://cloud.google.com/compute/docs/machine-resource
    String newMachineType = "e2-standard-8";
    changeMachineType(projectId, zone, instanceName, newMachineType);
  }

  // Changes the machine type of VM.
  // The VM needs to be in the 'TERMINATED' state for this operation to be successful.
  public static void changeMachineType(String projectId, String zone, String instanceName,
      String newMachineType)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `instancesClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (InstancesClient instancesClient = InstancesClient.create()) {

      Instance instance = instancesClient.get(projectId, zone, instanceName);
      if (!instance.getStatus().equals(Status.TERMINATED.name())) {
        throw new Error(String.format(
            "Only machines in TERMINATED state can have their machine type changed. "
                + "%s is in %s state.", instance.getName(), instance.getStatus()));
      }

      InstancesSetMachineTypeRequest machineTypeRequest =
          InstancesSetMachineTypeRequest.newBuilder()
              .setMachineType(String.format("projects/%s/zones/%s/machineTypes/%s",
                  projectId, zone, newMachineType))
              .build();

      Operation response = instancesClient
          .setMachineTypeAsync(projectId, zone, instanceName, machineTypeRequest)
          .get(3, TimeUnit.MINUTES);

      if (response.hasError()) {
        System.out.println("Machine type update failed! " + response);
        return;
      }
      System.out.println("Machine type update - operation status: " + response.getStatus());
    }
  }
}

Python

Python

在試用這個範例之前,請先按照Python使用用戶端程式庫的 Compute Engine 快速入門」中的操作說明進行設定。詳情請參閱 Compute Engine Python API 參考說明文件

如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

from __future__ import annotations

import sys
from typing import Any

from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1


def wait_for_extended_operation(
    operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
    """
    Waits for the extended (long-running) operation to complete.

    If the operation is successful, it will return its result.
    If the operation ends with an error, an exception will be raised.
    If there were any warnings during the execution of the operation
    they will be printed to sys.stderr.

    Args:
        operation: a long-running operation you want to wait on.
        verbose_name: (optional) a more verbose name of the operation,
            used only during error and warning reporting.
        timeout: how long (in seconds) to wait for operation to finish.
            If None, wait indefinitely.

    Returns:
        Whatever the operation.result() returns.

    Raises:
        This method will raise the exception received from `operation.exception()`
        or RuntimeError if there is no exception set, but there is an `error_code`
        set for the `operation`.

        In case of an operation taking longer than `timeout` seconds to complete,
        a `concurrent.futures.TimeoutError` will be raised.
    """
    result = operation.result(timeout=timeout)

    if operation.error_code:
        print(
            f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
            file=sys.stderr,
            flush=True,
        )
        print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
        raise operation.exception() or RuntimeError(operation.error_message)

    if operation.warnings:
        print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
        for warning in operation.warnings:
            print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

    return result


def change_machine_type(
    project_id: str, zone: str, instance_name: str, new_machine_type: str
) -> None:
    """
    Changes the machine type of VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        zone: name of the zone your instance belongs to.
        instance_name: name of the VM you want to modify.
        new_machine_type: the new machine type you want to use for the VM.
            For example: `e2-standard-8`, `e2-custom-4-2048` or `m1-ultramem-40`
            More about machine types: https://cloud.google.com/compute/docs/machine-resource
    """
    client = compute_v1.InstancesClient()
    instance = client.get(project=project_id, zone=zone, instance=instance_name)

    if instance.status != compute_v1.Instance.Status.TERMINATED.name:
        raise RuntimeError(
            f"Only machines in TERMINATED state can have their machine type changed. "
            f"{instance.name} is in {instance.status}({instance.status_message}) state."
        )

    machine_type = compute_v1.InstancesSetMachineTypeRequest()
    machine_type.machine_type = (
        f"projects/{project_id}/zones/{zone}/machineTypes/{new_machine_type}"
    )
    operation = client.set_machine_type(
        project=project_id,
        zone=zone,
        instance=instance_name,
        instances_set_machine_type_request_resource=machine_type,
    )

    wait_for_extended_operation(operation, "changing machine type")

REST

  1. 使用 instances.stop 方法停止 VM:

    POST https://compute.s3nsapis.fr/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop
    

    更改下列內容:

    • PROJECT_ID:專案 ID

    • ZONE:包含 VM 的可用區

    • VM_NAME:要變更機器類型的 VM

  2. 使用 instances.setMachineType 方法變更機器類型:

    POST https://compute.s3nsapis.fr/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMachineType
    

    在要求主體中提供更新後的 machineType

    {
        machineType: "zones/MACHINE_TYPE_ZONE/machineTypes/NEW_MACHINE_TYPE"
    }
    

    更改下列內容:

    • MACHINE_TYPE_ZONE:包含機器類型的區域

    • NEW_MACHINE_TYPE:VM 的新機器類型

      機器類型可以是下列任一類型:

  3. 使用 instances.start 方法啟動 VM:

    POST https://compute.s3nsapis.fr/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/start
    

    更改下列內容:

    • PROJECT_ID:專案 ID
    • ZONE:包含 VM 的可用區
    • VM_NAME:您變更的 VM 名稱

後續步驟