Sub topic 7
Giving Pods access to the host
This chapter assumes you can write a DaemonSet manifest and know how Pods, volumes, and security contexts work. Node-level agents such as log collectors, monitoring exporters, and network plugins often need to see the node itself. Kubernetes provides three common ways to do that: hostPath volumes, hostNetwork, and elevated privileges.
Why host access is different
A normal Pod is isolated. It has its own filesystem, its own network namespace, and no special rights on the node. Host access removes part of that isolation on purpose. Each option gives the agent more reach, and each one also gives a compromised or buggy container more reach.
| Option | What the Pod gets | Typical use |
|---|---|---|
| `hostPath` volume | A directory or file from the node's filesystem | Reading `/var/log`, talking to a runtime socket |
| `hostNetwork: true` | The node's network namespace and IP address | Network plugins, node-level exporters |
| `securityContext.privileged: true` | Nearly all Linux capabilities and device access | Storage and networking agents that change node state |
hostPath volumes
A hostPath volume mounts a path from the node into the container. The log agent from the earlier chapter uses it:
volumeMounts:
- name: varlog
mountPath: /host-logs
readOnly: true
volumes:
- name: varlog
hostPath:
path: /var/logTwo habits keep this safer:
- Set
readOnly: trueon the mount unless the agent must write. - Mount the narrowest path that works. Mount
/var/log, not/.
You can also set type on the hostPath to check what exists at the path. Directory requires an existing directory. DirectoryOrCreate creates it if it is missing. Socket requires a Unix socket, which suits runtime sockets.
volumes:
- name: runtime-sock
hostPath:
path: /run/containerd/containerd.sock
type: SocketThe path differs between nodes if the nodes run different operating systems or container runtimes. A DaemonSet that works on one node pool can fail on another.
hostNetwork
With hostNetwork: true, the Pod shares the node's network namespace. It sees the node's interfaces and uses the node's IP address. Any port the container opens is a port on the node.
spec:
template:
spec:
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNetSet dnsPolicy: ClusterFirstWithHostNet as well. Without it, a host-network Pod uses the node's DNS settings and cannot resolve cluster Service names.
Because two Pods cannot bind the same port on one node, a host-network DaemonSet can fail to start if another process already uses its port. Pick ports carefully.
If the agent only needs to know the node's address, you do not need hostNetwork. Use the downward API instead:
env:
- name: NODE_IP
valueFrom:
fieldRef:
fieldPath: status.hostIPPrivileges
Some agents must change the node, for example by loading kernel modules or editing network rules. Grant the least that works, in this order:
- Run as a non-root user with no extra rights where possible.
- Add only specific capabilities, such as
NET_ADMIN. - Use
privileged: trueonly as a last resort.
containers:
- name: agent
image: busybox:1.36
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
add: ["NET_ADMIN"]Restricted namespaces
Clusters that enforce the Pod Security Standards reject host access in namespaces using the baseline or restricted levels. Both forbid hostPath volumes, host networking, and privileged containers. Node agents therefore usually live in a namespace such as kube-system, or in a dedicated namespace labeled with the privileged level. Keep that namespace small and restrict who can deploy into it.
Exercise
A DaemonSet Pod only needs to read the node's logs under /var/log and report the node's IP address in its metrics. Which host access settings should you use?
Show answer
Use a read-only hostPath volume for /var/log, and get the node IP from the downward API with fieldRef status.hostIP. Do not enable hostNetwork or privileged, because the agent needs neither the node's network namespace nor extra privileges.
Exercise
You set hostNetwork: true and the Pod can no longer resolve a Service name such as my-service.default.svc. What setting fixes this?
Show answer
Set dnsPolicy: ClusterFirstWithHostNet in the Pod spec. Host-network Pods otherwise use the node's DNS configuration.