You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.0 KiB
77 lines
2.0 KiB
---
|
|
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.
|
|
|