Home Using Persistent Storage with Kubernetes (using hostPath)
Post
Cancel

Using Persistent Storage with Kubernetes (using hostPath)

Using persistent storage allows the pods to share storage. The example below will mount “/var/data” into each pod at “/usr/share/nginx/html”.

/var/data will be created on each node if it does not already exist. If your pods have replicas and are spread across multiple nodes, only the pods on the same node can share data. You could mount some NFS storage at /var/data then all pods that use the same NFS mount point can share data.

Source: https://github.com/kubernetes/examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-vol
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/var/data" # Will be created on each node. Could be an NFS mount on the node.
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-vol
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: webserver
spec:
  volumes:
    - name: storage-vol
      persistentVolumeClaim:
        claimName: pv-vol
  containers:
    - name: webserver
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: storage-vol
This post is licensed under CC BY 4.0 by the author.