🤖
GKE Podに他のGoogle Cloud Serviceへアクセスさせる
GKEのPodから他のGoogle Cloudサービスにアクセスするために、主にWorkload Identity(推奨)を設定する必要があります。以下に必要なTerraformの設定を示します。
main.tf
# Kubernetes Service Account
resource "kubernetes_service_account" "workload_identity_sa" {
metadata {
name = "application-sa" # Kubernetes内で使用するService Account名
namespace = "default" # 必要に応じて変更
annotations = {
"iam.gke.io/gcp-service-account" = google_service_account.gcp_sa.email
}
}
}
# Google Cloud Service Account
resource "google_service_account" "gcp_sa" {
account_id = "gke-workload-identity-sa"
display_name = "GKE Workload Identity Service Account"
}
# IAMポリシーバインディング(Workload Identity)
resource "google_service_account_iam_binding" "workload_identity_binding" {
service_account_id = google_service_account.gcp_sa.name
role = "roles/iam.workloadIdentityUser"
members = [
"serviceAccount:${var.project_id}.svc.id.goog[${kubernetes_service_account.workload_identity_sa.metadata[0].namespace}/${kubernetes_service_account.workload_identity_sa.metadata[0].name}]"
]
}
# 必要なIAMロールの付与
# 例:Cloud Storageへのアクセス権限
resource "google_project_iam_member" "storage_access" {
project = var.project_id
role = "roles/storage.objectViewer"
member = "serviceAccount:${google_service_account.gcp_sa.email}"
}
# 例:Cloud SQLへのアクセス権限
resource "google_project_iam_member" "cloudsql_access" {
project = var.project_id
role = "roles/cloudsql.client"
member = "serviceAccount:${google_service_account.gcp_sa.email}"
}
# 例:Secret Managerへのアクセス権限
resource "google_project_iam_member" "secret_access" {
project = var.project_id
role = "roles/secretmanager.secretAccessor"
member = "serviceAccount:${google_service_account.gcp_sa.email}"
}
そして、GKEクラスターでWorkload Identityを有効にする必要があります(既存のクラスター設定に追加):
main.tf
resource "google_container_cluster" "primary" {
# ... 既存の設定 ...
workload_identity_config {
workload_pool = "${var.project_id}.svc.id.goog"
}
}
Kubernetes Deploymentで使用する例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-application
spec:
template:
metadata:
labels:
app: my-application
spec:
serviceAccountName: application-sa # 上記で作成したKubernetes Service Account名
containers:
- name: main
image: my-application-image
主な設定ポイント:
-
Google Cloud Service Account (GSA)の作成
- アプリケーションが必要とするGoogle Cloudサービスにアクセスするための権限を持つサービスアカウント
-
Kubernetes Service Account (KSA)の作成
- Pod内で使用するサービスアカウント
- GSAとの紐付けのためのアノテーションを設定
-
Workload Identity IAMバインディング
- KSAとGSAを紐付けるIAMバインディング
- KSAがGSAとして認証できるようになる
-
必要なIAMロールの付与
- アクセスしたいGoogle Cloudサービスに応じて適切なIAMロールを付与
- 最小権限の原則に従って必要な権限のみを付与
注意点:
-
project_id
は適切な値に置き換えてください。 -
Kubernetes Service Accountの
namespace
は、実際のアプリケーションのnamespaceに合わせて変更してください。 -
IAMロールは必要最小限の権限のみを付与するようにしてください。
-
複数のアプリケーションがある場合は、アプリケーションごとに別々のService Accountを作成することを推奨します。
-
Terraformの
kubernetes
プロバイダーを使用するため、以下の設定も必要です:
provider.tf
provider "kubernetes" {
host = "https://${google_container_cluster.primary.endpoint}"
token = data.google_client_config.default.access_token
cluster_ca_certificate = base64decode(google_container_cluster.primary.master_auth[0].cluster_ca_certificate)
}
data "google_client_config" "default" {}
これらの設定により、GKE上のPodは安全にGoogle Cloudサービスにアクセスできるようになります。
Discussion