Workload outside kube-system uses hostPath

Using hostPath for ordinary applications broadens access to node files and requires strict limits on need and permissions.

Description

For ordinary applications, use a PVC for data, a ConfigMap for configuration or a Secret for sensitive values unless the app needs direct access to node files.

Potential impact

Files can differ across nodes. Where the mount and file permissions allow it, the application can read or change node logs or configuration.

Remediation

Limit system agents to the host paths they need. Set readOnly: true on the container mount for read-only tasks. Check who can create or modify the workload and who can read the logs.

Examples

Both excerpts omit the selector, matching Pod labels and volumeMounts. Supply them and a supported image for deployment.

Before

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default
spec:
  template:
    spec:
      containers:
        - name: nginx
          image: nginx:1.14.2
      volumes:
        - name: static-page-dir
          hostPath:
            path: /var/local/static
            type: DirectoryOrCreate

The Deployment in default declares the node’s /var/local/static directory as a volume. A container needs a matching mount to access it.

After

yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: kube-system
spec:
  template:
    spec:
      containers:
        - name: fluentd-elasticsearch
          image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
      volumes:
        - name: varlog
          hostPath:
            path: /var/log

The DaemonSet in kube-system declares /var/log for a log collector. Changing the namespace leaves the mounted files’ permissions unchanged.

References