Google Cloud リソースをデプロイとして管理する

Google Cloud Deployment Manager を使用して、一連の Google Cloud リソースを作成し、デプロイと呼ばれる 1 つの単位として管理できます。たとえば、チームの開発環境に 2 台の仮想マシン(VM)と BigQuery データベースが必要な場合、これらのリソースを構成ファイルに定義し、Deployment Manager でリソースの作成、変更または削除を行うことができます。構成ファイルをチームのコード リポジトリの一部にすると、誰でも同じ環境を作成して同じ結果を実現できるようになります。

このチュートリアルでは、Google Cloud Deployment Manager を使用して仮想マシン(VM)インスタンスを作成します。基本構成ファイルに VM を定義し、その構成ファイルを使用してデプロイを作成します。

このチュートリアルでは、Linux、macOS または Windows ターミナルでコマンドを実行します。チュートリアルを始める前に、これらの操作に慣れておく必要があります。

Cloud Shell では、このチュートリアルのインタラクティブ バージョンを使用できます。このバージョンでは、サンプル構成を編集し、ワークステーションにインストールすることなくリソースをデプロイできます。自分のパソコン上でチュートリアルを実行する場合は、次のセクションに進んでください。

Cloud Shell のクイックスタート

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  3. Google Cloud プロジェクトで課金が有効になっていることを確認します

  4. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  5. Google Cloud プロジェクトで課金が有効になっていることを確認します

  6. Enable the Deployment Manager and Compute Engine APIs.

    Enable the APIs

  7. ワークステーションに、Google Cloud CLI をインストールします。
  8. プロジェクトを使用するように Google Cloud CLI を構成します。次のコマンドを実行します。[MY_PROJECT] はプロジェクト ID で置き換えます。
    gcloud config set project [MY_PROJECT]

リソースを定義する

構成ファイルに YAML 構文でリソースを記述します。

  1. 下のサンプルをコピーして、テキスト エディタに貼り付けます。

    # Copyright 2016 Google Inc. All rights reserved.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    
    # Put all your resources under `resources:`. For each resource, you need:
    # - The type of resource. In this example, the type is a Compute VM instance.
    # - An internal name for the resource.
    # - The properties for the resource. In this example, for VM instances, you add
    #   the machine type, a boot disk, network information, and so on.
    #
    # For a list of supported resources,
    # see https://cloud.google.com/deployment-manager/docs/configuration/supported-resource-types.
    resources:
    - type: compute.v1.instance
      name: quickstart-deployment-vm
      properties:
        # The properties of the resource depend on the type of resource. For a list
        # of properties, see the API reference for the resource.
        zone: us-central1-f
        # Replace [MY_PROJECT] with your project ID
        machineType: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/zones/us-central1-f/machineTypes/f1-micro
        disks:
        - deviceName: boot
          type: PERSISTENT
          boot: true
          autoDelete: true
          initializeParams:
            # See a full list of image families at https://cloud.google.com/compute/docs/images#os-compute-support
            # The format of the sourceImage URL is: https://www.googleapis.com/compute/v1/projects/[IMAGE_PROJECT]/global/images/family/[FAMILY_NAME]
            sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-11
        # Replace [MY_PROJECT] with your project ID
        networkInterfaces:
        - network: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/global/networks/default
          # Access Config required to give the instance a public IP address
          accessConfigs:
          - name: External NAT
            type: ONE_TO_ONE_NAT
    

    この基本的な構成ファイルでは、以下のプロパティを持つ仮想マシンのインスタンス 1 つを含むデプロイを記述しています。

    • マシンタイプ: f1-micro
    • イメージ ファミリー: debian-9
    • ゾーン: us-central1-f
    • ルート永続ディスク: boot
    • ランダムに割り当てられた外部 IP アドレス
  2. [MY_PROJECT] のすべてのインスタンスをプロジェクト ID に置き換えます。

  3. ファイルを vm.yaml として保存します。

リソースをデプロイする

リソースをデプロイするには、Google Cloud CLI を使用し、構成ファイルを使用して新しいデプロイを作成します。

gcloud deployment-manager deployments create quickstart-deployment --config vm.yaml

デプロイが成功すると、次のようなメッセージが表示されます。

Create operation operation-1432319707382-516afeb5d00f1-b864f0e7-b7103978 completed successfully.
NAME                    TYPE                STATE      ERRORS
quickstart-deployment   compute.v1.instance COMPLETED  -

これで最初のデプロイが完了しました。

新しいデプロイを確認する

デプロイの状態を確認するには、次のコマンドを実行します。

gcloud deployment-manager deployments describe quickstart-deployment

開始時刻と終了時刻、作成されたリソース、警告またはエラーなど、デプロイの説明が表示されます。

fingerprint: xmVVeTtPq-5rr8F-vWFlrg==
id: '54660732508021769'
insertTime: '2016-03-09T04:45:26.032-08:00'
manifest: https://www.googleapis.com/deploymentmanager/v2/projects/myproject/global/deployments/my-first-deployment/manifests/manifest-1457527526037
name: quickstart-deployment
operation:
  endTime: '2016-03-09T04:46:19.480-08:00'
  id: '8993923014899639305'
  kind: deploymentmanager#operation
  name: operation-1457527525951-52d9d126f4618-f1ca6e72-3404bd3b
  operationType: insert
  progress: 100
  startTime: '2016-03-09T04:45:27.275-08:00'
  status: DONE
...
resources:
NAME                     TYPE                 STATE      ERRORS
quickstart-deployment-vm  compute.v1.instance  COMPLETED  -

リソースを確認する

デプロイを作成したら、Google Cloud コンソールでリソースを確認します。

  1. デプロイのリストを表示するには、Deployment Manager ページを開きます。

    Deployment Manager に移動

  2. デプロイのリソースを表示するには、quickstart-deployment をクリックします。デプロイの概要が表示されます。デプロイの情報とデプロイに含まれるリソースが表示されます。

  3. VM に関する情報を表示するには、quickstart-deployment-vm をクリックします。

クリーンアップ

このページで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、次の手順を行います。

gcloud deployment-manager deployments delete quickstart-deployment

プロンプトで y と入力します。

The following deployments will be deleted:
- quickstart-deployment

Do you want to continue (y/N)?

作成したデプロイとリソースが完全に削除されます。

次のステップ