Kubernetes Views 7

Creating and mounting Kubernetes PVs and PVCs

Creating and mounting Kubernetes PVs and PVCs image 1

PV and PVC


Definitions

PV - Persistent Volume
PVC - Persistent Volume Claim

A PV is a volume that mounts a specific folder inside the container to a specific folder (physical disk) on the node.
When a container is deleted, every folder inside the container is deleted. If you mount a volume, the physical folder is not deleted when the container is deleted, so you can mount it again when you restart the container.

A PVC is what you use when you mount a PV on a Deployment, and it acts as the link between them.
So the usual flow is: create a PV, create a PVC, confirm the binding state, then mount the volume from the Deployment using the PVC.


yaml Example

  • pv
     apiVersion: v1
     kind: PersistentVolume
     metadata:
       name: testpv
     spec:
       capacity:
         storage: 2Gi
       volumeMode: Filesystem
       accessModes:
       - ReadWriteOnce
       storageClassName: manual
       persistentVolumeReclaimPolicy: Delete
       hostPath:
         path: /mnt/test # this means it is mounted on /mnt/test on the host node. The host node is the worker node where the container runs
  • pvc
     apiVersion: v1
     kind: PersistentVolumeClaim
     metadata:
       name: testpvc
     spec:
       accessModes:
       - ReadWriteOnce
       volumeMode: Filesystem
       resources:
         requests:
           storage: 2Gi
       storageClassName: manual

Apply the two files above first, then apply them in the deploy.yaml file and deploy.
I will try applying nginx.yaml as a test.

  • nginx.yaml
     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: nginx-deploy
     spec:
       selector:
         matchLabels:
           run: my-nginx
       replicas: 1
       template:
         metadata:
           labels:
             run: my-nginx
         spec:
           containers:
           - name: my-nginx-container
             image: nginx:1.9.1
             imagePullPolicy: Always
             volumeMounts:
               - name: pv
                 mountPath: /mnt
             ports:
             - containerPort: 80
           volumes:
             - name: pv
               persistentVolumeClaim:
                 claimName: testpvc

Reactions

Comments 0

A nickname and a password are required. The same password edits or deletes the comment later. Comments appear after a review.

Comments appear after a review. The password is used again to edit or delete.