Skip to content

Generation-aware storage provisioning for GKE ComputeClass

Fourth-generation (Gen4) Compute Engine machine series, such as c4 and n4, require Google Cloud Hyperdisk storage volumes such as hyperdisk-balanced and cannot attach legacy Persistent Disks. In contrast, second-generation (Gen2) machine series, such as n2 and c2, default to Persistent Disk such as pd-balanced, and Google Kubernetes Engine (GKE) supply admission webhooks reject Hyperdisk boot disks on Gen2 nodes. When a cluster falls back across hardware generations during regional capacity stockouts, stateful workloads stall in ContainerCreating unless compute selection and storage provisioning coordinate across generations.

This project provides a reference architecture, a Terraform testbed, an Agent Skill for generating and validating generation-aware GKE manifests, and a replicated Apache Kafka demo workload with live performance tuning and Day-2 cross-generation storage rebalancing.

Architecture

The solution coordinates compute priority ladders, late-binding dynamic storage, declarative volume tuning, and Container Storage Interface (CSI) snapshot rebalancing across GKE Autopilot and GKE Standard clusters with Node Auto-Provisioning (NAP) enabled.

flowchart TB
    subgraph AgentSkill ["Agent Skill (.agents/skills/generation-aware-storage-ccc)"]
        Discovery["Cluster and Compute Engine catalog discovery"]
        Validator["Catalog sizing check and server-side dry-run"]
    end

    subgraph ControlPlane ["GKE scheduling and storage policy"]
        CCC["ComputeClass (c4 -> n4 -> n2 with Gen4 reservation affinity)"]
        SC["Dynamic StorageClass (WaitForFirstConsumer + topology)"]
        VAC["VolumeAttributesClass (live Hyperdisk IOPS and throughput)"]
    end

    subgraph PrimaryTier ["Preferred Gen4 tier (reserved C4 capacity)"]
        C4Node["c4-standard-4 node (hyperdisk-balanced boot disk)"]
        Broker0["kafka-cluster-0"]
        HD0["hyperdisk-balanced PVC (tuned to 6000 IOPS)"]
    end

    subgraph FallbackTier ["Fallback Gen2 tier (on-demand N2 during stockout)"]
        N2Node["n2-standard-4 node (pd-balanced boot disk)"]
        Broker1["kafka-cluster-1"]
        PD1["pd-balanced PVC"]
    end

    subgraph Day2 ["Day-2 capacity recovery"]
        Watcher["rebalance_capacity_watcher.sh"]
        Snap["CSI VolumeSnapshot -> new hyperdisk-balanced PVC on C4"]
    end

    Discovery --> Validator
    Validator --> CCC
    Validator --> SC
    Validator --> VAC

    CCC --> C4Node
    CCC --> N2Node
    SC --> HD0
    SC --> PD1
    VAC --> HD0

    Broker0 --> C4Node
    Broker0 --> HD0
    Broker1 --> N2Node
    Broker1 --> PD1

    PD1 --> Watcher
    Watcher --> Snap
    Snap --> C4Node

Components

  • Terraform testbed (terraform/): Provisions an isolated Virtual Private Cloud (VPC) network and subnetwork with Private Google Access enabled, a Cloud Router and Cloud Network Address Translation (NAT) gateway for private-node outbound egress, a GKE Autopilot cluster with private nodes in the REGULAR release channel, and a single-instance c4-standard-4 Compute Engine reservation (specific_reservation_required = false) to trigger a deterministic capacity stockout when scaling beyond one broker.
  • Agent Skill (.agents/skills/generation-aware-storage-ccc/): Discovers the active GKE cluster operating mode and live Compute Engine machine-type and reservation catalogs, interviews you for workload requirements, synthesizes a custom ComputeClass, late-binding StorageClass, and VolumeAttributesClass (VAC), verifies catalog sizing and reservation parity, runs server-side dry-runs, and applies the manifests after approval.
  • Stateful demo workload and Day-2 automation (demo/): Includes a replicated Apache Kafka KRaft StatefulSet (kafka-cluster.yaml), an In-Sync Replicas (ISR) and sequence-gap verification helper (kafka_data_continuity.sh), and a Day-2 CSI VolumeSnapshot rebalancer (rebalance_capacity_watcher.sh) that promotes stranded Gen2 brokers back to Gen4 Hyperdisk volumes when C4 capacity returns.

Requirements

To deploy the testbed, run the Agent Skill, and execute the Kafka walkthrough, you need:

  • A Google Cloud project with billing enabled and regional virtual CPU (vCPU) quota for C4, N4, and N2 machine families in a target region that supports all three families, such as us-central1.
  • An account granted the following Identity and Access Management (IAM) roles on the project:
    • Service Usage Admin (roles/serviceusage.serviceUsageAdmin)
    • Kubernetes Engine Admin (roles/container.admin)
    • Compute Network Admin (roles/compute.networkAdmin)
    • Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1)
    • Compute Storage Admin (roles/compute.storageAdmin)
    • Service Account User (roles/iam.serviceAccountUser)
  • The Google Cloud command-line interface (CLI) (gcloud 480.0.0 or later), kubectl (1.30 or later), GKE kubectl auth plugin (gke-gcloud-auth-plugin), jq, and Terraform installed.
  • An AI coding agent CLI that supports the Agent Skills specification, such as Gemini CLI or Antigravity CLI.

Prepare the environment

  1. Set your project and region environment variables and authenticate the gcloud CLI:

    export PROJECT_ID="YOUR_PROJECT_ID"
    export REGION="us-central1"
    gcloud config set project "${PROJECT_ID}"
    gcloud auth login
    gcloud auth application-default login
    
  2. Clone the repository using sparse checkout and change to the project directory:

    git clone --filter=blob:none --no-checkout https://github.com/GoogleCloudPlatform/cloud-solutions
    cd cloud-solutions
    git sparse-checkout set --cone projects/gke-crossgen-storage-aware-ccc
    git checkout
    cd projects/gke-crossgen-storage-aware-ccc
    

Deploy the testbed with Terraform

If you already have a GKE cluster and want to generate manifests for it, skip to Generate, validate, and apply manifests with the Agent Skill.

To provision the GKE Autopilot cluster and single-instance c4-standard-4 reservation:

  1. Initialize and apply the Terraform configuration from the project root. By default, Terraform places the C4 reservation in the first UP zone of ${REGION} (pass -var="reservation_zone=<ZONE>" to select another zone in the same region):

    terraform -chdir=terraform init
    terraform -chdir=terraform apply \
      -var="project_id=${PROJECT_ID}" \
      -var="region=${REGION}"
    
  2. Capture the provisioned cluster name, reservation name, and reservation zone, authenticate kubectl, and verify that the cluster is clean and the C4 reservation shows IN_USE_COUNT=0 out of COUNT=1:

    export CLUSTER_NAME=$(terraform -chdir=terraform output -raw cluster_name)
    export RES_NAME=$(terraform -chdir=terraform output -raw c4_reservation_name)
    export ZONE=$(terraform -chdir=terraform output -raw reservation_zone)
    gcloud container clusters get-credentials "${CLUSTER_NAME}" \
      --region="${REGION}" \
      --project="${PROJECT_ID}"
    kubectl config current-context
    gcloud compute reservations list \
      --project="${PROJECT_ID}" \
      --filter="name=${RES_NAME}"
    

Generate, validate, and apply manifests with the Agent Skill

Start your agent CLI from the projects/gke-crossgen-storage-aware-ccc directory so it discovers the generation-aware-storage-ccc skill under .agents/skills/. The skill writes generated manifests outside the repository under /tmp/gke-crossgen-manifests/${CLUSTER_NAME}/.

  1. Trigger read-only cluster discovery and the design interview:

    Help design GKE infrastructure for my workloads with flexibility,
    efficiency, and capacity stockouts in mind.
    

    Verify that the skill detects clusterMode: autopilot and sampleZone: us-central1-a, caches Compute Engine machine-types.json and reservations.json under /tmp/gke-crossgen-manifests/<cluster-name>/, and surfaces the c4-standard-4 reservation (4 vCPU, 15 GiB, specificReservationRequired: false).

  2. Provide your answers to the Step 2 design interview:

    1. Workload profile and fallback ladder: Option A (General Purpose /
       Balanced: c4 -> n4 -> n2), targeting 4-vCPU standard-tier nodes. Name the
       ComputeClass `general-crossgen-cc`.
    2. Reservations: Option C (`AnyThenFail`) on the Gen4 priorities (`c4` and
       `n4`) only, leaving the Gen2 (`n2`) fallback priority on-demand (no
       reservation affinity) so it can scale up when the C4 reservation is
       exhausted.
    3. Default status: No, do not set it as cluster or namespace default.
    4. Storage performance (VolumeAttributesClass): Yes, create
       `hyperdisk-performance-vac` with `iops: "6000"` and `throughput: "250Mi"`,
       alongside `dynamic-sc`.
    5. CMEK encryption: No.
    6. Output directory: Accept the default
       (`/tmp/gke-crossgen-manifests/<cluster-name>/`).
    

    Verify that the skill queries machine-types.json, sets minMemoryGb: 15 on c4 and 16 on n4 and n2, omits storage.bootDiskType on Gen4 priorities for GKE Autopilot admission compliance, confirms resolved shapes (c4-standard-4, n4-standard-4, and n2-standard-4) and reservation parity against the live catalog, and passes server-side dry-run on all three manifests.

  3. Approve deployment of the validated manifests:

    Approved. Apply the validated manifests to the cluster. Skip the interactive
    walkthrough.
    
  4. Confirm that the three cluster resources are active:

    kubectl get computeclass general-crossgen-cc
    kubectl get sc dynamic-sc
    kubectl get volumeattributesclass hyperdisk-performance-vac
    

Run the Kafka failover, live IOPS tuning, and Day-2 rebalance walkthrough

Run all commands in this section from the demo/ directory:

cd demo

Deploy Kafka on Gen4 compute and Hyperdisk

  1. Apply the single-replica Kafka KRaft StatefulSet and wait until kafka-cluster-0 reaches 1/1 Running:

    kubectl apply -f kafka-cluster.yaml
    kubectl get pods -l app=kafka -o wide -w
    
  2. Stop the watch (Ctrl+C) and inspect the provisioned node, data disk, and reservation utilization:

    kubectl get nodes \
      -L node.kubernetes.io/instance-type,cloud.google.com/machine-family,cloud.google.com/gke-boot-disk,topology.kubernetes.io/zone
    PV0=$(kubectl get pvc kafka-data-kafka-cluster-0 -o jsonpath='{.spec.volumeName}')
    gcloud compute disks describe "${PV0}" \
      --zone="${ZONE}" \
      --project="${PROJECT_ID}" \
      --format="table(name,type.basename(),sizeGb,provisionedIops,provisionedThroughput)"
    gcloud compute reservations list \
      --project="${PROJECT_ID}" \
      --filter="name=${RES_NAME}"
    

    Confirm that kafka-cluster-0 runs on c4-standard-4 with a hyperdisk-balanced boot disk, PV0 is a 50 GiB hyperdisk-balanced data disk, and the C4 reservation reports IN_USE_COUNT=1 out of COUNT=1.

Tune Hyperdisk IOPS without pod restarts

  1. Patch kafka-data-kafka-cluster-0 to bind hyperdisk-performance-vac:

    kubectl patch pvc kafka-data-kafka-cluster-0 \
      -p '{"spec":{"volumeAttributesClassName":"hyperdisk-performance-vac"}}'
    
  2. Wait approximately 15 to 30 seconds and verify that Input/Output Operations Per Second (IOPS) and throughput update in place with zero pod restarts:

    kubectl get pvc kafka-data-kafka-cluster-0 \
      -o jsonpath='{.spec.volumeAttributesClassName} -> {.status.currentVolumeAttributesClassName}{"\n"}'
    kubectl get pod kafka-cluster-0 \
      -o jsonpath='restarts={.status.containerStatuses[0].restartCount}{"\n"}'
    PV0=$(kubectl get pvc kafka-data-kafka-cluster-0 -o jsonpath='{.spec.volumeName}')
    gcloud compute disks describe "${PV0}" \
      --zone="${ZONE}" \
      --project="${PROJECT_ID}" \
      --format="table(name,type.basename(),sizeGb,provisionedIops,provisionedThroughput)"
    

    Confirm hyperdisk-performance-vac -> hyperdisk-performance-vac, restarts=0, provisionedIops=6000, and provisionedThroughput=250.

Simulate a C4 stockout and verify fallback to N2

  1. Scale the Kafka StatefulSet to 2 replicas and watch kafka-cluster-1 schedule. Because kafka-cluster-0 consumes the only C4 reservation slot and Gen4 tiers enforce reservations.affinity: AnyThenFail, GKE falls back to the on-demand Gen2 n2 tier:

    kubectl scale statefulset kafka-cluster --replicas=2
    kubectl get pods -l app=kafka -o wide -w
    
  2. Once kafka-cluster-1 is 1/1 Running, stop the watch (Ctrl+C), inspect the new node and PersistentVolume (PV), and seed a replicated topic (crossgen-orders, replication-factor=2, min.insync.replicas=1) with 10 baseline messages:

    kubectl get nodes \
      -L node.kubernetes.io/instance-type,cloud.google.com/machine-family,cloud.google.com/gke-boot-disk,topology.kubernetes.io/zone
    PV1=$(kubectl get pvc kafka-data-kafka-cluster-1 -o jsonpath='{.spec.volumeName}')
    PV1_ZONE=$(kubectl get pv "${PV1}" -o jsonpath='{.spec.csi.volumeHandle}' | cut -d/ -f4)
    gcloud compute disks describe "${PV1}" \
      --zone="${PV1_ZONE}" \
      --project="${PROJECT_ID}" \
      --format="table(name,type.basename(),sizeGb)"
    bash ./kafka_data_continuity.sh init
    

    Confirm that kafka-cluster-1 runs on n2-standard-4 with a pd-balanced boot disk and 50 GiB pd-balanced data disk, and that crossgen-orders reports Replicas: 0,1 and Isr: 0,1 across both generations.

Recover C4 capacity and run the Day-2 rebalancer

  1. In a second terminal positioned in demo/, start streaming live acks=all messages while leaving it running throughout the rebalance:

    bash ./kafka_data_continuity.sh produce
    
  2. In the first terminal, run a dry-run check to confirm the watcher detects kafka-cluster-1 on n2 and defers while zero free C4 slots exist:

    DRY_RUN=true bash ./rebalance_capacity_watcher.sh
    
  3. Expand the C4 reservation from 1 to 2 instances so preferred C4 capacity becomes available in ${ZONE}:

    gcloud compute reservations update "${RES_NAME}" \
      --zone="${ZONE}" \
      --vm-count=2 \
      --project="${PROJECT_ID}"
    
  4. Run the automated Day-2 CSI VolumeSnapshot rebalancer to snapshot the N2 disk, drain the idle N2 node, and promote kafka-cluster-1 onto a C4 node with hyperdisk-balanced:

    bash ./rebalance_capacity_watcher.sh
    
  5. Stop the producer in the second terminal (Ctrl+C), then verify full ISR recovery, zero sequence gaps, and promoted C4 and Hyperdisk state in the first terminal:

    bash ./kafka_data_continuity.sh verify
    kubectl get nodes \
      -L node.kubernetes.io/instance-type,cloud.google.com/machine-family,cloud.google.com/gke-boot-disk
    PV1_NEW=$(kubectl get pvc kafka-data-kafka-cluster-1 -o jsonpath='{.spec.volumeName}')
    gcloud compute disks describe "${PV1_NEW}" \
      --zone="${ZONE}" \
      --project="${PROJECT_ID}" \
      --format="table(name,type.basename(),sizeGb,provisionedIops,provisionedThroughput)"
    gcloud compute reservations list \
      --project="${PROJECT_ID}" \
      --filter="name=${RES_NAME}"
    

    Confirm 0 gaps, 0 lost messages, two c4-standard-4 nodes backed by hyperdisk-balanced boot and data disks, zero n2 nodes, and IN_USE_COUNT=2 out of COUNT=2.

Clean up the environment

To avoid ongoing charges, return to the project root, delete the Kubernetes demo resources, and destroy the Terraform testbed:

cd ..
kubectl delete -f demo/kafka-cluster.yaml --ignore-not-found
kubectl delete pvc -l app=kafka --ignore-not-found
kubectl delete volumesnapshot kafka-data-kafka-cluster-1-rebalance-snap --ignore-not-found
kubectl delete volumesnapshotclass gke-pd-snapshot-class --ignore-not-found
kubectl delete volumeattributesclass hyperdisk-performance-vac --ignore-not-found
kubectl delete computeclass general-crossgen-cc --ignore-not-found
kubectl delete sc dynamic-sc --ignore-not-found

terraform -chdir=terraform destroy \
  -var="project_id=${PROJECT_ID}" \
  -var="region=${REGION}"