Introduction
Kubernetes 1.14: Local Persistent Volumes GA Recap: A local persistent volume represents a local disk directly-attached to a single Kubernetes Node. With the Local Persistent Volume plugin, Kubernetes workloads can now consume high performance local storage using the same volume APIs that app developers have become accustomed to.
一个和hostPath的重要区别: The biggest difference is that the Kubernetes scheduler understands which node a Local Persistent Volume belongs to. With HostPath volumes, a pod referencing a HostPath volume may be moved by the scheduler to a different node resulting in data loss. But with Local Persistent Volumes, the Kubernetes scheduler ensures that a pod using a Local Persistent Volume is always scheduled to the same node.
While HostPath volumes may be referenced via a Persistent Volume Claim (PVC) or directly inline in a pod definition, Local Persistent Volumes can only be referenced via a PVC. This provides additional security benefits since Persistent Volume objects are managed by the administrator, preventing Pods from being able to access any path on the host.
Additional benefits include support for formatting of block devices during mount, and volume ownership using fsGroup.
注意: 实际上emptyDir + fsGroup也可以实现类似hostPath的效果,emptyDir用的是/sysroot
(RedHat Linux), 比如多个pods 使用emptyDir在同一个Node, 我在各自的emptyDir中touch了一个file: compute-0 和compute-3, 进入Node使用find command就可以看到了:
1 | /sysroot/ostree/deploy/rhcos/var/lib/kubelet/pods/68e65ed4-4e62-4588-9269-8947dea9dd46/volumes/kubernetes.io~empty-dir/compute-dedicated-scratch/compute-0 |
还需要注意的是,local storage provisioning 在每个node上只会provision attach的disk个数一样的PV,并且这个PV会被一个PVC占据,尽管PV大小是500G但是PVC只请求5G。(不知道这个以后是否会有改进)
Steps
Only test on OCP
4.3
version
OpenShift persistent storage using local volumes Deploy local-storage Operator
- install local storage operator (it is by default set in
local-storage
namespace) - provision the local storage
- create local volume persistentVolumeClaim and attach to pod
After deploy the operator, then
1 | ## get hostname of each worker node |
1 | apiVersion: "local.storage.openshift.io/v1" |
For example:
1 | blkid |
let’s check /dev/vdc
1 | lsblk /dev/vdc |
After apply the CR LocalVolume
, let’s check local-storage
namespace status, you should see lcoal diskmaker and provisioner pods are up and running, corresponding PVs are ready as well.
1 | NAME READY STATUS RESTARTS AGE |
If things are all set, we can consume the local storage provisioned by local-sc. Here I use volumeClaimTemplates
instead of create separate PVC (这里应该不能使用分开的PVC,因为PVC的创建和pod位于的node有关,事先并不知道).
Notice that if there is one PV per node, then one PVC will consume the whole PV. So if use statefulset with volume claim template, we will only have one pod per node.
1 | apiVersion: apps/v1 |
Now let’s check /dev/vdc
again by lsblk
, you will see it is associated with the pod.