1. Answers
  2. Implementing shared file systems in Kubernetes with CephFS

How do I implement shared file systems in Kubernetes with CephFS?

In this guide, we will implement shared file systems in Kubernetes using CephFS. CephFS is a POSIX-compliant file system that uses the Ceph storage cluster to store data. It allows multiple pods to share the same storage, making it ideal for applications that require shared access to data.

We will use Pulumi to define and deploy the necessary Kubernetes resources, including the CephFS StorageClass, PersistentVolume, and PersistentVolumeClaim. Here’s how to do it:

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

// Define the CephFS StorageClass
const cephfsStorageClass = new k8s.storage.v1.StorageClass("cephfs-storage-class", {
    metadata: {
        name: "cephfs",
    },
    provisioner: "ceph.com/cephfs",
    parameters: {
        monitors: "mon1:6789,mon2:6789,mon3:6789",
        adminId: "admin",
        adminSecretName: "ceph-secret",
        adminSecretNamespace: "default",
        pool: "cephfs_data",
        fsName: "myfs",
    },
});

// Define the CephFS PersistentVolume
const cephfsPersistentVolume = new k8s.core.v1.PersistentVolume("cephfs-pv", {
    metadata: {
        name: "cephfs-pv",
    },
    spec: {
        capacity: {
            storage: "10Gi",
        },
        accessModes: ["ReadWriteMany"],
        persistentVolumeReclaimPolicy: "Retain",
        storageClassName: "cephfs",
        cephfs: {
            monitors: ["mon1:6789", "mon2:6789", "mon3:6789"],
            path: "/",
            user: "admin",
            secretRef: {
                name: "ceph-secret",
                namespace: "default",
            },
        },
    },
});

// Define the CephFS PersistentVolumeClaim
const cephfsPersistentVolumeClaim = new k8s.core.v1.PersistentVolumeClaim("cephfs-pvc", {
    metadata: {
        name: "cephfs-pvc",
    },
    spec: {
        accessModes: ["ReadWriteMany"],
        resources: {
            requests: {
                storage: "10Gi",
            },
        },
        storageClassName: "cephfs",
    },
});

// Export the names of the resources
export const storageClassName = cephfsStorageClass.metadata.name;
export const persistentVolumeName = cephfsPersistentVolume.metadata.name;
export const persistentVolumeClaimName = cephfsPersistentVolumeClaim.metadata.name;

Key Points

  • We defined a StorageClass named cephfs that uses the CephFS provisioner.
  • We created a PersistentVolume named cephfs-pv with a capacity of 10Gi and set it to use the cephfs StorageClass.
  • We defined a PersistentVolumeClaim named cephfs-pvc to request storage from the cephfs StorageClass.

Summary

In this guide, we implemented shared file systems in Kubernetes using CephFS by defining a StorageClass, PersistentVolume, and PersistentVolumeClaim with Pulumi. This setup allows multiple pods to share the same storage, facilitating applications that require shared access to data.

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