GCPインスタンスを一時停止、再開するGCPクラウド機能

okwaves2024-01-25  11

GCP クラウド機能を使用して GCP インスタンスを開始および停止できますが、クラウド機能とスケジューラを使用して GCP インスタンスのスケジュールされた一時停止と再開に取り組む必要があります。

GCP ドキュメントから、以下で利用可能なクラウド機能を使用して開始と停止を実行できることがわかりました。 https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/functions/scheduleinstance

GCP インスタンスの一時停止と再開に使用できる同じノード JS または他の言語 Pcgk はありますか?

そうでない場合は、一時停止/再開用に独自のものを作成できます。

試してみたところ、以下のエラーが発生しました "TypeError: compute.zone(...).vm(...).resume は関数ではありません

編集、Chris と Guillaume に感謝します。リンクをたどった後、コードを編集しました。以下が私のindex.js ファイルです。 ソムのために私がそうするときの理由 gcloud 関数のデプロイ、resumeInstancePubSub --trigger-topic、resume-instance --runtime、nodejs10 --allow-unauthenticated

いつも手に入れます 関数「resumeInstancePubSub1」は、提供されたモジュールで定義されていません。 resumeInstancePubSub1 2020-09-04 10:57:00.333 実行する正しいターゲット関数を指定しましたか?

私はこれまで Node JS や JS で作業したことがありませんでした。git リポジトリの下で使用して簡単に動作させることができる、開始/停止ドキュメントのようなものを期待していました。 https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

私のindex.jsファイル

// BEFORE RUNNING:
// ---------------
// 1. If not already done, enable the Compute Engine API
//    and check the quota for your project at
//    https://console.developers.google.com/apis/api/compute
// 2. This sample uses Application Default Credentials for authentication.
//    If not already done, install the gcloud CLI from
//    https://cloud.google.com/sdk and run
//    `gcloud beta auth application-default login`.
//    For more information, see
//    https://developers.google.com/identity/protocols/application-default-credentials
// 3. Install the Node.js client library by running
//    `npm install googleapis --save`

const {google} = require('googleapis');
var compute = google.compute('beta');

authorize(function(authClient) {
  var request = {
    // Project ID for this request.
    project: 'my-project',  // TODO: Update placeholder value.

    // The name of the zone for this request.
    zone: 'my-zone',  // TODO: Update placeholder value.

    // Name of the instance resource to resume.
    instance: 'my-instance',  // TODO: Update placeholder value.

    resource: {
      // TODO: Add desired properties to the request body.
    },

    auth: authClient,
  };
exports.resumeInstancePubSub = async (event, context, callback) => {
try {
    const payload = _validatePayload(
      JSON.parse(Buffer.from(event.data, 'base64').toString())
    );
    const options = {filter: `labels.${payload.label}`};
    const [vms] = await compute.getVMs(options);
    await Promise.all(
      vms.map(async (instance) => {
        if (payload.zone === instance.zone.id) {
          const [operation] = await compute
            .zone(payload.zone)
            .vm(instance.name)
            .resume();

          // Operation pending
          return operation.promise();
        }
      })
    );
          // Operation complete. Instance successfully started.
    const message = `Successfully started instance(s)`;
    console.log(message);
    callback(null, message);
  } catch (err) {
    console.log(err);
    callback(err);
  }
};
 compute.instances.resume(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

    // TODO: Change code below to process the `response` object:
    console.log(JSON.stringify(response, null, 2));
  });
});

function authorize(callback) {
  google.auth.getClient({
    scopes: ['https://www.googleapis.com/auth/cloud-platform']
  }).then(client => {
    callback(client);
  }).catch(err => {
    console.error('authentication failed: ', err);
  });
}

これはベータ機能であり、クライアント ライブラリにはまだ実装されていないことに注意してください。ただし、検出 API を使用する場合は、これらを使用できるはずです。現在のコードを共有してもらえますか?解決してみます!

– ギヨーム・ブラキエール

2020 年 9 月 3 日 20:02

@guillaumeblaquiere 上記にindex.jsを追加しました。ありがとう...!

– サウラブ・ウマテ

2020 年 9 月 4 日 11:58

上記のエラーは現在解決されています。関数は別のモジュール内にあるためアクセスできませんでした。

– サウラブ・ウマテ

2020 年 9 月 4 日 17:44



------------------------

こことここに、API の新しいベータ版のドキュメントがあります。次のようにインスタンスを一時停止できることがわかります。

compute.instances.suspend(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

同様の方法でインスタンスを再開できます。

 compute.instances.resume(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

3

正しい場所に表示されません。新しいベータ API:cloud.google.com/compute/docs/reference/rest/beta/instances/…および、cloud.google.com/compute/docs/reference/rest/beta/instances/…

– ギヨーム・ブラキエール

2020 年 9 月 3 日 20:00

ありがとう!そのベータ版については知りませんでした。回答を編集しています

– クリス32

2020 年 9 月 3 日t 21:47

ギョームとクリス、ありがとう。あなたのリンクは本当に役に立ちました。

– サウラブ・ウマテ

2020 年 9 月 4 日 11:57



------------------------

GCP は最近「スケジュールの作成」を追加しました。構成されたスケジュールに基づいて VM インスタンスを起動および停止する機能。 詳細については、

をご覧ください。

https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop#managing_instance_schedules

新着記事:
C# - ASP.NET Core API コントローラーでの奇妙な CultureNotFoundExceptionPython – 文字列に数字が含まれているかどうかをチェックするエラーチェッカーを作成しようとしていますが、うまくいきませんMySql バックエンド データベース コードを使用した C# は VS 2019 で正常に動作しますが、コンパイル後、最初のクエリで実行可能ファイルが中断します。java - Spring StateMachineの再利用可能なステートマシンインスタンスselenium - RobotFramework を使用してインライン JS スクリプト タグから値を抽出するパフォーマンス テスト - AWS RDS MySQL インスタンスは、特定の負荷の下で同じ SQL SELECT クエリの実行を開始する速度が大幅に遅くなります。javascript - HtmlAgilityPack jsによって追加されるdivを取得するにはどうすればよいですか?servicenow - この評価で予期しないエラーが発生しました。無効なタイプが指定されました画像処理 - x_train 変数と y_train 変数でデータを取り込むニューラル ネットワークで Python ジェネレーターを使用する方法配列 - JavaScript でスタックを使用したプレフィックス計算機bitmap - プログラムで textview を imageview Android に配置しますselenium webdriver - Gradle を使用して Cucumber エクステント レポート 4 を生成する方法
総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。