1. Answers
  2. Kubernetes StatefulSet with Persistent Storage

How do I create persistent storage for each pod with StatefulSet in Kubernetes?

In this example, we will create a Kubernetes StatefulSet with persistent storage for each pod using Pulumi. A StatefulSet ensures that each pod has a unique, stable identity and storage that persists across rescheduling. We will use PersistentVolumeClaims (PVCs) to provide persistent storage for each pod in the StatefulSet.

Key Points

  • StatefulSet: Manages the deployment and scaling of a set of pods, and provides guarantees about the ordering and uniqueness of these pods.
  • PersistentVolumeClaim: A request for storage by a user that can be used by pods.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a namespace for the StatefulSet
const ns = new k8s.core.v1.Namespace("mynamespace");

// Define a PersistentVolumeClaim template
const pvc = new k8s.core.v1.PersistentVolumeClaim("mypvc", {
    metadata: {
        namespace: ns.metadata.name,
    },
    spec: {
        accessModes: ["ReadWriteOnce"],
        resources: {
            requests: {
                storage: "1Gi",
            },
        },
    },
});

// Define the StatefulSet
const statefulSet = new k8s.apps.v1.StatefulSet("mystatefulset", {
    metadata: {
        namespace: ns.metadata.name,
    },
    spec: {
        serviceName: "mystatefulset",
        replicas: 3,
        selector: {
            matchLabels: {
                app: "mystatefulset",
            },
        },
        template: {
            metadata: {
                labels: {
                    app: "mystatefulset",
                },
            },
            spec: {
                containers: [
                    {
                        name: "nginx",
                        image: "nginx:1.19.6",
                        ports: [
                            {
                                containerPort: 80,
                                name: "http",
                            },
                        ],
                        volumeMounts: [
                            {
                                name: "mypersistentstorage",
                                mountPath: "/usr/share/nginx/html",
                            },
                        ],
                    },
                ],
            },
        },
        volumeClaimTemplates: [
            {
                metadata: {
                    name: "mypersistentstorage",
                },
                spec: pvc.spec,
            },
        ],
    },
});

Summary

In this example, we created a Kubernetes StatefulSet with persistent storage for each pod using Pulumi. We defined a PersistentVolumeClaim template and used it in the StatefulSet to ensure each pod gets its own persistent storage. The StatefulSet ensures that each pod has a unique and stable identity, and the storage persists across rescheduling.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up