1. Answers
  2. Configuring a Kubernetes PersistentVolumeClaim

How do I configure a Kubernetes PersistentVolumeClaim?

In this guide, we will configure a Kubernetes PersistentVolumeClaim (PVC) using Pulumi. A PVC is a request for storage by a user that is used to dynamically provision a PersistentVolume (PV). This allows Kubernetes applications to request storage resources without needing to know the details of the underlying storage infrastructure.

Below is a Pulumi program written in TypeScript that demonstrates how to create a PVC in Kubernetes:

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

// Define the PersistentVolume
const pv = new k8s.core.v1.PersistentVolume("my-pv", {
    metadata: {
        name: "my-pv",
    },
    spec: {
        capacity: {
            storage: "1Gi",
        },
        accessModes: ["ReadWriteOnce"],
        persistentVolumeReclaimPolicy: "Retain",
        hostPath: {
            path: "/mnt/data",
        },
    },
});

// Define the PersistentVolumeClaim
const pvc = new k8s.core.v1.PersistentVolumeClaim("my-pvc", {
    metadata: {
        name: "my-pvc",
    },
    spec: {
        accessModes: ["ReadWriteOnce"],
        resources: {
            requests: {
                storage: "1Gi",
            },
        },
        volumeName: "my-pv",
    },
});

// Export the name of the PVC
export const pvcName = pvc.metadata.name;

Key Points

  1. PersistentVolume: This resource defines the actual storage resource in the cluster. In this example, we use a hostPath volume that points to /mnt/data on the host machine.
  2. PersistentVolumeClaim: This resource is a request for storage by a user. It specifies the desired size and access modes for the storage.
  3. Access Modes: The accessModes field in both the PV and PVC specifies how the volume can be mounted. In this example, ReadWriteOnce means the volume can be mounted as read-write by a single node.
  4. Volume Binding: The volumeName field in the PVC binds it to a specific PV by name.

Summary

In this guide, we configured a Kubernetes PersistentVolumeClaim using Pulumi. We defined both a PersistentVolume and a PersistentVolumeClaim, and bound them together. This setup allows Kubernetes applications to request and use storage resources dynamically.

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