dhi.io/fluentd-kubernetes-daemonset
Fluentd DaemonSet for collecting, enriching, and shipping Kubernetes node and container logs.
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.
kubectl./var/log and the container runtime log dir). See
Running as the non-root user.This image is the Kubernetes DaemonSet build of Fluentd. It tails container and node logs, enriches each record with Kubernetes metadata, and forwards records to a single output destination. The destination is encoded in the tag flavor:
| Tag flavor | Output |
|---|---|
elasticsearch7, elasticsearch8 | Elasticsearch |
opensearch | OpenSearch |
s3 | Amazon S3 |
cloudwatch | Amazon CloudWatch Logs |
kinesis | Amazon Kinesis |
kafka, kafka2 | Apache Kafka |
gcs | Google Cloud Storage |
azureblob | Azure Blob Storage |
datadog, loggly, logzio, papertrail, graylog, logentries, syslog, forward | Hosted/forward outputs |
For example, dhi.io/fluentd-kubernetes-daemonset:1.19-elasticsearch8 ships to Elasticsearch 8. Every flavor includes
the shared Kubernetes plugins (metadata filter, systemd, Prometheus, concat, grok, multi-format parser) plus the output
plugin for that destination.
Configuration lives in /etc/fluent (fluent.conf includes systemd.conf, prometheus.conf, kubernetes.conf, and
conf.d/*.conf). The custom parsers are in /etc/fluent/plugins. The runtime listens on 24231/tcp for Prometheus
metrics.
The image runs as the non-root nonroot user (uid 65532) and reads its destination settings from environment
variables. A minimal Elasticsearch DaemonSet:
apiVersion: v1
kind: ServiceAccount
metadata: { name: fluentd, namespace: kube-system }
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata: { name: fluentd }
rules:
- apiGroups: [""]
resources: ["pods", "namespaces"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata: { name: fluentd }
roleRef: { apiGroup: rbac.authorization.k8s.io, kind: ClusterRole, name: fluentd }
subjects:
- { kind: ServiceAccount, name: fluentd, namespace: kube-system }
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd
namespace: kube-system
spec:
selector:
matchLabels: { app: fluentd }
template:
metadata:
labels: { app: fluentd }
spec:
serviceAccountName: fluentd
securityContext:
runAsUser: 65532
runAsGroup: 65532
fsGroup: 65532
containers:
- name: fluentd
image: dhi.io/fluentd-kubernetes-daemonset:1.19-elasticsearch8
env:
- { name: FLUENT_ELASTICSEARCH_HOST, value: elasticsearch.logging.svc }
- { name: FLUENT_ELASTICSEARCH_PORT, value: "9200" }
- { name: FLUENT_ELASTICSEARCH_SCHEME, value: https }
ports:
- { name: metrics, containerPort: 24231 }
volumeMounts:
- { name: varlog, mountPath: /var/log, readOnly: true }
- { name: varlibdockercontainers, mountPath: /var/lib/docker/containers, readOnly: true }
- { name: state, mountPath: /fluentd/log }
volumes:
- { name: varlog, hostPath: { path: /var/log } }
- { name: varlibdockercontainers, hostPath: { path: /var/lib/docker/containers } }
- { name: state, emptyDir: {} }
The ServiceAccount + ClusterRole above give the Kubernetes metadata filter the get/list/watch access to pods
and namespaces it needs; the API connection uses the in-cluster credentials automatically
(KUBERNETES_SERVICE_HOST/KUBERNETES_SERVICE_PORT are injected by Kubernetes). To also collect logs from
control-plane nodes, add a tolerations block to the pod spec that tolerates the control-plane taints.
The default config also enables the systemd input (journald). If you are not collecting node journald logs, set
FLUENTD_SYSTEMD_CONF=disable to silence the "No such file or directory" warnings; to collect them, mount the host
journal (e.g. /var/log/journal or /run/log/journal) read-only and add the systemd-journal group via
securityContext.supplementalGroups.
Upstream fluent/fluentd-kubernetes-daemonset runs as root so it can both read host logs and write its state files
under /var/log. This image runs as nonroot (uid 65532) and does not add a -root or -compat flavor, so a
deployment must arrange two things:
/var/log/containers/*.log (symlinks into the container runtime
log dir). Mount host /var/log and /var/lib/docker/containers (or the containerd equivalent) read-only. On most
distributions these files are group-readable; if your nodes make them root-only, add the owning group via
securityContext.supplementalGroups (note: fsGroup does not apply to hostPath volumes). Confirm with
ls -l /var/log/containers inside the pod.s3/gcs/logzio/
azureblob) disk buffers to /fluentd/log. The image creates /fluentd/log owned by uid 65532, so it starts out
of the box, but that is container-ephemeral — mount a writable volume there (an emptyDir as above, or a per-node
hostPath you own) so read positions and buffered events survive pod restarts. Do not point state back under the
read-only /var/log mount.This is the key difference from running upstream as root: writes go to /fluentd/log, not /var/log.
Each flavor reads FLUENT_<DESTINATION>_* environment variables (the same names upstream uses). Examples:
FLUENT_ELASTICSEARCH_HOST, FLUENT_ELASTICSEARCH_PORT, FLUENT_ELASTICSEARCH_SCHEME,
FLUENT_ELASTICSEARCH_USER, FLUENT_ELASTICSEARCH_PASSWORD.AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, S3_BUCKET_NAME, S3_BUCKET_REGION (or an IRSA role).FLUENT_KAFKA_BROKERS, plus the topic/format settings in fluent.conf. SASL PLAIN/SCRAM/OAUTHBEARER and
GSSAPI/Kerberos are supported (for GSSAPI, mount your krb5.conf and keytab).To disable the systemd or Prometheus inputs, set FLUENTD_SYSTEMD_CONF=disable or FLUENTD_PROMETHEUS_CONF=disable.
Add your own snippets by mounting files into /etc/fluent/conf.d/.
The runtime image ships only the plugins for its flavor. To add another plugin, build on the -dev variant, which
includes the Ruby build toolchain needed for gems with native extensions:
FROM dhi.io/fluentd-kubernetes-daemonset:1.19-elasticsearch8-dev
RUN gem install --no-document fluent-plugin-<name>
/etc/fluent (not upstream's /fluentd/etc); custom plugins go
in /etc/fluent/plugins and config snippets in /etc/fluent/conf.d; GEM_HOME is /usr/local/bundle. State files
(position files, journald cursors, disk buffers) are written to /fluentd/log, not /var/log — see
Running as the non-root user. Environment-driven configuration (the FLUENT_*
variables) is unchanged.uninitialized constant usually means the plugin isn't shipped in this flavor; add it on the
-dev variant. The Elasticsearch/OpenSearch simple sniffer is registered automatically by the entrypoint.kubectl debug -it <pod> --image=dhi.io/fluentd-kubernetes-daemonset:1.19-<flavor>-dev for an ephemeral debug
container, or Docker Debug outside Kubernetes.LD_PRELOAD=/usr/lib/libjemalloc/libjemalloc.so.2.logentries flavor with
fluent-plugin-logentries, but that plugin is unmaintained upstream (treat it as best-effort).columnify helper (used only for store_as parquet) is not bundled; the default
gzip/text/json store formats work out of the box. Add columnify on the dev variant if you need Parquet output.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:
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.