Terraform で GCP のリソースを管理する(GKE編)
前回の続きで、GKEのコードを載せる。
modules/gke_cluster/main.tf
resource "google_container_cluster" "cluster" { name = "${lookup(var.cluster, "name")}" region = "${lookup(var.cluster, "region")}" additional_zones = [ "${lookup(var.cluster, "region")}-a", "${lookup(var.cluster, "region")}-b", ] min_master_version = "${lookup(var.cluster, "min_master_version")}" logging_service = "logging.googleapis.com/kubernetes" monitoring_service = "monitoring.googleapis.com/kubernetes" initial_node_count = "${lookup(var.cluster, "initial_node_count")}" remove_default_node_pool = true # FIXME: IPエイリアスの有効については対応待ち # @see https://github.com/terraform-providers/terraform-provider-google/issues/1597 lifecycle { ignore_changes = [ "node_pool", "ip_allocation_policy", ] } }
modules/gke_cluster/variables.tf
variable "cluster" { default = {} }
modules/gke_node_pool/main.tf
resource "google_container_node_pool" "node_pool" { name = "${lookup(var.node_pool, "name")}" region = "${lookup(var.cluster, "region")}" cluster = "${lookup(var.cluster, "name")}" initial_node_count = "${lookup(var.node_pool, "initial_node_count", "${lookup(var.cluster, "initial_node_count")}")}" autoscaling { min_node_count = "${lookup(var.node_pool, "autoscaling_min_node_count", 1)}" max_node_count = "${lookup(var.node_pool, "autoscaling_max_node_count", 3)}" } management { auto_repair = true auto_upgrade = true } node_config = { machine_type = "${lookup(var.node_pool, "node_config_machine_type", "n1-standard-1")}" oauth_scopes = "${split(",", lookup(var.node_pool, "node_config_oauth_scopes", "compute-rw,storage-ro,logging-write,monitoring"))}" } }
modules/gke_node_pool/variables.tf
variable "cluster" { default = {} } variable "node_pool" { default = {} }
main.tf
variable "cluster" { default = { project = "<プロジェクト名>" name = "<クラスタ名>" min_master_version = "1.10.5-gke.0" initial_node_count = 1 # TODO: Cloud Memorystore が東京リージョンに対応したら変更する region = "us-west1" } } variable "default_node_pool" { default = { name = "default-node-pool" initial_node_count = 1 autoscaling_min_node_count = 1 autoscaling_max_node_count = 3 node_config_machine_type = "n1-standard-1" node_config_oauth_scopes = "https://www.googleapis.com/auth/cloud_debugger,https://www.googleapis.com/auth/compute,https://www.googleapis.com/auth/devstorage.read_write,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/service.management,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/trace.append" } } module "cluster" { source = "./modules/gke_cluster" cluster = "${var.cluster}" } module "default_node_pool" { source = "./modules/gke_node_pool" cluster = "${var.cluster}" node_pool = "${var.default_node_pool}" }
google_container_cluster の node_config では、ノードプールの自動アップグレード・自動修復の設定ができなかったので調べた。
https://github.com/terraform-providers/terraform-provider-google/issues/475#issuecomment-342874888
とのことなので、google_container_cluster でデフォルトで作成されるノードプールを削除し、google_container_node_pool で新しくノードプールを作成するようにした。
また、現在 Terraform の ip_allocation_policy オプションでは secondary ranges の指定が必要だった。
クラスタ作成時に --enable-ip-aliases
でIPエイリアス有効にした場合は、secondary ranges の割り当てを自動で行なってくれている。
https://cloud.google.com/kubernetes-engine/docs/how-to/alias-ips#secondary_ranges
僕たちが subnet に secondary ranges を作成し、それを指定するようにすれば Terraform の ip_allocation_policy で IPエリアスを有効にできると思うが、こちらの issue が対応されるまで待とうと思う。
それまでは workaround を実行しようかと。