😎
GKE Rolling Update on Google Cloud
gke_cluster.tf
# GKE Cluster
resource "google_container_cluster" "primary" {
name = "my-gke-cluster"
location = "us-central1"
# 初期ノードプールを削除(カスタムノードプールを使用)
remove_default_node_pool = true
initial_node_count = 1
# クラスターのバージョン管理
min_master_version = "1.27" # 希望するGKEバージョン
# リリースチャンネルの設定
release_channel {
channel = "REGULAR" # RAPID, REGULAR, または STABLE
}
# メンテナンスポリシー
maintenance_policy {
recurring_window {
start_time = "2024-01-01T09:00:00Z"
end_time = "2024-01-01T17:00:00Z"
recurrence = "FREQ=WEEKLY;BYDAY=SA,SU" # 土日の9時から17時
}
}
}
# Node Pool with rolling update configuration
resource "google_container_node_pool" "primary_nodes" {
name = "my-node-pool"
cluster = google_container_cluster.primary.name
location = google_container_cluster.primary.location
version = google_container_cluster.primary.min_master_version
node_count = 3
# ノードプールの自動アップグレード設定
management {
auto_repair = true
auto_upgrade = true
}
# ローリングアップデートの設定
upgrade_settings {
max_surge = 1
max_unavailable = 0
strategy = "SURGE"
}
# ノードの設定
node_config {
machine_type = "e2-standard-2"
# サービスアカウント
service_account = google_service_account.gke_sa.email
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform"
]
# Workload Identity
workload_metadata_config {
mode = "GKE_METADATA"
}
# ラベルとTaint
labels = {
environment = "production"
}
taint {
key = "special"
value = "true"
effect = "NO_SCHEDULE"
}
}
# ライフサイクル
lifecycle {
create_before_destroy = true
ignore_changes = [
node_count # Autoscalerを使用する場合
]
}
}
# Service Account for GKE nodes
resource "google_service_account" "gke_sa" {
account_id = "gke-node-sa"
display_name = "GKE Node Service Account"
}
# IAM role binding for the service account
resource "google_project_iam_member" "gke_sa_roles" {
for_each = toset([
"roles/logging.logWriter",
"roles/monitoring.metricWriter",
"roles/monitoring.viewer",
"roles/stackdriver.resourceMetadata.writer"
])
project = var.project_id
role = each.key
member = "serviceAccount:${google_service_account.gke_sa.email}"
}
variables.tf
variable "project_id" {
description = "Google Cloud Project ID"
type = string
}
variable "region" {
description = "GCP Region"
type = string
default = "us-central1"
}
variable "cluster_name" {
description = "Name of the GKE cluster"
type = string
default = "my-gke-cluster"
}
node_pool_update.tf
# 追加のノードプール(Blue/Green デプロイメント用)
resource "google_container_node_pool" "blue" {
name = "blue-pool"
cluster = google_container_cluster.primary.name
location = google_container_cluster.primary.location
version = google_container_cluster.primary.min_master_version
node_count = 2
management {
auto_repair = true
auto_upgrade = true
}
upgrade_settings {
max_surge = 1
max_unavailable = 0
strategy = "SURGE"
}
node_config {
machine_type = "e2-standard-2"
labels = {
pool = "blue"
}
}
}
resource "google_container_node_pool" "green" {
name = "green-pool"
cluster = google_container_cluster.primary.name
location = google_container_cluster.primary.location
version = google_container_cluster.primary.min_master_version
# 初期状態では0ノード
node_count = 0
management {
auto_repair = true
auto_upgrade = true
}
upgrade_settings {
max_surge = 1
max_unavailable = 0
strategy = "SURGE"
}
node_config {
machine_type = "e2-standard-2"
labels = {
pool = "green"
}
}
}
主な設定ポイントの説明:
- クラスターの更新設定:
-
min_master_version
: クラスターのバージョン -
release_channel
: アップデートチャンネル -
maintenance_policy
: メンテナンス時間帯
- ノードプールの更新設定:
upgrade_settings {
max_surge = 1 # アップデート中に追加できるノード数
max_unavailable = 0 # アップデート中に利用不可になれるノード数
strategy = "SURGE" # アップデート戦略
}
- 自動修復と自動アップグレード:
management {
auto_repair = true # 異常なノードの自動修復
auto_upgrade = true # 自動アップグレードの有効化
}
- Blue/Green デプロイメント:
- 2つのノードプールを用意
- 新バージョンをGreenプールにデプロイ
- テスト後、トラフィックを切り替え
ベストプラクティス:
- メンテナンス時間帯の設定
- トラフィックの少ない時間帯を選択
- タイムゾーンを考慮
- ノードプールの更新戦略
-
max_surge
とmax_unavailable
を適切に設定 - 可用性要件に基づいて調整
- モニタリングとロギング
- 適切なIAM権限を設定
- ログとメトリクスの収集を有効化
- セキュリティ設定
- Workload Identityの使用
- 最小権限の原則に従ったサービスアカウント
- スケーラビリティ
- 必要に応じてノードプールを複数作成
- リージョナルクラスターの検討
このコードを使用することで、GKEクラスターの安全で制御されたローリングアップデートを実現できます。
Discussion