😊
GKE Cluster by Terraform
承知しました。ご要件に基づいてTerraformコードを作成いたします。まず、必要なファイル構成を示し、その後各ファイルの内容を説明します。
ファイル構成:
├── main.tf
├── variables.tf
├── outputs.tf
└── provider.tf
まず、provider.tf
から始めましょう:
provider.tf
terraform {
required_version = ">= 1.0"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.0"
}
google-beta = {
source = "hashicorp/google-beta"
version = "~> 4.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
}
provider "google-beta" {
project = var.project_id
region = var.region
}
variables.tf
:
variables.tf
variable "project_id" {
description = "GCPプロジェクトID"
type = string
}
variable "region" {
description = "プライマリリージョン"
type = string
default = "asia-northeast1"
}
variable "location" {
description = "クラスターのロケーション"
type = string
default = "asia-northeast1"
}
variable "network_name" {
description = "VPCネットワーク名"
type = string
default = "gke-network"
}
variable "subnet_name" {
description = "サブネット名"
type = string
default = "gke-subnet"
}
メインのGKEクラスター設定 main.tf
:
main.tf
# VPCネットワークの作成
resource "google_compute_network" "gke_network" {
name = var.network_name
auto_create_subnetworks = false
}
# サブネットの作成
resource "google_compute_subnetwork" "gke_subnet" {
name = var.subnet_name
network = google_compute_network.gke_network.self_link
region = var.region
ip_cidr_range = "10.0.0.0/20"
secondary_ip_range {
range_name = "pod-ranges"
ip_cidr_range = "10.48.0.0/14"
}
secondary_ip_range {
range_name = "service-ranges"
ip_cidr_range = "10.52.0.0/20"
}
}
# GKEクラスターの作成
resource "google_container_cluster" "primary" {
provider = google-beta
name = "xxxx-gke-cluster"
location = var.location
# リージョナルクラスター設定
network = google_compute_network.gke_network.self_link
subnetwork = google_compute_subnetwork.gke_subnet.self_link
# Enterpriseティア設定
release_channel {
channel = "STABLE"
}
# データプレーンV2の有効化
datapath_provider = "ADVANCED_DATAPATH"
# プライベートクラスター設定
private_cluster_config {
enable_private_nodes = true
enable_private_endpoint = false
master_ipv4_cidr_block = "172.16.0.0/28"
master_global_access_config {
enabled = true
}
}
# DNSの設定
dns_config {
cluster_dns = "CLOUD_DNS"
cluster_dns_scope = "VPC_SCOPE"
cluster_dns_domain = "cluster.local"
}
# メンテナンスウィンドウの設定
maintenance_policy {
recurring_window {
start_time = "2023-01-01T17:00:00Z" # JST 02:00
end_time = "2023-01-01T21:00:00Z" # JST 06:00
recurrence = "FREQ=WEEKLY;BYDAY=SA,SU"
}
}
# セキュリティ設定
security_posture_config {
mode = "BASIC"
}
workload_vulnerability_scanning_config {
mode = "STANDARD"
}
# サービスメッシュの有効化
mesh_certificates {}
# Backup設定
backup_config {
enabled = true
location = "asia-northeast2" # 大阪リージョン
}
# Secretの暗号化設定
database_encryption {
state = "ENCRYPTED"
key_name = "projects/${var.project_id}/locations/global/keyRings/my-ring/cryptoKeys/my-key"
}
# Workload Identity設定
workload_identity_config {
workload_pool = "${var.project_id}.svc.id.goog"
}
# RBACのGoogle Groups設定
authenticator_groups_config {
security_group = "gke-security-groups@yourdomain.com"
}
# モニタリング設定
monitoring_config {
enable_components = ["SYSTEM_COMPONENTS", "WORKLOADS"]
managed_prometheus {
enabled = true
}
}
# ノードプールの設定
node_config {
# アクセススコープの設定
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/devstorage.read_write",
"https://www.googleapis.com/auth/taskqueue",
"https://www.googleapis.com/auth/sqlservice.admin",
"https://www.googleapis.com/auth/datastore",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
"https://www.googleapis.com/auth/pubsub",
"https://www.googleapis.com/auth/service.control",
"https://www.googleapis.com/auth/servicemanagement",
"https://www.googleapis.com/auth/trace.append",
"https://www.googleapis.com/auth/source.read_write"
]
# ブートディスクの暗号化設定
boot_disk_kms_key = "projects/${var.project_id}/locations/global/keyRings/my-ring/cryptoKeys/my-key"
}
# IPエイリアシング設定
ip_allocation_policy {
cluster_secondary_range_name = "pod-ranges"
services_secondary_range_name = "service-ranges"
}
# 初期ノードプール削除(別途作成)
remove_default_node_pool = true
initial_node_count = 1
}
# ノードプールの作成
resource "google_container_node_pool" "primary_nodes" {
name = "primary-node-pool"
location = var.location
cluster = google_container_cluster.primary.name
node_count = 3
node_config {
machine_type = "e2-standard-4"
# アクセススコープとサービスアカウントの設定
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform"
]
}
# オートスケーリング設定
autoscaling {
min_node_count = 3
max_node_count = 10
}
# アップグレード設定
upgrade_settings {
max_surge = 1
max_unavailable = 0
}
}
outputs.tf
:
outputs.tf
output "cluster_name" {
description = "クラスター名"
value = google_container_cluster.primary.name
}
output "cluster_endpoint" {
description = "クラスターエンドポイント"
value = google_container_cluster.primary.endpoint
}
output "cluster_ca_certificate" {
description = "クラスター認証局証明書"
value = google_container_cluster.primary.master_auth[0].cluster_ca_certificate
sensitive = true
}
このTerraformコードは以下の主要な機能を実装しています:
- Enterprise GKEクラスターの作成
- 東京リージョンでの展開
- データプレーンV2の有効化
- DNSを使用したコントロールプレーンアクセス
- 土日深夜のメンテナンスウィンドウ設定
- サービスメッシュの有効化
- 大阪リージョンでのバックアップ設定
- Secret Managerの統合
- RBACのためのGoogleグループ有効化
- アプリケーションレイヤでのSecret暗号化
- 基本的なセキュリティポスチャー
- 標準的な脆弱性スキャン
- Googleマネージド暗号鍵によるブートディスク暗号化
- 要求されたすべてのアクセススコープの設定
- 自動アプリケーションモニタリング
使用方法:
- 必要な変数を設定します:
terraform.tfvars ファイルを作成し、以下を設定:
project_id = "your-project-id"
- Terraformを初期化し、実行します:
terraform init
terraform plan
terraform apply
注意点:
- プロジェクトIDやその他の機密情報は、環境変数または
.tfvars
ファイルで管理することをお勧めします。 - 実際の展開前に、IPレンジやノードプールの設定を環境に合わせて調整してください。
- KMSキーの参照先は実際の環境に合わせて修正が必要です。
Discussion