You can use hostPath volumes to access read-write files on nodes. This can be useful for pods that can configure and monitor the host from the inside. You can also use hostPath volumes to mount volumes on the host using mountPropagation.
|
|
Using hostPath volumes can be dangerous, as they allow pods to read and write any file on the host. Proceed with caution.
|
It is recommended that you specify hostPath volumes directly in the Pod specification, rather than in a PersistentVolume object. This is useful because the pod already knows the path it needs to access when configuring nodes.
Procedure
-
Create a privileged pod:
apiVersion: v1
kind: Pod
metadata:
name: pod-name
spec:
containers:
...
securityContext:
privileged: true
volumeMounts:
- mountPath: /host/etc/motd.confg (1)
name: hostpath-privileged
...
volumes:
- name: hostpath-privileged
hostPath:
path: /etc/motd.confg (2)
| 1 |
The path used to mount the hostPath share inside the privileged pod. |
| 2 |
The path on the host that is used to share into the privileged pod. |
In this example, the pod can see the path of the host inside /etc/motd.confg as /host/etc/motd.confg. As a result, the motd can be configured without accessing the host directly.