このページでは、アイドル状態の VM に関する推奨事項を使用して、アイドル状態の VM インスタンスを特定して停止することでリソースの無駄を減らし、プロジェクトのコンピューティング料金を削減する方法について説明します。
- Compute Engine がアイドル状態の VM の推奨事項を生成する方法の詳細については、アイドル状態の VM インスタンスの検出方法をご覧ください。
- アイドル状態の VM Recommender を構成して、受け取る推奨事項の数を増減する方法については、アイドル状態の VM に対する推奨事項を構成するをご覧ください。
始める前に
- 制限事項を参照して、VM がアイドル状態の VM の推奨事項をサポートしているかどうかを確認します。
-
まだ設定していない場合は、認証を設定します。認証とは、 Google Cloud サービスと API にアクセスするために ID を確認するプロセスです。ローカル開発環境からコードまたはサンプルを実行するには、次のいずれかのオプションを選択して Compute Engine に対する認証を行います。
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
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.
- Set a default region and zone.
PROJECT_ID
: オブジェクトの IDZONE
: 推奨を一覧表示するインスタンスを含むゾーンoperationGroups
: 推奨を適用する際に順番に実行できるオペレーションのグループdescription
: 人が読める形式での推奨の説明- google.compute.instance.IdleResourceRecommender
PROJECT_ID
: オブジェクトの ID。ZONE
: 推奨を一覧表示するインスタンスを含むゾーン。VM の現在の
status
を検証するテスト オペレーション。例:{ "action": "test", "resourceType": "compute.googleapis.com/Instance", "resource" : "//compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name", "path": "/status", "value": "RUNNING" }
VM の
status
を変更する置換オペレーション。例:{ "action": "replace", "resourceType": "compute.googleapis.com/Instance", "resource" : "//compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name", "path": "/status", "value": "TERMINATED" }
- Google Cloud コンソールで、[VM インスタンス] ページに移動します。
- 停止するインスタンスを選択します。
- [停止] をクリックして VM を停止します。[停止] オプションがない場合は、 [その他の操作] > [停止] をクリックします。
VM_NAME
: 停止する VM インスタンスの名前ZONE
: 停止するインスタンスを含むゾーンPROJECT_ID
: オブジェクトの IDZONE
: 停止するインスタンスを含むゾーンVM_NAME
: 停止する VM インスタンスの名前- VM インスタンスの停止、リセット、削除がインスタンスとそのリソースに与える影響について学習する。
- アイドル状態の VM に関する推奨事項を構成する方法を確認する。
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 認証ドキュメントの REST を使用して認証するをご覧ください。
料金
アイドル状態の VM の推奨事項は無料で利用できます。推奨事項を使用してリソース使用量を減らすと、費用を節約できます。
アイドル状態の VM インスタンスに関する推奨の表示
アイドル状態の VM に関する推奨事項を表示するには、gcloud CLI または REST を使用します。
gcloud
--recommender=google.compute.instance.IdleResourceRecommender
でgcloud recommender recommendations list
コマンドを使用します。gcloud recommender recommendations list \ --project=PROJECT_ID \ --location=ZONE \ --recommender=google.compute.instance.IdleResourceRecommender \ --format=yaml
次のように置き換えます。
例:
gcloud recommender recommendations list \ --project=my-project \ --location=us-central1-c \ --recommender=google.compute.instance.IdleResourceRecommender \ --format=yaml
そのロケーションにアイドル状態の VM がない場合、レスポンスは空になります。それ以外の場合、レスポンスには推奨ごとに次のフィールドが含まれます。
--- content: ... operationGroups: - operations: - action: test path: /status resource: //compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name resourceType: compute.googleapis.com/Instance value: RUNNING - action: replace path: /status resource: //compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name resourceType: compute.googleapis.com/Instance value: TERMINATED description: Save cost by stopping Idle VM 'vm-name'. etag: '"83da314c23f634e1"' lastRefreshTime: '2020-02-24T07:56:40Z' name: projects/141732092341/locations/us-central1-c/recommenders/google.compute.instance.IdleResourceRecommender/recommendations/0e061a3a-f921-4216-b1b4-62e16942cd1a primaryImpact: category: COST costProjection: cost: currencyCode: USD nanos: -91533961 units: '-262' duration: 2592000s recommenderSubtype: STOP_VM stateInfo: state: ACTIVE
gcloud を使用した推奨の詳細については、gcloud の例をご覧ください。
REST
recommendations.list
メソッドを呼び出し、次の推奨タイプを使用します。API 呼び出しは次のようになります。
GET https://recommender.googleapis.com/v1/projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/recommendations
次のように置き換えます。
次の例は、
curl
を使用してリクエストを送信する方法と、関連するサンプル レスポンスを示しています。PROJECT_ID=my-project ZONE=us-central1-c RECOMMENDER_ID=google.compute.instance.IdleResourceRecommender curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "x-goog-user-project: $PROJECT_ID" \ https://recommender.googleapis.com/v1/projects/$PROJECT_ID/locations/$ZONE/recommenders/$RECOMMENDER_ID/recommendations
アイドル状態の VM の推奨に対する JSON レスポンスの例:
{ "description" : "Save cost by stopping Idle VM `vm-name`", "name": "projects/1574864402/locations/us-central1-c/recommenders/" "google.compute.instance.IdleResourceRecommender/" "recommendations/0fd31b24-cc05-4132-8431-ed54a22dd4f1", "lastRefreshTime": { "seconds": 1543912652 }, "primaryImpact": { "category": COST, "costProjection": { "cost": {"currencyCode": "USD", "units": -50}, "duration": { "seconds": 2592000 } } }, "stateInfo": ACTIVE, "content": "groups" : [ { "operations" : [ { "action": "test", "resourceType": "compute.googleapis.com/Instance", "resource": "//compute.googleapis.com/projects/my-project/" "zones/us-central1-c/instances/vm-name", "path": "/status", "value": "RUNNING" }, { "action": "replace", "resourceType": "compute.googleapis.com/Instance", "resource": "//compute.googleapis.com/projects/my-project/" "zones/us-central1-c/instances/vm-name", "path": "/status", "value": "TERMINATED" } ] } ] }, "etag" : "cb0e6ac2cfc0b591" }
各フィールドの詳細については、Recommender API のドキュメントをご覧ください。
推奨のレスポンスの解釈
gcloud CLI または REST を介して受信する各推奨には、推奨を適用するために連続して実行できるオペレーション グループが含まれています。アイドル状態の VM の推奨のオペレーション グループには、次の 2 つのオペレーションが含まれます。
最初のオペレーションは
test
です。つまり、resource
がまだRUNNING
であることをテストする必要があります。これを行うには、VM の状態を確認します。2 番目のオペレーションである
replace
は、リソースのstatus
を新しい値TERMINATED
に置き換えることを意味します。これを行うには、VM を停止します。これについては以下で説明します。アイドル状態の VM の推奨の適用
アイドル状態の VM の推奨を受け取り、インスタンスが不要になった場合は、 Google Cloud console、gcloud CLI、または REST を使用して、インスタンスを停止し、必要に応じて削除します。
インスタンスとそのディスクを停止しても削除しない場合は、そのディスクの料金が発生します。
コンソール
gcloud
instances stop
コマンドを使用して、停止するVM_NAME
を指定します。gcloud compute instances stop VM_NAME --zone=ZONE
次のように置き換えます。
REST
インスタンスを停止する
POST
リクエストを作成します。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop
次のように置き換えます。
VM インスタンスとそのディスクを削除しても問題がない場合は、インスタンスを削除できます。
次のステップ
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-11 UTC。
-