TL;DR: A local persistent volume in a Kubernetes cluster is a persistent volume allocated in a certain node. Any pod mounting the volume will be automatically scheduled in the same node.
Since it’s limited to 1 node, clearly it can’t be used as a high-availability solution, but hey, it’s quite Ok to use it for experiments in a garage Kubernetes cluster.
Here’s a quick sample to provision a local PV(persistentVolume):
YAML
x
30
30
1
apiVersion v1
2
kind PersistentVolume
3
metadata
4
name prometheus-server-local
5
spec
6
capacity
7
storage 40Gi
8
volumeMode Filesystem
9
# it can only be attached to 1 pod, so...
10
accessModes
11
ReadWriteOnce
12
persistentVolumeReclaimPolicy Retain
13
storageClassName local-storage
14
# use this to ensure it being claimed correctly
15
claimRef
16
namespace prometheus
17
name prometheus-server
18
# this is the full path in the node
19
# I had to created this path myself before I can use it
20
local
21
path /mnt/prometheus-server
22
# select where the PV will be scheduled
23
nodeAffinity
24
required
25
nodeSelectorTerms
26
matchExpressions
27
key kubernetes.io/hostname
28
operator In
29
values
30
node7
To claim the PV, the PVC(persistentVolumeClaim) looks like this:
YAML
xxxxxxxxxx
1
12
12
1
apiVersion v1
2
kind PersistentVolumeClaim
3
metadata
4
name prometheus-server
5
namespace prometheus
6
spec
7
# this has to match the one in PV
8
accessModes
9
ReadWriteOnce
10
resources
11
requests
12
storage"40Gi"
And that’s it. The PV can be used by a pod now.
YAML
xxxxxxxxxx
1
12
12
1
apiVersion v1
2
kind Deployment
3
...
4
spec
5
replicas1
6
...
7
volumes
8
name storage-volume
9
persistentVolumeClaim
10
claimName prometheus-server
11
...
12