k8s persistent volume tut

master
Adam Veldhousen 4 years ago
parent c6dd7c36e1
commit e8a5b95932
Signed by: adam
GPG Key ID: 6DB29003C6DD1E4B

@ -1,7 +1,7 @@
---
title: "Kubernetes Loadbalancing with Metal LB"
date: 2020-01-02T20:57:36Z
tags: ["kubernetes", "homelab", "networking", "bare metal"]
tags: [kubernetes, homelab, networking, bare metal]
draft: false
---

@ -0,0 +1,76 @@
---
title: "How to setup a local persistent volume in kubernetes"
date: 2020-01-04T23:14:35Z
draft: false
tags: [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.capacity.storage`, `spec.hostPath.path` and change these accordingly.
```yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: persistent-test-volume
labels:
name: persistent-test-volume
spec:
volumeMode: Filesystem
storageClassName: standard
accessModes:
- ReadWriteOnce # type of access
capacity:
storage: 100Gi # Size of the volume
hostPath:
path: "/storage/volumes/test-volume"
```
Next you must create a *PersistentVolumeClaim (PVC)* to request access to the resources of the *PersistentVolume (PV)*.
```yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: persistent-test-volume-claim
spec:
volumeMode: Filesystem
storageClassName: standard
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Gi
selector:
matchLabels:
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`
```yaml
apiVersion: v1
kind: Pod
metadata:
name: pv-tester
namespace: default
spec:
restartPolicy: Never
containers:
- name: pv-tester
image: busybox
command: ["/bin/sh", "-c", "echo 'Hello volume' > /test_vol/hello.txt"]
volumeMounts:
- name: vol
mountPath: /test_vol
volumes:
- name: vol
persistentVolumeClaim:
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.
Loading…
Cancel
Save