How to setup a local persistent volume in kubernetes

kubernetes, configuration, storage

I’m running a single node kubernetes cluster and one of the first things I needed was persistent storage. To create a volume that you can mount into your containers in a pod you have to create a PersistentVolume (PV) and then request it with a PersistentVolumeClaim (PVC).

Create a PersistentVolume (PV) object, pointing at a path on your host. Note the, spec.hostPath.path and change these accordingly.

 1apiVersion: v1
 2kind: PersistentVolume
 4  name: persistent-test-volume
 5  labels:
 6    name: persistent-test-volume
 8  volumeMode: Filesystem
 9  storageClassName: standard
10  accessModes:
11    - ReadWriteOnce # type of access
12  capacity:
13    storage: 100Gi # Size of the volume
14  hostPath:
15    path: "/storage/volumes/test-volume"

Next you must create a PersistentVolumeClaim (PVC) to request access to the resources of the PersistentVolume (PV).

 1apiVersion: v1
 2kind: PersistentVolumeClaim
 4  name: persistent-test-volume-claim
 6  volumeMode: Filesystem
 7  storageClassName: standard
 8  accessModes:
 9    - ReadWriteOnce
10  resources:
11    requests:
12      storage: 100Gi
13  selector:
14    matchLabels:
15      name: persistent-test-volume

Now that we’ve set these two resources up, we can create a pod with a container that references the PVC we made above in the spec.volumes

 1apiVersion: v1
 2kind: Pod
 4  name: pv-tester
 5  namespace: default
 7  restartPolicy: Never
 8  containers:
 9    - name: pv-tester
10      image: busybox
11      command: ["/bin/sh", "-c", "echo 'Hello volume' > /test_vol/hello.txt"]
12      volumeMounts:
13        - name: vol
14          mountPath: /test_vol
15  volumes:
16    - name: vol
17      persistentVolumeClaim:
18        claimName: persistent-test-volume-claim

You now should be able to see the hello.txt file at the path /storage/volumes/ on the host machine.

Articles from blogs I follow

My plans at FOSDEM: SourceHut, Hare, and Helios

FOSDEM is right around the corner, and finally in person after long years of dealing with COVID. I’ll be there again this year, and I’m looking forward to it! I have four slots on the schedule (wow! Thanks for arranging these, FOSDEM team) and I’ll be talkin…

via Drew DeVault's blog January 24, 2023

YSK: Google allows spoofing news headlines in search results

A minor scandal unfolding in the Swedish election highlights a way to influence news narratives: Google allows you to set headlines for news articles in search results by paying for adwords placements of legitimate articles. This is being used by political …

via Jacob Davis-Hansson September 9, 2022

Going multipath without Multipath TCP

Going multipath without Multipath TCP Gigabit ethernet has been around for a long time, it’s so ubiquitous that there is a very strong chance that if you have a RJ-45 port on your compu

via benjojo blog February 24, 2022

Generated by openring