プリエンプティブル VM を使用する MIG を作成する

このドキュメントでは、プリエンプティブル仮想マシン(VM)インスタンスを使用するマネージド インスタンス グループ(MIG)を作成する方法について説明します。プリエンプティブル VM は、ワークロードの中断を許容でき、プリエンプティブル VM による費用削減のメリットを活用する必要がある場合に便利です。

MIG を作成するための基本的なシナリオもご覧ください。

始める前に

  • まだ設定していない場合は、認証を設定します。認証とは、 Trusted Cloud by S3NS サービスと API にアクセスするために ID を確認するプロセスです。ローカル開発環境からコードまたはサンプルを実行するには、次のいずれかのオプションを選択して 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.

    gcloud

      1. After installing the Google Cloud CLI, sign in to the gcloud CLI with your federated identity and then initialize it by running the following command:

        gcloud init
      2. Set a default region and zone.
      3. Terraform

        ローカル開発環境でこのページの Terraform サンプルを使用するには、gcloud CLI をインストールして初期化し、ユーザー認証情報を使用してアプリケーションのデフォルト認証情報を設定します。

        1. Install the Google Cloud CLI.

        2. Configure the gcloud CLI to use your federated identity.

          For more information, see Sign in to the gcloud CLI with your federated identity.

        3. To initialize the gcloud CLI, run the following command:

          gcloud init
        4. 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 に指定した認証情報を使用します。

          After installing the Google Cloud CLI, sign in to the gcloud CLI with your federated identity and then initialize it by running the following command:

          gcloud init

        詳細については、 Trusted Cloud 認証ドキュメントの REST を使用して認証するをご覧ください。

制限事項

使用する構成に応じて変動する MIG の制限事項に関する全リストについては、MIG の制限事項をご覧ください。

プリエンプティブル VM が含まれるインスタンス テンプレートを作成する

ゾーンまたはリージョン MIG を使用して複数のプリエンプティブル VM をすばやく作成し、マネージド インスタンス グループ内の VM の費用を削減できます。たとえば、プリエンプティブル VM のグループを作成して、それを使用してバッチ処理タスクを実行し、タスクが完了したらそのグループを削除できます。

プリエンプティブル VM のグループを作成するには、インスタンス テンプレートでプリエンプティブル オプションを設定し、そのテンプレートを使用して MIG を作成します。

コンソール

  1. コンソールで、[インスタンス テンプレート] ページに移動します。

    [インスタンス テンプレート] に移動

    残りの手順は、 Trusted Cloud コンソールに表示されます。

  2. [インスタンス テンプレートを作成] をクリックします。
  3. インスタンス テンプレートに必要なプロパティを入力します。
  4. [詳細オプション] をクリックして、[管理] セクションを展開します。
  5. [可用性ポリシー] の [VM プロビジョニング モデル] リストで、[Spot] を選択します。
  6. [作成] をクリックしてテンプレートを作成します。

gcloud

instance-templates create コマンドを使用してインスタンス テンプレートを作成します。フラグ --preemptible を追加します。

gcloud compute instance-templates create INSTANCE_TEMPLATE \
    --preemptible

Terraform

次の例では、グローバル インスタンス テンプレートを作成します。プリエンプティブル オプションを指定するには、scheduling ブロックを含めます。サンプルで使用されているリソースの詳細については、google_compute_instance_template リソースをご覧ください。リージョン インスタンス テンプレートを作成するには、google_compute_region_instance_template リソースを使用します。

resource "google_compute_instance_template" "default" {
  name         = "preemptible-template"
  machine_type = "n1-standard-1"
  disk {
    source_image = "debian-cloud/debian-11"
  }
  network_interface {
    network = "default"
  }
  scheduling {
    preemptible       = "true"
    automatic_restart = "false"
  }
}

Terraform 構成を適用または削除する方法については、基本的な Terraform コマンドをご覧ください。

REST

instanceTemplates.insert メソッドを呼び出して新しいインスタンス テンプレートを作成します。scheduling.preemptible プロパティを追加して、true に設定します。

{
"name": "INSTANCE_TEMPLATE",
"properties": {
  "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",
  "networkInterfaces": [
    {
      "network": "global/networks/default",
      "accessConfigs":
      [
        {
          "name": "external-IP",
          "type": "ONE_TO_ONE_NAT"
        }
      ]
    }
  ],
  "scheduling":
  {
    "preemptible": true
  },
  "disks":
  [
    {
      "type": "PERSISTENT",
      "boot": true,
      "mode": "READ_WRITE",
      "initializeParams":
      {
        "sourceImage": "projects/debian-cloud/global/images/family/debian-9"
      }
    }
  ]
  }
}

インスタンス テンプレートを作成したら、それを使用して VM を単一ゾーンに固定または VM をリージョン内の複数のゾーンに分散した MIG を作成します。

次のステップ