1. Answers
  2. Setting a Default StorageClass in Kubernetes

How do I set a default StorageClass in Kubernetes?

In this guide, we will set a default StorageClass in Kubernetes using Pulumi. A StorageClass provides a way for administrators to describe the “classes” of storage they offer. Different classes might map to quality-of-service levels, or to backup policies, or to arbitrary policies determined by the cluster administrators. Kubernetes uses a default StorageClass to dynamically provision persistent volumes when none is specified.

We will create a StorageClass and mark it as the default by adding the appropriate annotation.

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

// Create a StorageClass
const storageClass = new k8s.storage.v1.StorageClass("default-storage-class", {
    metadata: {
        name: "default-storage-class",
        annotations: {
            "storageclass.kubernetes.io/is-default-class": "true", // Mark this StorageClass as default
        },
    },
    provisioner: "kubernetes.io/aws-ebs", // Provisioner for AWS EBS
    parameters: {
        type: "gp2", // General Purpose SSD
    },
    reclaimPolicy: "Retain",
    volumeBindingMode: "Immediate",
});

export const storageClassName = storageClass.metadata.name;

Key Points

  • We imported the necessary Pulumi and Kubernetes packages.
  • We created a StorageClass resource with the kubernetes.io/aws-ebs provisioner.
  • We set the storageclass.kubernetes.io/is-default-class annotation to true to make this StorageClass the default.
  • We configured the parameters for the StorageClass, such as the type of EBS volume (gp2), reclaim policy (Retain), and volume binding mode (Immediate).

Conclusion

We successfully created and set a default StorageClass in Kubernetes using Pulumi. This StorageClass will be used by default when persistent volumes are dynamically provisioned and no other StorageClass is specified.

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