1. Answers
  2. Rolling updates for stateful applications in Kubernetes

How do I perform rolling updates for stateful applications in Kubernetes?

In this guide, we will demonstrate how to perform rolling updates for stateful applications using Kubernetes StatefulSets. StatefulSets are used to manage stateful applications, and they ensure that the deployment and scaling of the pods happen in an ordered and predictable manner. We’ll configure a StatefulSet with a rolling update strategy to update the application pods one at a time.

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Define the namespace
const namespace = new k8s.core.v1.Namespace("app-namespace", {
    metadata: { name: "app-namespace" },
});

// Define the StatefulSet
const appLabels = { app: "my-stateful-app" };
const statefulSet = new k8s.apps.v1.StatefulSet("my-stateful-app", {
    metadata: {
        namespace: namespace.metadata.name,
        name: "my-stateful-app",
    },
    spec: {
        serviceName: "my-stateful-app",
        replicas: 3,
        selector: {
            matchLabels: appLabels,
        },
        template: {
            metadata: {
                labels: appLabels,
            },
            spec: {
                containers: [
                    {
                        name: "app",
                        image: "myapp:1.0.0",
                        ports: [{ containerPort: 80 }],
                    },
                ],
            },
        },
        volumeClaimTemplates: [
            {
                metadata: {
                    name: "app-storage",
                },
                spec: {
                    accessModes: ["ReadWriteOnce"],
                    resources: {
                        requests: {
                            storage: "1Gi",
                        },
                    },
                },
            },
        ],
        updateStrategy: {
            type: "RollingUpdate",
            rollingUpdate: {
                partition: 0, // Update all pods in the StatefulSet
            },
        },
    },
});

// Export the StatefulSet name
export const statefulSetName = statefulSet.metadata.name;

Key Points

  • Namespace: A Kubernetes namespace is created to isolate the resources.
  • StatefulSet: The StatefulSet is defined with labels, selectors, and a template for the pod specification.
  • Rolling Update Strategy: The update strategy is set to RollingUpdate which ensures that the pods are updated one at a time.
  • Volume Claim Templates: Persistent storage is defined for the StatefulSet using volume claim templates.

Summary

In this guide, we configured a Kubernetes StatefulSet with a rolling update strategy to manage stateful applications. This ensures that updates to the application are rolled out in an ordered and predictable manner, updating one pod at a time while maintaining the application state.

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