Google Kubernetes Engine (GKE) Pod snapshots let you save and restore running Pod
states to improve workload startup latency. Before you can capture snapshots of
your workloads, you must prepare your cluster and storage environment.

Use this document to learn how to enable and configure Pod snapshots on your
cluster and set up Cloud Storage to store the snapshots. For more information
about how Pod snapshots work, see [About GKE Pod snapshots](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/pod-snapshots.md.txt).

Platform admins and operators and Application developers can use this
document to configure clusters, create storage buckets, and set up access
permissions for Pod snapshots. For more information about the common roles and
example tasks referenced in Google Cloud content, see
[Common GKE user roles and
tasks](https://docs.cloud.google.com/kubernetes-engine/enterprise/docs/concepts/roles-tasks).

## Before you begin


Before you start, make sure that you have performed the following tasks:

- Enable the Google Kubernetes Engine API.
[Enable Google Kubernetes Engine API](https://console.cloud.google.com/apis/enableflow?apiid=container.googleapis.com)
- To use the Google Cloud CLI for this task, [install](https://docs.cloud.google.com/sdk/docs/install) and then [initialize](https://docs.cloud.google.com/sdk/docs/initialize.md.txt) the gcloud CLI. If you previously installed the gcloud CLI, get the latest version by running the `gcloud components update` command. Earlier gcloud CLI versions might not support running the commands in this document.

  > [!NOTE]
  > **Note:** For existing gcloud CLI installations, make sure to set the `compute/region` [property](https://docs.cloud.google.com/sdk/docs/properties.md.txt#setting_properties). If you use primarily zonal clusters, set the `compute/zone` instead. By setting a default location, you can avoid errors in the gcloud CLI like the following: `One of [--zone, --region] must be supplied: Please specify location`. You might need to specify the location in certain commands if the location of your cluster differs from the default that you set.

## Enable Pod snapshots

You can enable Pod snapshots on a new or existing cluster. Pod snapshots
require GKE Sandbox and [Workload Identity Federation for GKE](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/workload-identity.md.txt).
Autopilot clusters enable both features by default. For
Standard clusters, you must enable
Workload Identity Federation for GKE on the cluster and configure a node pool to
run in GKE Sandbox.

To enable Pod snapshots on a cluster, follow the steps for the
GKE mode of operation that you want to use:

### Autopilot

- To enable Pod snapshots on a new cluster, run the following command:

  ```bash
    gcloud container clusters create-auto CLUSTER_NAME \
        --enable-pod-snapshots \
        --location=CONTROL_PLANE_LOCATION \
        --cluster-version=CLUSTER_VERSION
  ```

  Replace the following:
  - `CLUSTER_NAME`: the name of your cluster.
  - `CONTROL_PLANE_LOCATION`: the [location](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) of the control plane of your cluster.
  - `CLUSTER_VERSION`: the version of your new cluster, which must be 1.35.3-gke.1234000 or later.
- To enable Pod snapshots on an existing cluster, follow these steps:

  1. Upgrade the cluster to version 1.35.3-gke.1234000 or later:

     ```bash
       gcloud container clusters upgrade CLUSTER_NAME \
           --cluster-version=CLUSTER_VERSION \
           --location=CONTROL_PLANE_LOCATION
     ```

     Replace the following:
     - `CLUSTER_NAME`: the name of your cluster.
     - `CLUSTER_VERSION`: the version to upgrade your cluster to, which must be 1.35.3-gke.1234000 or later.
     - `CONTROL_PLANE_LOCATION`: the [location](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) of the control plane of your cluster.
  2. Enable Pod snapshots on your cluster:

     ```bash
       gcloud container clusters update CLUSTER_NAME \
           --enable-pod-snapshots \
           --location=CONTROL_PLANE_LOCATION
     ```

     Replace the following:
     - `CLUSTER_NAME`: the name of your cluster.
     - `CONTROL_PLANE_LOCATION`: the [location](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) of the control plane of your cluster.

Pod snapshots with the default `whole-pod` scope don't support E2 machine
types, whereas file system snapshots (`rootfs-only`) do. In
Autopilot clusters, GKE might schedule Pods on E2
nodes by default. If you use the `whole-pod` scope, use a
[ComputeClass custom resource](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/node-attributes-compute-classes.md.txt) to help ensure that your workloads run on
compatible hardware.

To create and use a ComputeClass custom resource, follow these steps:

1. Save the following manifest as `non-e2-class.yaml`:

   ```yaml
     apiVersion: cloud.google.com/v1
     kind: ComputeClass
     metadata:
       name: non-e2-class
     spec:
       priorities:
       - machineFamily: n2
       - machineFamily: c3
       activeMigration:
         optimizeRulePriority: false
       whenUnsatisfiable: DoNotScaleUp
   ```
2. Apply the manifest:

   ```bash
     kubectl apply -f non-e2-class.yaml
   ```
3. In your Pod specification, reference the ComputeClass custom resource
   by using the `cloud.google.com/compute-class` node selector:

   ```yaml
     spec:
       nodeSelector:
         cloud.google.com/compute-class: non-e2-class
       # Multiple lines are omitted here
   ```

### Standard

- To enable Pod snapshots on a new cluster, run the following command:

  ```bash
    gcloud container clusters create CLUSTER_NAME \
        --enable-pod-snapshots \
        --cluster-version=CLUSTER_VERSION \
        --workload-pool=PROJECT_ID.svc.id.goog \
        --workload-metadata=GKE_METADATA \
        --location=CONTROL_PLANE_LOCATION
  ```

  Replace the following:
  - `CLUSTER_NAME`: the name of your cluster.
  - `CLUSTER_VERSION`: the version of your new cluster, which must be 1.35.3-gke.1234000 or later.
  - `PROJECT_ID`: your project ID.
  - `CONTROL_PLANE_LOCATION`: the [location](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) of the control plane of your cluster.
- To enable Pod snapshots on an existing cluster, follow these steps:

  1. Upgrade the cluster to version 1.35.3-gke.1234000 or later:

     ```bash
       gcloud container clusters upgrade CLUSTER_NAME \
           --node-pool=NODE_POOL_NAME \
           --cluster-version=CLUSTER_VERSION \
           --location=CONTROL_PLANE_LOCATION
     ```

     Replace the following:
     - `CLUSTER_NAME`: the name of your cluster.
     - `NODE_POOL_NAME`: the name of your node pool.
     - `CLUSTER_VERSION`: the version to upgrade your cluster to, which must be 1.35.3-gke.1234000 or later.
     - `CONTROL_PLANE_LOCATION`: the [location](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) of the control plane of your cluster.
  2. Enable Pod snapshots on your cluster:

     ```bash
       gcloud beta container clusters update CLUSTER_NAME \
           --enable-pod-snapshots \
           --location=CONTROL_PLANE_LOCATION
     ```

     Replace the following:
     - `CLUSTER_NAME`: the name of your cluster.
     - `CONTROL_PLANE_LOCATION`: the [location](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) of the control plane of your cluster.

To run Pods in GKE Sandbox on a Standard cluster, you must
create or update a node pool with GKE Sandbox enabled. To update a node
pool, use the `--sandbox type=gvisor` flag. To create a node pool with
GKE Sandbox enabled, run the following command:

```bash
gcloud container node-pools create NODE_POOL_NAME \
    --cluster=CLUSTER_NAME \
    --node-version=NODE_VERSION \
    --machine-type=MACHINE_TYPE \
    --location=CONTROL_PLANE_LOCATION \
    --image-type=cos_containerd \
    --sandbox type=gvisor
```

Replace the following:

- `NODE_POOL_NAME`: the name of your new node pool.
- `CLUSTER_NAME`: the name of your cluster.
- `NODE_VERSION`: the version to use for the node pool.
- `MACHINE_TYPE`: the [type of machine](https://docs.cloud.google.com/compute/docs/machine-types) to use for the nodes.
- `CONTROL_PLANE_LOCATION`: the [location](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) of the control plane of your cluster.

For more information about GKE Sandbox, see
[Harden workload isolation with GKE Sandbox](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/sandbox-pods.md.txt#enabling-existing).

## Store snapshots

GKE stores Pod snapshots in a [Cloud Storage bucket](https://docs.cloud.google.com/storage/docs/buckets.md.txt). Each
snapshot contains the application memory and, optionally, the GPU state of the
Pod. To authenticate to Cloud Storage, Pod snapshots use the Pod's
ServiceAccount object, which requires
[Workload Identity Federation for GKE](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/workload-identity.md.txt).

Pod snapshots require the following bucket configurations:

- **[Hierarchical namespaces](https://docs.cloud.google.com/storage/docs/hns-overview.md.txt)** : you must enable hierarchical namespaces to support higher read and write queries per second. Hierarchical namespaces also require that you enable [uniform bucket-level access](https://docs.cloud.google.com/storage/docs/uniform-bucket-level-access.md.txt).
- **[Soft delete](https://docs.cloud.google.com/storage/docs/soft-delete.md.txt)** : because Pod snapshots use [parallel composite uploads](https://docs.cloud.google.com/storage/docs/parallel-composite-uploads.md.txt), disable data protection features like soft delete. If you leave these features enabled, then soft deletions of the temporary objects can increase your storage bill significantly.
- **[Location](https://docs.cloud.google.com/storage/docs/locations.md.txt)**: the Cloud Storage bucket location must be in the same location as the GKE cluster because transferring snapshots across different regions might affect performance.

To create a bucket with the required configurations, run the following command:

```bash
gcloud storage buckets create gs://BUCKET_NAME \
    --uniform-bucket-level-access \
    --enable-hierarchical-namespace \
    --soft-delete-duration=0d \
    --location=LOCATION
```

Replace the following:

- `BUCKET_NAME`: the name of your bucket.
- `LOCATION`: the location of your bucket.

For a complete list of options for bucket creation, see
[Flags](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/create.md.txt#FLAGS) in the documentation for the `buckets create` command.

## Grant workloads access to the Cloud Storage bucket

By default, GKE doesn't have access to Cloud Storage.
To read and write Pod snapshots, you must grant an IAM role
that contains the corresponding permissions to the Kubernetes ServiceAccount
object that your workload Pods use, or [enable multi-tenancy by using
short-lived tokens](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/pod-snapshots-prepare#enable-multi-tenancy).

### Grant roles to specific Kubernetes ServiceAccount objects

> [!WARNING]
> **Warning:** Granting Cloud Storage permissions to a Pod's Kubernetes ServiceAccount (`podKSA`) lets processes running inside the container access the Cloud Storage bucket directly, which can be a security risk. Don't use `podKSA` if the Pod runs untrusted code, such as an agentic sandbox. Instead, [enable multi-tenancy by using short-lived tokens](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/pod-snapshots-prepare.md.txt#enable-multi-tenancy) (`federatedP4SA`).

To grant roles to specific Kubernetes ServiceAccount objects, follow these
steps:

1. To communicate with your cluster by using `kubectl` commands, get
   credentials for your cluster:

   ```bash
   gcloud container clusters get-credentials CLUSTER_NAME \
       --location=CONTROL_PLANE_LOCATION
   ```

   Replace the following:
   - `CLUSTER_NAME`: the name of your cluster.
   - `CONTROL_PLANE_LOCATION`: the [location](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) of the control plane of your cluster.
2. For each Pod, follow these steps:

   1. Create a ServiceAccount object:

      ```bash
      kubectl create serviceaccount SERVICEACCOUNT_NAME \
          --namespace=NAMESPACE
      ```

      Replace the following:
      - `SERVICEACCOUNT_NAME`: the name of your ServiceAccount object.
      - `NAMESPACE`: the namespace for your Pods.
   2. Grant the ServiceAccount object permission to access the bucket. Choose
      one of the following options based on your security requirements:

      - **Standard permissions**: grants full read, write, and delete access.
      - **Write-once permissions**: prevents overwriting or deleting finalized snapshot files, but grants permission to delete temporary objects for parallel composite uploads.

      To grant permission, run the commands for your selected option:

      ### Standard permissions

      ```bash
      gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
          --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/SERVICEACCOUNT_NAME" \
          --role="roles/storage.bucketViewer"
      ```

      ```bash
      gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
          --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/SERVICEACCOUNT_NAME" \
          --role="roles/storage.objectUser"
      ```

      ### Write-once permissions

      ```bash
      gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
          --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/SERVICEACCOUNT_NAME" \
          --role="roles/storage.bucketViewer"
      ```

      ```bash
      gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
          --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/SERVICEACCOUNT_NAME" \
          --role="roles/storage.objectCreator"
      ```

      ```bash
      gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
          --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/SERVICEACCOUNT_NAME" \
          --role="roles/storage.objectViewer"
      ```

      ```bash
      gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
          --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/SERVICEACCOUNT_NAME" \
          --role="roles/storage.objectUser" \
          --condition='expression=resource.type == "storage.googleapis.com/Object" && (resource.name.extract("/pages.img_{part}") != "" || resource.name.extract("/fs_checkpoint_pages.img_{part}") != ""),title=allow_pcu_temp_part_cleanup,description=Allow deleting temporary parallel composite upload part objects only'
      ```

      Replace the following:
      - `BUCKET_NAME`: the name of your bucket.
      - `PROJECT_NUMBER`: your project number.
      - `PROJECT_ID`: your project ID.
      - `NAMESPACE`: the namespace for your Pods.
      - `SERVICEACCOUNT_NAME`: the name of your ServiceAccount object.

### Enable multi-tenancy using short-lived tokens

As an alternative to granting permissions to individual ServiceAccount objects,
you can enable multi-tenancy with short-lived, scope-restricted tokens. This
approach helps to avoid the propagation delay associated with manual
IAM bindings. Instead of granting permissions to each
ServiceAccount object, you perform a one-time grant of the
[Storage Admin role](https://docs.cloud.google.com/iam/docs/roles-permissions/storage.md.txt#storage.admin) (`roles/storage.admin`) on the snapshot storage bucket
to the GKE node service account. The node service account then
creates short-lived tokens on demand for specific paths.

Enabling tokens with Pod snapshots requires GKE
version 1.35.3-gke.1737000 or later.

To enable multi-tenancy, follow these steps:

1. To grant the node service account permission to use the bucket,
   run the following command:

   ```bash
   gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
       --member="serviceAccount:service-PROJECT_NUMBER@gcp-sa-gkenode.iam.gserviceaccount.com" \
       --role="roles/storage.admin"
   ```

   Replace the following:
   - `BUCKET_NAME`: the name of your bucket.
   - `PROJECT_NUMBER`: your project number.
2. When you [configure storage for snapshots](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/pod-snapshots-prepare.md.txt#configure-storage), set the value of the
   `tokenSource` field to `federatedP4SA`.

## Grant the Pod snapshot controller access to the Cloud Storage bucket

To let the Pod snapshot controller delete snapshots inside the
Cloud Storage bucket, you must grant the
[Storage Object User role](https://docs.cloud.google.com/iam/docs/roles-permissions/storage.md.txt#storage.objectUser) (`roles/storage.objectUser`) to the
GKE service agent on the project. To grant the role, run the
following command:

```bash
gcloud projects add-iam-policy-binding PROJECT_ID \
    --member="serviceAccount:service-PROJECT_NUMBER@container-engine-robot.iam.gserviceaccount.com" \
    --role="roles/storage.objectUser" \
    --condition="expression=resource.name.startsWith(\"projects/_/buckets/BUCKET_NAME\"),title=restrict_to_bucket,description=Restricts access to one bucket only"
```

Replace the following:

- `PROJECT_ID`: your project ID.
- `PROJECT_NUMBER`: your project number.
- `BUCKET_NAME`: the name of your bucket.

## Optional: Create managed folders for the Cloud Storage bucket

To isolate permissions for snapshots from mutually untrusted Pods, which is
useful in multi-tenant use cases, create managed folders. To set up managed
folders, follow these steps:

1. Create a custom IAM role that contains only the necessary
   permissions for Pod snapshots. Run the command for your selected option:

   ### Standard permissions

   ```bash
   gcloud iam roles create podSnapshotGcsReadWriter \
       --project=PROJECT_ID \
       --permissions="storage.objects.get,storage.objects.list,storage.objects.create,storage.objects.delete,storage.folders.create"
   ```

   ### Write-once permissions

   ```bash
   gcloud iam roles create podSnapshotGcsReadWriter \
       --project=PROJECT_ID \
       --permissions="storage.objects.get,storage.objects.list,storage.objects.create,storage.folders.create"
   ```

   Replace `PROJECT_ID` with your project ID.
2. Grant the [Storage Bucket Viewer role](https://docs.cloud.google.com/iam/docs/roles-permissions/storage.md.txt#storage.bucketViewer) (`roles/storage.bucketViewer`)
   to all ServiceAccount objects in the target namespace. This role lets the
   ServiceAccount objects read bucket metadata, but doesn't grant them
   permissions to read or write the objects in the bucket.

   ```bash
   gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
       --member="principalSet://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/namespace/NAMESPACE" \
       --role="roles/storage.bucketViewer"
   ```

   Replace the following:
   - `BUCKET_NAME`: the name of your bucket.
   - `PROJECT_NUMBER`: your project number.
   - `PROJECT_ID`: your project ID.
   - `NAMESPACE`: the namespace for your Pods.
3. For each ServiceAccount object that needs to store Pod snapshots,
   follow these steps:

   1. Create a managed folder for the ServiceAccount object:

      ```bash
      gcloud storage managed-folders create gs://BUCKET_NAME/FOLDER_PATH/
      ```

      Replace the following:
      - `BUCKET_NAME`: the name of your bucket.
      - `FOLDER_PATH`: the path for the managed folder (for example, `my-app-snapshots`).
   2. Grant the ServiceAccount object the custom `podSnapshotGcsReadWriter`
      role on the managed folder. To grant permission, run the commands for
      your selected option:

      ### Standard permissions

      ```bash
      gcloud storage managed-folders add-iam-policy-binding gs://BUCKET_NAME/FOLDER_PATH/ \
          --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/SERVICEACCOUNT_NAME" \
          --role="projects/PROJECT_ID/roles/podSnapshotGcsReadWriter"
      ```

      ### Write-once permissions

      ```bash
      gcloud storage managed-folders add-iam-policy-binding gs://BUCKET_NAME/FOLDER_PATH/ \
          --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/SERVICEACCOUNT_NAME" \
          --role="projects/PROJECT_ID/roles/podSnapshotGcsReadWriter"
      ```

      ```bash
      gcloud storage managed-folders add-iam-policy-binding gs://BUCKET_NAME/FOLDER_PATH/ \
          --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/SERVICEACCOUNT_NAME" \
          --role="roles/storage.objectUser" \
          --condition='expression=resource.type == "storage.googleapis.com/Object" && (resource.name.extract("/pages.img_{part}") != "" || resource.name.extract("/fs_checkpoint_pages.img_{part}") != ""),title=allow_pcu_temp_part_cleanup,description=Allow deleting temporary parallel composite upload part objects only'
      ```

      Replace the following:
      - `BUCKET_NAME`: the name of your bucket.
      - `FOLDER_PATH`: the path for the managed folder (for example, `my-app-snapshots`).
      - `PROJECT_NUMBER`: your project number.
      - `PROJECT_ID`: your project ID.
      - `NAMESPACE`: the namespace for your Pods.
      - `SERVICEACCOUNT_NAME`: the name of your ServiceAccount object.

   > [!NOTE]
   > **Note:** IAM policies can take several minutes to take effect in GKE.

## Configure storage for snapshots

To specify where to store Pod snapshots, create a PodSnapshotStorageConfig
custom resource:

1. Save the following manifest as
   `example-pod-snapshot-storage-config.yaml`. This example configures
   GKE to store Pod snapshots in the
   `FOLDER_PATH/` path inside the
   Cloud Storage bucket `BUCKET_NAME`:

   ```yaml
   apiVersion: podsnapshot.gke.io/v1
   kind: PodSnapshotStorageConfig
   metadata:
     name: example-pod-snapshot-storage-config
   spec:
     snapshotStorageConfig:
       gcs:
         bucket: "BUCKET_NAME"
         path: "FOLDER_PATH"
         tokenSource: "TOKEN_SOURCE"
   ```

   Replace the following:
   - `BUCKET_NAME`: the name of your Cloud Storage bucket.
   - `FOLDER_PATH`: the path for the Cloud Storage managed folder.
   - `TOKEN_SOURCE`: the identity provider for
     access. Use `podKSA` (default) or `federatedP4SA` for multi-tenancy.

     > [!WARNING]
     > **Warning:** `podKSA` lets processes inside the container access the Cloud Storage bucket, which can be a security risk. Don't use `podKSA` if the Pod runs untrusted code, such as an agentic sandbox; use `federatedP4SA` instead.

2. Apply the manifest:

   ```bash
   kubectl apply -f example-pod-snapshot-storage-config.yaml
   ```

## What's next

Learn how to [trigger a Pod snapshot](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/pod-snapshots-trigger.md.txt).