This page looks best with JavaScript enabled

Using emptyDir, hostPath, and localVolume in Kubernetes

 ·  ☕ 4 min read

Earlier, in Kubernetes Volumes, we got some understanding of Volumes. This post focuses on practice, learning how to use the three local storage options: emptydir, hostpath, and localvolume.

1. Basic Properties of a PV

1.1 PV Lifecycle

PV states:

  • Available: available, not yet bound by any PVC
  • Bound: already bound to a PVC
  • Released: the PVC was deleted, but the resource has not yet been reclaimed
  • Failed: automatic reclamation failed

1.2 PV Reclaim Policy

PersistentVolumeReclaimPolicy, i.e. the PV’s reclaim policy.

What to do when a Pod no longer needs the PV:

  • Retain: keep the data, the administrator must clean it up manually
  • Recycle: reclaim the resource, clearing the data in the PV
  • Delete: delete the PV directly

Currently only NFS and HostPath type volumes support reclaim policies, while AWS EBS, GCE PD, Azure Disk, and Cinder support the Delete policy.

1.3 PV Access Modes

PV access modes (accessModes):

  • ReadWriteOnce: the PV is mounted read-write to a single Pod
  • ReadWriteMany: the PV is mounted read-write to multiple Pods
  • ReadOnlyMany: the PV is mounted read-only to multiple Pods

2. emptyDir

When using emptyDir, Kubernetes automatically allocates a directory for the Pod on the Node. The directory starts out empty, and when the Pod is removed from the Node, the data in the emptyDir is removed as well.

It is mainly used for temporary directories that do not need to be persisted, shared directories between multiple containers, and similar scenarios.

Create the file emptydir.yaml

 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
apiVersion: v1
kind: Pod
metadata:
  name: emptydir
spec:
  containers:
    - image: busybox
      name: app
      volumeMounts:
        - mountPath: /logs
          name: shared-dir
      args:
        - /bin/sh
        - -c
        - echo emptydir >> /logs/app.log; sleep 60000
    - image: busybox
      name: log-collector
      volumeMounts:
        - mountPath: /app_logs
          name: shared-dir
      args:
        - /bin/sh
        - -c
        - cat /app_logs/app.log; sleep 60000
  volumes:
    - name: shared-dir
      emptyDir: {}

Run the commands:

1
2
3
kubectl apply -f emptydir.yaml
kubectl exec emptydir -c log-collector cat /app_logs/app.log
emptydir

You can see that file sharing between the two containers works.

3. hostPath

The hostPath type maps a file or directory from the Node’s filesystem into a Pod. Supported types are files, directories, File, Socket, CharDevice, and BlockDevice.

Create the file hostpath.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1
kind: Pod
metadata:
  name: pod-hostpath
spec:
  nodeSelector:
    kubernetes.io/hostname: i-6fns0nua
  containers:
    - image: busybox
      name: app
      volumeMounts:
        - mountPath: /logs
          name: shared-dir
      args:
        - /bin/sh
        - -c
        - echo hostpath >> /logs/app.log; sleep 60000
  volumes:
    - name: shared-dir
      hostPath:
        path: /data/logs
  1. Log in to host i-6fns0nua and create the directory
1
mkdir -p /data/logs
  1. Create the hostpath
1
kubectl apply -f pod.yaml
  1. Log in to host i-6fns0nua and view the shared file
1
2
cat /data/logs/app.log
hostpath

4. local volume

Scenarios that suit local volume:

  • Data caching, where applications can access data nearby and process it quickly.
  • Distributed storage systems, such as distributed databases and distributed file systems.

4.1 Creating Static Storage

Create the file sc.yaml

1
2
3
4
5
6
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

pv.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local1
spec:
  capacity:
    storage: 30Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local
  local:
    path: /data/local1
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - i-6fns0nua

4.2 Using It in a Pod

Create the file pvc.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc1
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 30Gi

Kubernetes automatically matches the PV and the PVC. Below, the storage is used inside a Pod through the PVC.

pod.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
kind: Pod
apiVersion: v1
metadata:
  name: pod1
spec:
  containers:
    - name: nginx1
      image: nginx
      volumeMounts:
        - mountPath: "/var/www/html"
          name: pod1
  volumes:
    - name: pod1
      persistentVolumeClaim:
        claimName: pvc1

微信公众号
WRITTEN BY
微信公众号