sajad torkamani

What is a PersistentVolumeClaim (PVC)?

A PersistentVolumeClaim (PVC) is a storage resource that can be used by a Pod to request storage.

Example PersistentVolumeClaim YAML

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: php-storage
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

This creates a storage request for 1Gi of block storage.

To use the above PVC, you can create a Deployment resource like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-app
  template:
    metadata:
      labels:
        app: php-app
    spec:
      containers:
        - name: php
          image: php:8.2-cli
          command: ["php", "-S", "0.0.0.0:8080", "-t", "/app"]
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: php-persistent
              mountPath: /app
      volumes:
        - name: php-persistent
          persistentVolumeClaim:
            claimName: php-storage

This Deployment manifest:

  1. Defines a php-persistent volume using the persistentVolumeClaim which references our php-storage PVC claim.
  2. Mounts the php-persistent volume at /app so that any data added there persist across container starts.
Tagged: Kubernetes