IT 3300 : Virtualization

Kubernetes Storage

Storage problems

  • Remember that files within a container are lost when the container crashes. A deployment can restart the pod (and containers) but the files are gone.
  • Also, how do you share files between pods

Volume (K8s volumes)

Yes, we did discuss docker volumes... but this is rather difficult to use in k8s when you have multiple containers, pods, nodes, etc...

Volume is just a directory, which is accessible to containers in a pod.

Volume

Most types volumes that you create will be related to the type of cloud provider that you are running on. In microk8s you can do a microk8s enable storage... which will just allocate storage from a host directory.

Persistent Volume

A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource. They are independent of a pod lifecycle.

Persistent Volume Claim

  • A request for storage by a user
  • As a pod can consume node resources, a PVC can consume PV resources

Example PV

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: task-pv-volume
      labels:
        type: local
    spec:
      storageClassName: manual
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: "/mnt/data"

Example PV Claim

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: task-pv-claim
    spec:
      storageClassName: manual
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 3Gi

Examples

Then,

  • kubectl get pv task-pv-volume
  • kubectl get pvc task-pv-claim

Create a pod that utilizes that PVC

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

Example

  • kubectl apply -f filename
  • kubectl exec -it task-pv-pod -- /bin/bash

Clean up

  • kubectl delete pod task-pv-pod
  • kubectl delete pvc task-pv-claim
  • kubectl delete pv task-pv-volume
  • sudo rm -rf /mnt/data

This may be useful

https://portworx.com/tutorial-kubernetes-persistent-volumes/