mcpField
G
gcp-gke

gcp-gke

github.com/bagelholeクラウド・DevOps1.2k337 回インストール23 週間前に更新
リポジトリ

gcp-gke について

Google Cloud で Kubernetes を運用するチーム向けの GKE スキルです。Standard と Autopilot の両モードで、クラスターの構築、スケーリング、トラブルシューティングを扱います。

主な機能

  1. 01Standard と Autopilot のクラスター作成
  2. 02ノードプールと自動スケーリングの設定
  3. 03Workload Identity の設定
  4. 04ワークロードとマネージド SSL Ingress のデプロイ
  5. 05Terraform によるクラスター設定
  6. 06一般的なクラスター問題の診断

活用シーン

  1. 01Google Cloud でコンテナサービスをデプロイ
  2. 02用途別のノードプール設定
  3. 03クラスターの ID とアクセス管理
  4. 04Pod、ノード、Ingress の問題診断

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/bagelhole/devops-security-agent-skills --skill gcp-gke

インストール後、Claude Code で/gcp-gkeを入力して読み込まれたか確認します。

詳細

Google Kubernetes Engine(GKE)

Google Cloud Platform 上で、マネージド Kubernetes クラスターをデプロイ、運用、スケーリングします。

使用する場面

  • 自動スケーリングと自動修復を活用して、コンテナ化されたマイクロサービスを大規模に実行する場合
  • きめ細かなオーケストレーション、サービスメッシュ、カスタムスケジューリングが必要なワークロード
  • Kubernetes ツール(Helm、Argo CD、Flux)をすでに活用しているチーム
  • Cloud Run のリクエストベースのモデルが適さない場合(長時間実行されるワークロード、ステートフルなワークロード)

前提条件

  • Google Cloud SDK(gcloud)と kubectl がインストールされていること
  • API が有効になっていること:Kubernetes Engine、Compute Engine
  • クラスター管理用の IAM ロール roles/container.admin
gcloud services enable container.googleapis.com compute.googleapis.com
gcloud components install kubectl

Standard と Autopilot の比較

機能StandardAutopilot
ノード管理ノードプールを自分で管理Google がノードを管理
料金ノード(VM)単位で課金Pod のリソースリクエスト単位で課金
GPU/TPU完全にサポートサポート(制限あり)
DaemonSet許可制限あり
最適な用途完全な制御、特殊なハードウェア管理の手間を省き、コストを最適化

Standard クラスターの作成

gcloud container clusters create prod-cluster \
  --region=us-central1 --num-nodes=2 \
  --machine-type=e2-standard-4 --disk-size=100 \
  --enable-autoscaling --min-nodes=1 --max-nodes=5 \
  --enable-autorepair --enable-autoupgrade \
  --release-channel=regular \
  --workload-pool=${PROJECT_ID}.svc.id.goog \
  --enable-ip-alias --enable-network-policy \
  --enable-shielded-nodes \
  --logging=SYSTEM,WORKLOAD --monitoring=SYSTEM,WORKLOAD \
  --labels=env=production,team=platform

gcloud container clusters get-credentials prod-cluster --region=us-central1

Autopilot クラスターの作成

gcloud container clusters create-auto autopilot-prod \
  --region=us-central1 --release-channel=regular \
  --workload-pool=${PROJECT_ID}.svc.id.goog \
  --network=my-vpc --subnetwork=gke-subnet

ノードプール

# High-memory pool with taint
gcloud container node-pools create highmem-pool \
  --cluster=prod-cluster --region=us-central1 \
  --machine-type=n2-highmem-8 --disk-size=200 --disk-type=pd-ssd \
  --num-nodes=1 --enable-autoscaling --min-nodes=0 --max-nodes=4 \
  --node-labels=workload=memory-intensive \
  --node-taints=dedicated=highmem:NoSchedule

# GPU pool
gcloud container node-pools create gpu-pool \
  --cluster=prod-cluster --region=us-central1 \
  --machine-type=n1-standard-8 \
  --accelerator=type=nvidia-tesla-t4,count=1 \
  --num-nodes=0 --enable-autoscaling --min-nodes=0 --max-nodes=4 \
  --node-taints=nvidia.com/gpu=present:NoSchedule

# Spot pool for batch workloads
gcloud container node-pools create spot-pool \
  --cluster=prod-cluster --region=us-central1 \
  --machine-type=e2-standard-4 --spot \
  --num-nodes=0 --enable-autoscaling --min-nodes=0 --max-nodes=20 \
  --node-taints=cloud.google.com/gke-spot=true:NoSchedule

Workload Identity

# Create GSA and grant permissions
gcloud iam service-accounts create app-gsa
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
  --member="serviceAccount:app-gsa@${PROJECT_ID}.iam.gserviceaccount.com" \
  --role="roles/storage.objectViewer"

# Create KSA and bind to GSA
kubectl create namespace myapp
kubectl create serviceaccount app-ksa --namespace=myapp
gcloud iam service-accounts add-iam-policy-binding \
  app-gsa@${PROJECT_ID}.iam.gserviceaccount.com \
  --role=roles/iam.workloadIdentityUser \
  --member="serviceAccount:${PROJECT_ID}.svc.id.goog[myapp/app-ksa]"
kubectl annotate serviceaccount app-ksa --namespace=myapp \
  iam.gke.io/gcp-service-account=app-gsa@${PROJECT_ID}.iam.gserviceaccount.com

ワークロードのデプロイ

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
  namespace: myapp
spec:
  replicas: 3
  selector:
    matchLabels: { app: web-app }
  template:
    metadata:
      labels: { app: web-app }
    spec:
      serviceAccountName: app-ksa
      containers:
      - name: web
        image: us-central1-docker.pkg.dev/PROJECT_ID/repo/web-app:v1.2.0
        ports: [{ containerPort: 8080 }]
        resources:
          requests: { cpu: 250m, memory: 512Mi }
          limits: { cpu: 500m, memory: 1Gi }
        readinessProbe:
          httpGet: { path: /healthz, port: 8080 }
          initialDelaySeconds: 5
        livenessProbe:
          httpGet: { path: /healthz, port: 8080 }
          initialDelaySeconds: 15
      topologySpreadConstraints:
      - maxSkew: 1
        topologyKey: topology.kubernetes.io/zone
        whenUnsatisfiable: DoNotSchedule
        labelSelector:
          matchLabels: { app: web-app }
---
apiVersion: v1
kind: Service
metadata: { name: web-app, namespace: myapp }
spec:
  selector: { app: web-app }
  ports: [{ port: 80, targetPort: 8080 }]
  type: ClusterIP

マネージド SSL を使用した Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web-ingress
  namespace: myapp
  annotations:
    kubernetes.io/ingress.class: "gce"
    networking.gke.io/managed-certificates: "web-cert"
    kubernetes.io/ingress.global-static-ip-name: "web-static-ip"
spec:
  rules:
  - host: app.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service: { name: web-app, port: { number: 80 } }
---
apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata: { name: web-cert, namespace: myapp }
spec:
  domains: [app.example.com]
gcloud compute addresses create web-static-ip --global

Terraform の構成

resource "google_container_cluster" "primary" {
  name     = "prod-cluster"
  location = "us-central1"

  release_channel { channel = "REGULAR" }
  workload_identity_config { workload_pool = "${var.project_id}.svc.id.goog" }

  network    = google_compute_network.vpc.name
  subnetwork = google_compute_subnetwork.gke.name

  ip_allocation_policy {
    cluster_secondary_range_name  = "pods"
    services_secondary_range_name = "services"
  }

  private_cluster_config {
    enable_private_nodes   = true
    master_ipv4_cidr_block = "172.16.0.0/28"
  }

  network_policy { enabled = true }
  logging_config { enable_components = ["SYSTEM_COMPONENTS", "WORKLOADS"] }
  monitoring_config {
    enable_components = ["SYSTEM_COMPONENTS", "WORKLOADS"]
    managed_prometheus { enabled = true }
  }

  remove_default_node_pool = true
  initial_node_count       = 1
}

resource "google_container_node_pool" "primary" {
  name     = "primary-pool"
  cluster  = google_container_cluster.primary.name
  location = "us-central1"

  initial_node_count = 2
  autoscaling { min_node_count = 1; max_node_count = 5 }
  management  { auto_repair = true; auto_upgrade = true }

  node_config {
    machine_type = "e2-standard-4"
    disk_size_gb = 100
    disk_type    = "pd-balanced"
    oauth_scopes = ["https://www.googleapis.com/auth/cloud-platform"]
    shielded_instance_config {
      enable_secure_boot          = true
      enable_integrity_monitoring = true
    }
    metadata = { disable-legacy-endpoints = "true" }
  }
}

resource "google_compute_subnetwork" "gke" {
  name          = "gke-subnet"
  ip_cidr_range = "10.0.0.0/20"
  region        = "us-central1"
  network       = google_compute_network.vpc.id

  secondary_ip_range { range_name = "pods";     ip_cidr_range = "10.4.0.0/14" }
  secondary_ip_range { range_name = "services"; ip_cidr_range = "10.8.0.0/20" }
}

一般的な操作

gcloud container clusters list
gcloud container clusters upgrade prod-cluster --region=us-central1 --master
kubectl top nodes && kubectl top pods --namespace=myapp
kubectl scale deployment web-app --replicas=5 --namespace=myapp
kubectl autoscale deployment web-app --namespace=myapp --min=3 --max=20 --cpu-percent=70
kubectl logs -f deployment/web-app --namespace=myapp --all-containers

トラブルシューティング

症状原因対処方法
Pod が Pending のままになる十分なリソースを持つノードがないオートスケーラーを確認し、より大きなノードプールを追加して、リソースリクエストを確認します
ImagePullBackOffイメージのパスが誤っている、または AR へのアクセス権がないイメージ URL を確認し、ノード SA に roles/artifactregistry.reader を付与します
Workload Identity のアカウントが誤っているKSA のアノテーションがないKSA にアノテーションを付け直し、新しいトークンを取得できるよう Pod を再起動します
ノードが NotReady になるディスク/メモリーの圧迫、またはネットワークの問題kubectl describe node を実行し、テイントと状態を確認します
Ingress が 502 を返すバックエンドの Pod がヘルスチェックに失敗しているReadiness Probe を確認し、Console で NEG のヘルス状態を確認します
クラスター作成時のクォータエラーリージョンの CPU/IP クォータが不足しているIAM と管理 > クォータでクォータの引き上げをリクエストします
ネットワークポリシーが機能しないクラスターで有効になっていない--enable-network-policy を指定して再作成するか、Dataplane V2 を使用します

関連スキル

  • gcp-networking - GKE クラスター向けの VPC、ファイアウォールルール、ロードバランサー
  • terraform-gcp - Infrastructure as Code で GKE クラスターをプロビジョニングします
  • gcp-compute - コンテナよりも VM が適しているワークロード向け
  • gcp-cloud-sql - サイドカープロキシを介して GKE Pod を Cloud SQL に接続します

コメント