others-What's the difference between hostpath volume and the Local Persistent Volume?
Problem
What’s the difference between hostpath volume and the Local Persistent Volume? e.g. hostpath vs Local Pv in kubernetes.
We know that we can use host’s local directory to provision the kubernetes pods like this:
A
hostPath
volume mounts a file or directory from the host node’s filesystem into your Pod. This is not something that most Pods will need, but it offers a powerful escape hatch for some applications.For example, some uses for a
hostPath
are:
- running a container that needs access to Docker internals; use a
hostPath
of/var/lib/docker
- running cAdvisor in a container; use a
hostPath
of/sys
- allowing a Pod to specify whether a given
hostPath
should exist prior to the Pod running, whether it should be created, and what it should exist as
We can configure a pod to use hostpath volume like this:
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory
Whereas the Local Persistent Volume that is released from Kubernetes 1.14 is defined as follows:
A local persistent volume represents a local disk directly-attached to a single Kubernetes Node.
To use local pv, we must first define the storage class:
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
Then we can use the volumeClaimTemplates to claim the pv automatically:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: local-test
spec:
serviceName: "local-service"
replicas: 3
selector:
matchLabels:
app: local-test
template:
metadata:
labels:
app: local-test
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command:
- "/bin/sh"
args:
- "-c"
- "sleep 100000"
volumeMounts:
- name: local-vol
mountPath: /usr/test-pod
volumeClaimTemplates:
- metadata:
name: local-vol
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "local-storage"
resources:
requests:
storage: 368Gi
Pay attention to the storageClassName in the above yaml file, it must be the same as the StorageClass’s name.
Environment
- Kubernetest 1.14+
Difference
Let’s see the comparision of hostpath volume vs the Local Persistent Volume:
HostPath | Local Persistent Volume | |
---|---|---|
mount type | a file or directory | a local disk or partition |
k8s native | k8s scheduler does know the hostpath of the pod when it schedules a pod,a pod referencing a HostPath volume may be moved by the scheduler to a different node resulting in data loss. | the Kubernetes scheduler ensures that a pod using a Local Persistent Volume is always scheduled to the same node. |
use type | directly or with PV/PVC, it’s not secure if used directly because it can mount any directory on the host | ONLY PV/PVC, it can be controlled with access control. |