Skip to content

PersistentVolumeClaim

List Storage Classes

kubectl get sc

Example Claim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: replace_me
spec:
  storageClassName: replace_me
  accessModes:
    - ReadWriteOnce # (1)
  resources:
    requests:
      storage: 1Gi
  1. Avaiable Access Modes: ReadWriteOnce, ReadOnlyMany, ReadWriteMany, ReadWriteOncePod
    Link

Create And Attach PVC (Shell Script)

Caution

Highlighted Lines must be changed!

#!/bin/bash

PVC=$( mktemp /tmp/pvc-XXXXXX.yaml)
PATCH=$( mktemp /tmp/patch-XXXXXX.yaml)

read -p "Namespace?: " NS
read -p "PVC name: " PVC
read -p "PVC size (mb): " SIZE
read -p "Deployment?: " DEPLOY
read -p "Container?: " CONTAINER
read -p "Mount?: " MOUNT



cat <<EOF>$PVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: $PVC
  namespace: $NS
spec:
  accessModes:
    - ReadWriteOnce 
  resources:
    requests:
      storage: ${SIZE}Mi
  storageClassName: longhorn-one-replica
  volumeMode: Filesystem
EOF

kubectl apply -f $PVC 

cat <<EOF>$PATCH
spec:
  template:
    spec:
      volumes:
        - name: pvc-$PVC
          persistentVolumeClaim:
            claimName: $PVC
      containers:
        - name: $CONTAINER
          volumeMounts:
            - name: pvc-$PVC
              mountPath: $MOUNT
EOF


echo "Sleep 5 secs"
sleep 5
kubectl -n $NS patch deployment $DEPLOY --patch-file $PATCH 

Example Attach

1
2
3
4
5
    spec:
      volumes:
        - name: ohab-data
          persistentVolumeClaim:
            claimName: ohab-data
1
2
3
4
5
6
7
    spec:
      containers:
        - name: openhab3
          volumeMounts:
            - name: ohab-data
              mountPath: /openhab/addons
              subPath: addons

Last update: March 29, 2022