hostPath

hostPath acts as data volume in Docker. The local folder on a node listed in hostPath will be mounted into the Pod. Since the Pod can run on any nodes, read/write functions happening in the volume could explicitly exist in the node on which the Pod is running. In Kubernetes, however, the Pod should not be node-aware. Please note that the configuration and files might be different on different nodes when using hostPath. Therefore, the same Pod, created by the same command or configuration file, might act differently on different nodes.

By using hostPath, you're able to read and write the files between containers and localhost disks of nodes. What we need for volume definition is for hostPath.path to specify the target mounted folder on the node:

apiVersion: v1
# cat 2-6-2_hostPath.yaml
kind: Pod
metadata:
name: ubuntu
spec:
containers:
-
image: ubuntu
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
name: ubuntu
volumeMounts:
-
mountPath: /data-mount
name: data
volumes:
-
name: data
hostPath:
path: /tmp/data

Using docker inspect to check the volume details, you will see the volume on the host is mounted in the /data-mount destination:

"Mounts": [
{
"Type": "bind",
"Source": "/tmp/data",
"Destination": "/data-mount",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
...
]

If we run kubectl exec ubuntu touch /data-mount/sample, we should be able to see one empty file, named sample under /tmp/data, on the host.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.174.204