dhi.io/kube-bench
kube-bench checks whether a Kubernetes cluster is deployed securely by running the checks documented in the CIS Kubernetes Benchmark against its nodes.
All examples in this guide use the public image. If you've mirrored the repository for your own use (for example, to your Docker Hub namespace), update your commands to reference the mirrored image instead of the public one.
For example:
dhi.io/<repository>:<tag><your-namespace>/dhi-<repository>:<tag>For the examples, you must first use docker login dhi.io to authenticate to the registry to pull the images.
kube-bench runs the checks documented in the
CIS Kubernetes Benchmark against the node it runs on. Replace
<tag> with the image variant you want to run.
docker run --rm dhi.io/kube-bench:<tag> version
docker run --rm dhi.io/kube-bench:<tag> --help
kube-bench runs inside a Pod that has the host's PID namespace and read-only mounts of the host directories holding the
Kubernetes configuration; upstream ships this as a Kubernetes Job (upstream README.md). The manifest below is
upstream's job-node.yaml with five changes:
image: points at this image.runAsUser: 0 and runAsGroup: 0. Upstream's image runs as root by default; this one runs as UID 65532, see "Run as
root".securityContext with allowPrivilegeEscalation: false (no process in the container can gain more
privileges than it started with), capabilities.drop: [all] (root keeps none of the Linux capabilities it normally
has) and readOnlyRootFilesystem: true (the container can write only to mounted volumes, so --outputfile needs
one). Upstream's manifest sets none of these.backoffLimit: 0, so a Pod that exits with a non-zero code (see --exit-code below) is not started again.hostPath mount of /var/lib/rancher. k3s and RKE2 keep their kubeconfigs, certificates and keys under
it (/var/lib/rancher/k3s/agent/kubelet.kubeconfig, .../server/cred/admin.kubeconfig, ...), and the k3s-cis-* and
rke2-cis-* benchmarks read those paths. Upstream's node Job does not mount it, so on those distributions the
kubeconfig and certificate authority file permission and ownership checks report FAIL with an empty result.Save the manifest as kube-bench-node.yaml:
apiVersion: batch/v1
kind: Job
metadata:
name: kube-bench-node
spec:
backoffLimit: 0
template:
spec:
hostPID: true
restartPolicy: Never
securityContext:
runAsUser: 0
runAsGroup: 0
containers:
- name: kube-bench
image: dhi.io/kube-bench:<tag>
command: ["kube-bench", "run", "--targets", "node"]
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- all
readOnlyRootFilesystem: true
volumeMounts:
- name: var-lib-cni
mountPath: /var/lib/cni
readOnly: true
- name: var-lib-etcd
mountPath: /var/lib/etcd
readOnly: true
- name: var-lib-kubelet
mountPath: /var/lib/kubelet
readOnly: true
- name: var-lib-kube-scheduler
mountPath: /var/lib/kube-scheduler
readOnly: true
- name: var-lib-kube-controller-manager
mountPath: /var/lib/kube-controller-manager
readOnly: true
# only needed on k3s and RKE2
- name: var-lib-rancher
mountPath: /var/lib/rancher
readOnly: true
- name: etc-systemd
mountPath: /etc/systemd
readOnly: true
- name: lib-systemd
mountPath: /lib/systemd
readOnly: true
- name: srv-kubernetes
mountPath: /srv/kubernetes
readOnly: true
- name: etc-kubernetes
mountPath: /etc/kubernetes
readOnly: true
- name: usr-bin
mountPath: /usr/local/mount-from-host/bin
readOnly: true
- name: etc-cni-netd
mountPath: /etc/cni/net.d
readOnly: true
- name: opt-cni-bin
mountPath: /opt/cni/bin
readOnly: true
volumes:
- name: var-lib-cni
hostPath:
path: /var/lib/cni
- name: var-lib-etcd
hostPath:
path: /var/lib/etcd
- name: var-lib-kubelet
hostPath:
path: /var/lib/kubelet
- name: var-lib-kube-scheduler
hostPath:
path: /var/lib/kube-scheduler
- name: var-lib-kube-controller-manager
hostPath:
path: /var/lib/kube-controller-manager
# only needed on k3s and RKE2
- name: var-lib-rancher
hostPath:
path: /var/lib/rancher
- name: etc-systemd
hostPath:
path: /etc/systemd
- name: lib-systemd
hostPath:
path: /lib/systemd
- name: srv-kubernetes
hostPath:
path: /srv/kubernetes
- name: etc-kubernetes
hostPath:
path: /etc/kubernetes
- name: usr-bin
hostPath:
path: /usr/bin
- name: etc-cni-netd
hostPath:
path: /etc/cni/net.d
- name: opt-cni-bin
hostPath:
path: /opt/cni/bin
The Job completes successfully even when checks report FAIL, which is kube-bench's default. Add --exit-code <n> to the
command to make it exit with that code when one or more checks FAIL (upstream's documentation uses 42 as the example);
WARN results do not count as failures.
Then run it and read the report from the Pod logs:
kubectl apply -f kube-bench-node.yaml
kubectl wait --for=condition=complete --timeout=120s job/kube-bench-node
kubectl logs job/kube-bench-node
With --exit-code, a run with failing checks leaves the Job Failed rather than Complete, so the kubectl wait
above times out; kubectl get job kube-bench-node shows which, and kubectl logs prints the report either way.
For the other targets and for EKS, GKE, AKS, ACK, TKGI and OpenShift, start from upstream's
job-master.yaml,
job.yaml or the platform manifests (job-eks.yaml,
job-gke.yaml, ...) and apply the same five changes; see upstream's
running documentation.
The image runs as the nonroot user (UID 65532), and the manifest above overrides that with runAsUser: 0. kube-bench
stops with error looking for file <path>: stat <path>: permission denied as soon as one of the configuration files it
probes is in a directory only root may enter, which is the case on k3s (its agent directory /var/lib/rancher/k3s/agent
is mode 0700). The upstream image runs as root; run the Job as root for the same coverage. Nonroot is sufficient when
every path the selected benchmark inspects is readable by the container user, for example a custom benchmark against
files you control (see "Customize the benchmark definitions").
By default kube-bench maps the detected Kubernetes version and platform to a benchmark. Pass --benchmark <name> or
--version <kubernetes-version> (never both) to choose it yourself; see upstream's
flags and commands.
kube-bench reads its benchmark definitions from /etc/kube-bench/cfg (the default of --config-dir).
/opt/kube-bench/cfg is a symlink to it, so upstream's documented overrides keep working: a config.yaml mounted over
/opt/kube-bench/cfg/config.yaml or a directory mounted over /opt/kube-bench/cfg/<benchmark> ends up in
/etc/kube-bench/cfg, whether mounted with docker run -v or as a volume in a Pod.
Because the definitions are under /etc, mounting the node's /etc over the container's /etc, as upstream's Docker
example does, hides them and kube-bench stops with config file is missing 'target_mapping' section (or
'version_mapping' section when it detects no platform and no --benchmark is given). Mount the individual directories
the Job manifest above lists instead.
To run the node checks against the Docker host itself, share the host PID namespace and mount the directories the benchmark inspects read-only:
docker run --rm --pid=host --user 0 \
-v /etc/kubernetes:/etc/kubernetes:ro \
-v /var/lib/kubelet:/var/lib/kubelet:ro \
-v /etc/systemd:/etc/systemd:ro \
-v /lib/systemd:/lib/systemd:ro \
-v /usr/bin:/usr/local/mount-from-host/bin:ro \
dhi.io/kube-bench:<tag> run --targets node
--user 0 lets kube-bench enter directories only root may enter, as the Job's runAsUser: 0 does; see "Run as root"
above. Outside a Pod kube-bench cannot ask the API server for the Kubernetes version, so pass --benchmark (see "Choose
the benchmark"); on k3s, also mount /var/lib/rancher:/var/lib/rancher:ro, as the Job does.
The entrypoint is kube-bench, so any arguments are passed to it directly. Upstream's entrypoint.sh and its
install mode, which copies the binary and benchmark definitions to a mounted /host directory so kube-bench can run
on the node itself, are not included. Some benchmark audits call commands that neither this image nor upstream's
ships: journalctl (k3s-cis-*, cis-1.6-k3s), oc (rh-1.*), yq (rh-1.4, rh-1.8), gcloud (gke-1.9.0)
and systemctl (eks-stig-kubernetes-v1r6). In a container those checks fail or fall back to a default value instead
of reading the node. To run them, copy the files out and run them on the node with --config-dir; the binary is
statically linked, so it needs no libraries from the node, and the image is built for amd64 and arm64:
docker run --rm --user 0 -v "$PWD":/host --entrypoint /bin/sh dhi.io/kube-bench:<tag> \
-c 'cp /usr/bin/kube-bench /host/ && cp -r /etc/kube-bench/cfg /host/cfg'
sudo ./kube-bench run --config-dir ./cfg --targets node
The benchmark definitions are under /etc/kube-bench/cfg, so the node's /etc cannot be mounted over the container's
/etc; see "Customize the benchmark definitions".
Every variant ships /bin/sh, bash and the common tools the audits call (ps, grep, awk, sed, stat,
find, xargs, jq, kubectl, openssl), because kube-bench runs every check by piping a shell script into
/bin/sh. The runtime variants still run as nonroot and have no package manager.
Use the FIPS variant when your deployment environment requires FIPS 140-compliant cryptographic operations. The FIPS
build covers kube-bench's own TLS client to the API server, the kubectl binary from the kubectl-1.36-fips package
and the openssl binary in the image; binaries run from /usr/local/mount-from-host/bin come from the node and are not
covered by the FIPS build. The FIPS module is selected at build time and the binaries run with fips140=on by default.
To make the Go binaries refuse any algorithm outside the validated module, set GODEBUG=fips140=only in the container's
env:
env:
- name: GODEBUG
value: fips140=only
Docker Hardened Images come in different variants depending on their intended use. Image variants are identified by their tag.
Runtime variants are designed to run your application in production. These images are intended to be used either directly or as the FROM image in the final stage of a multi-stage build. These images typically:
Build-time variants typically include dev in the tag name and are intended for use in the first stage of a
multi-stage Dockerfile. These images typically:
FIPS variants include fips in the variant name and tag. They come in both runtime and build-time variants. These
variants use cryptographic modules that have been validated under FIPS 140, a U.S. government standard for secure
cryptographic operations. For example, usage of MD5 fails in FIPS variants.
To view the image variants and get more information about them, select the Tags tab for this repository, and then select a tag.
To migrate your application to a Docker Hardened Image, you must update your Dockerfile. At minimum, you must update the base image in your existing Dockerfile to a Docker Hardened Image. This and a few other common changes are listed in the following table of migration notes.
| Item | Migration note |
|---|---|
| Base image | Replace your base images in your Dockerfile with a Docker Hardened Image. |
| Package management | Non-dev images, intended for runtime, don't contain package managers. Use package managers only in images with a dev tag. |
| Non-root user | By default, non-dev images, intended for runtime, run as the nonroot user. Ensure that necessary files and directories are accessible to the nonroot user. |
| Multi-stage build | Utilize images with a dev tag for build stages and non-dev images for runtime. For binary executables, use a static image for runtime. |
| TLS certificates | Docker Hardened Images contain standard TLS certificates by default. There is no need to install TLS certificates. |
| Ports | Non-dev hardened images run as a nonroot user by default. As a result, applications in these images can't bind to privileged ports (below 1024) when running in Kubernetes or in Docker Engine versions older than 20.10. To avoid issues, configure your application to listen on port 1025 or higher inside the container. |
| Entry point | Docker Hardened Images may have different entry points than images such as Docker Official Images. Inspect entry points for Docker Hardened Images and update your Dockerfile if necessary. |
| No shell | By default, non-dev images, intended for runtime, don't contain a shell. Use dev images in build stages to run shell commands and then copy artifacts to the runtime stage. |
The following steps outline the general migration process.
Find hardened images for your app.
A hardened image may have several variants. Inspect the image tags and find the image variant that meets your needs.
Update the base image in your Dockerfile.
Update the base image in your application's Dockerfile to the hardened image you found in the previous step. For
framework images, this is typically going to be an image tagged as dev because it has the tools needed to install
packages and dependencies.
For multi-stage Dockerfiles, update the runtime image in your Dockerfile.
To ensure that your final image is as minimal as possible, you should use a multi-stage build. All stages in your
Dockerfile should use a hardened image. While intermediary stages will typically use images tagged as dev, your
final runtime stage should use a non-dev image variant.
Install additional packages
Docker Hardened Images contain minimal packages in order to reduce the potential attack surface. You may need to install additional packages in your Dockerfile. Inspect the image variants to identify which packages are already installed.
Only images tagged as dev typically have package managers. You should use a multi-stage Dockerfile to install the
packages. Install the packages in the build stage that uses a dev image. Then, if needed, copy any necessary
artifacts to the runtime stage that uses a non-dev image.
For Alpine-based images, you can use apk to install packages. For Debian-based images, you can use apt-get to
install packages.
The following are common issues that you may encounter during migration.
The hardened images intended for runtime don't contain a shell nor any tools for debugging. The recommended method for debugging applications built with Docker Hardened Images is to use Docker Debug to attach to these containers. Docker Debug provides a shell, common debugging tools, and lets you install other tools in an ephemeral, writable layer that only exists during the debugging session.
By default image variants intended for runtime, run as the nonroot user. Ensure that necessary files and directories are accessible to the nonroot user. You may need to copy files to different directories or change permissions so your application running as the nonroot user can access them.
Non-dev hardened images run as a nonroot user by default. As a result, applications in these images can't bind to
privileged ports (below 1024) when running in Kubernetes or in Docker Engine versions older than 20.10. To avoid issues,
configure your application to listen on port 1025 or higher inside the container, even if you map it to a lower port on
the host. For example, docker run -p 80:8080 my-image will work because the port inside the container is 8080, and
docker run -p 80:81 my-image won't work because the port inside the container is 81.
By default, image variants intended for runtime don't contain a shell. Use dev images in build stages to run shell
commands and then copy any necessary artifacts into the runtime stage. In addition, use Docker Debug to debug containers
with no shell.
Docker Hardened Images may have different entry points than images such as Docker Official Images. Use docker inspect
to inspect entry points for Docker Hardened Images and update your Dockerfile if necessary.