5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GCEでコンテナを動かす + Terraformで管理

Posted at

やりたいこと

コンテナをGoogle CloudのGCEで動かしたい。
ついでにTerraformで管理したい。

使うもの

  • Artifact Registry
  • GCE
  • Cloud Build(任意)

公式ドキュメント

この辺に書いてある。
https://cloud.google.com/compute/docs/containers/deploying-containers

費用

GCEのVMとネットワークとディスクの費用
https://cloud.google.com/compute/all-pricing
無料枠あり。
https://cloud.google.com/free/docs/gcp-free-tier/#compute

Artifact Registryにイメージを置いておく保存容量と下り(外向き)ネットワークの費用
https://cloud.google.com/artifact-registry/pricing
保存容量は0.5GBまで無料枠あり。
下り(外向き)ネットワークは移動範囲が狭ければ無料枠あり。
※いずれも2021年6月時点

外部IPが必要なら、その費用
https://cloud.google.com/compute/network-pricing/?hl=ja#ipaddress

やること

コンテナイメージをArtifact Registryで管理する

リポジトリ作成

main.tf
terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "3.70.0"
    }
    google-beta = {
      source  = "hashicorp/google-beta"
      version = "3.70.0"
    }
  }
}

resource "google_artifact_registry_repository" "my_repository" {
  provider      = google-beta
  location      = "asia"
  repository_id = "my-repository"
  description   = "example docker repository"
  format        = "DOCKER"
}

※2021年6月時点ではgoogle-betaのproviderを使う必要あり。
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/artifact_registry_repository

リポジトリにイメージを追加

Dockerfileを用意してgcloud builds submitを実行する。
docker tagdocker pushを使う方法でも可。

repository_nameは前述で指定したrepository_idに読み替える。
image_nameは任意の名前を指定する。

gcloud builds submit --tag asia-docker.pkg.dev/<project_id>/<repository_name>/<image_name> --project=<project_id>

VMを作ってコンテナを動かす

コンテナの定義はTerraform Google Container VM Metadata Moduleを使って記述し、
google_compute_instanceリソースで利用する。
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance

イメージの指定はタグも可。

ログとモニタリングはVMのメタデータで有効にする。
https://cloud.google.com/container-optimized-os/docs/how-to/logging?hl=ja#creating_a_new_instance_with_the_logging_agent_enabled
https://cloud.google.com/container-optimized-os/docs/how-to/monitoring?hl=ja#metadata

main.tf
module "gce-container" {
  source           = "terraform-google-modules/container-vm/google"
  version          = "~> 2.0"
  cos_image_family = "stable"
  restart_policy   = "Always"

  container = {
    image = "asia-docker.pkg.dev/<project_id>/<repository_name>/<image_name>"
  }
}

resource "google_compute_instance" "my_vm" {
  name         = "my-vm"
  machine_type = "f1-micro"
  zone         = "asia-northeast1-b"

  boot_disk {
    initialize_params {
      type  = "pd-standard"
      size  = 10
      image = module.gce-container.source_image
    }
  }

  network_interface {
    subnetwork = "default"
  }

  scheduling {
    preemptible         = false
    automatic_restart   = true
    on_host_maintenance = "MIGRATE"
  }

  service_account {
    scopes = ["logging-write", "monitoring-write", "trace-append", "storage-ro", "service-control", "service-management"]
  }

  metadata = {
    gce-container-declaration = module.gce-container.metadata_value
    google-logging-enabled    = "true"
    google-monitoring-enabled = "true"
  }
}

ログ

コンテナのログは以下の場所に出力される。

resource.type="gce_instance"
log_name="projects/<project_id>/logs/cos_containers"

モニタリング

COS上のコンテナの指標はcompute.googleapis.com/guestで取れる。

保守時の話

VM再起動時に再度イメージを取りに行くので、タグ無指定やlatest指定だとVM再起動により最新のイメージで動かせる。

結果の画面イメージ

Artifact Registry

リポジトリ

スクリーンショット 2021-06-24 15.07.28.png

イメージ

スクリーンショット 2021-06-24 15.07.36.png

イメージ詳細

スクリーンショット 2021-06-24 15.07.54.png

GCE

インスタンス一覧

スクリーンショット 2021-06-24 15.08.36.png

インスタンス詳細

console.cloud.google.com_compute_instancesDetail_zones_asia-northeast1-b_instances_my-vm_tab=details&hl=ja&organizationId=935288475598&project=sandbox-suzutatsu-gce&rif_reserved (3).png

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?