K8S storage

Managing Storage

Using Pod Volumes

Managing Persistent Volumes

# pv.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv-volume
spec:
  storageClassName: demo
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mydata"

Configuring PersistentVolumeClaim

# pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pv-claim
spec:
  storageClassName: demo
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

A Pod uses the PVC by referencing it in its volumes section:

kind: Pod
apiVersion: v1
metadata:
  name: pv-pod
spec:
  volumes:
    - name: pv-storage
      persistentVolumeClaim:
        claimName: pv-claim
  containers:
    - name: pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: pv-storage

persistentVolumeReclaimPolicy

Understanding ConfigMap

Creating ConfigMaps for files

kubectl create cm mydir --from-file=/my/directory/

Mounting ConfigMaps

# spec.template.spec
volumes:
- name: cmvol
  configMap:
    name: webindex
# spec.template.spec.containers
volumeMounts:
- mountPath: /usr/share/nginx/html
  name: cmvol
# Demo: Creating a ConfigMap
echo "hello world" > index.html
kubectl create cm webindex --from-file=index.html
kubectl describe cm webindex
kubectl create deploy webserver --image=nginx
kubectl edit deploy webserver

Auto-provisioning storage

Understanding StorageClass

kubectl patch storageclass mysc -p '{"metadata":{"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

Using StorageClass

Storage Provisioners

Using an NFS Storage Provisioner

# Demo: Configuring a Storage Provisioner
# On control: install the NFS server
sudo apt install nfs-server -y
# On other nodes: install the NFS client
sudo apt install nfs-client
# On control: create and export the shared directory
sudo mkdir /nfsexport
sudo sh -c 'echo "/nfsexport *(rw,no_root_squash)" > /etc/exports'
sudo systemctl restart nfs-server
# On other nodes: verify the export is visible
showmount -e control

Then install the NFS subdir external provisioner with helm:

# Fetch the helm binary from github.com/helm/helm/releases and install it to /usr/local/bin
# Add the helm repo
helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
# Install the package (replace xx.xx.xx.yy with the NFS server IP)
helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner --set nfs.server=xx.xx.xx.yy --set nfs.path=/nfsexport
# Verify the nfs-subdir-provisioner Pod is running
kubectl get pods

Demo: Creating the PVC

# Verify that currently no PVs are available
kubectl get pv
# Create a PVC
kubectl apply -f nfs-provisioner-pvc-test.yaml
# Verify the PVC is created and bound to an automatically created PV
kubectl get pvc,pv
# Then create any Pod that mounts the PVC storage, and verify data ends up in the NFS share
# nfs-provisioner-pvc-test.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc-test
spec:
  storageClassName: nfs-client   # SAME NAME AS THE STORAGECLASS
  accessModes:
    - ReadWriteMany              # must be the same as PersistentVolume
  resources:
    requests:
      storage: 50Mi

Page Source