In the previous post, we learned how to setup an NFS share on a Linux machine. Today, we are going to learn how to mount this share in a Kubernetes cluster.
The first thing we need to define is a PersistentVolume
:
persistent-volume.yaml
1apiVersion: v1
2kind: PersistentVolume
3metadata:
4 name: my-shared-folder-pv
5 labels:
6 usage: my-shared-folder-pv
7spec:
8 capacity:
9 storage: 50Gi
10 accessModes:
11 - ReadWriteMany
12 persistentVolumeReclaimPolicy: Recycle
13 nfs:
14 server: my-other-server
15 path: /var/nfs/my_shared_folder
Then we can create a PersistentVolumeClaim
pointing to the volume:
persistent-volume-claim.yaml
1apiVersion: v1
2kind: PersistentVolumeClaim
3metadata:
4 name: my-shared-folder-pvc
5 annotations:
6 volume.beta.kubernetes.io/storage-class: ""
7spec:
8 accessModes:
9 - ReadWriteMany
10 resources:
11 requests:
12 storage: 50Gi
13 selector:
14 matchLabels:
15 usage: my-shared-folder-pv
We can now deploy these to our Kubernetes cluster:
1kubectl apply -f persistent-volume.yaml
2kubectl apply -f persistent-volume-claim.yaml
To use it in a deployment, you can mount it now as a volume:
deployment.yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: my-server
5 labels:
6 app: my-server
7spec:
8 replicas: 1
9selector:
10 matchLabels:
11 app: my-server
12 template:
13 metadata:
14 labels:
15 app: my-server
16 spec:
17 containers:
18 - name: my-server
19 image: "alpine:3.12"
20 command: ["/bin/sh"]
21 args: ["-c", "while true; do date >> /mnt/my_shared_folder/dates.txt; sleep 5; done"]
22 volumeMounts:
23 - name: my-shared-folder
24 mountPath: /mnt/my_shared_folder
25 volumes:
26 - name: my-shared-folder
27 persistentVolumeClaim:
28 claimName: my-shared-folder-pvc
If you also want to setup the NFS share itself inside the cluster, there are examples available showing you how to do that in the Kubernetes Example repository.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.