DEV Community

Cheedge Lee
Cheedge Lee

Posted on • Originally published at Medium

Kubernets Storage Demos

In my previous post, shows the basic concepts for Storage in kubernetes, here I want to use some simplified demos to show how these concepts exactly used in a cluster.

Here we use one Emphemral volume -- HostPath type, and a Persistent Volume -- local type as two examples:

HostPath Type

HostPath Type is an empheral volume, even if it will keep the stored file after Pod deleted.

Main Difference:

  • No PVC, directly bound with Pod
  • Manually create affinity for Pod
apiVersion: v1 kind: PersistentVolume metadata: name: hostpath-pv spec: capacity: storage: 100Mi accessModes: - ReadWriteOnce hostPath: path: /mnt/data # if not exist will create --- apiVersion: v1 kind: Pod metadata: name: pod-using-hostpath spec: affinity: nodeAffinity: # host path should have the nodeAffinity item requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: In values: - node01 volumes: - name: hostpath-volume hostPath: path: /mnt/data containers: - name: app image: busybox command: ["sleep", "3600"] volumeMounts: - mountPath: /data name: hostpath-volume 
Enter fullscreen mode Exit fullscreen mode

Local Type

local type is Persistent Volume type, therefore we need create a PVC, and also with a StorageClass.

Main Difference:

  • PVC
  • SC
  • PV need nodeAffinity
  • Pod will be assigned according to the node of PV automatically
apiVersion: v1 kind: PersistentVolume metadata: name: local-pv spec: capacity: storage: 100Mi accessModes: - ReadWriteOnce storageClassName: local-storage # just a template local: path: /mnt/data # this should exist nodeAffinity: required: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: In values: - node01 --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: local-pvc spec: storageClassName: local-storage accessModes: - ReadWriteOnce resources: requests: storage: 50Mi --- apiVersion: v1 kind: Pod metadata: name: pod-using-local spec: volumes: - name: local-volume persistentVolumeClaim: claimName: local-pvc containers: - name: app image: busybox command: ["sleep", "3600"] volumeMounts: - mountPath: /data name: local-volume 
Enter fullscreen mode Exit fullscreen mode

Reuse the PV

If we have delete the PV, but still want to continue using the content stored, we need to specify the claimRef field and make sure the persistentVolumeReclaimPolicy field is Retain (for local type the official documnet DON'T recommend set persistentVolumeReclaimPolicy: Delete.
for more details of reuse the PV, pls check the official documents: here.

Top comments (0)