1. Answers
  2. Persisting Data Using Kubernetes Persistent Volume Claims

Persisting Data Using Kubernetes Persistent Volume Claims

Introduction

In this guide, we will demonstrate how to use Pulumi to create a Kubernetes Persistent Volume (PV) and Persistent Volume Claim (PVC) in a Kubernetes cluster. Persistent Volumes are used to manage durable storage in a Kubernetes cluster, while Persistent Volume Claims are used by pods to request storage resources.

Step-by-Step Explanation

Step 1: Set up your Pulumi project

  1. Ensure you have the Pulumi CLI installed and configured.
  2. Create a new Pulumi project using TypeScript as the language.
  3. Configure your Pulumi project to use your Kubernetes cluster.

Step 2: Define the Persistent Volume

  1. Create a new TypeScript file (e.g., index.ts) in your Pulumi project.
  2. Import the necessary Pulumi and Kubernetes packages.
  3. Define the Persistent Volume resource with the required specifications (e.g., storage capacity, access modes, storage class).

Step 3: Define the Persistent Volume Claim

  1. In the same TypeScript file, define the Persistent Volume Claim resource.
  2. Specify the storage requirements and access modes for the PVC.
  3. Ensure the PVC references the appropriate storage class and matches the specifications of the PV.

Step 4: Deploy the resources

  1. Run pulumi up to deploy the Persistent Volume and Persistent Volume Claim to your Kubernetes cluster.
  2. Verify that the resources have been created successfully in your cluster.

Summary

By following these steps, you have successfully created and deployed a Persistent Volume and Persistent Volume Claim in your Kubernetes cluster using Pulumi. This setup allows your pods to request and use durable storage resources efficiently.

Full Code Example

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

// Define the Persistent Volume
const pv = new k8s.core.v1.PersistentVolume("pv", {
    spec: {
        capacity: {
            storage: "10Gi",
        },
        accessModes: ["ReadWriteOnce"],
        persistentVolumeReclaimPolicy: "Retain",
        storageClassName: "manual",
        hostPath: {
            path: "/mnt/data",
        },
    },
});

// Define the Persistent Volume Claim
const pvc = new k8s.core.v1.PersistentVolumeClaim("pvc", {
    spec: {
        accessModes: ["ReadWriteOnce"],
        resources: {
            requests: {
                storage: "10Gi",
            },
        },
        storageClassName: "manual",
    },
});

export const pvName = pv.metadata.name;
export const pvcName = pvc.metadata.name;

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