Skip to content

[agents] Read k8s data from environment variables #21

@roncohen

Description

@roncohen

It would be great if agents could read kubernetes data from environment variables and add it automatically to metadata sent to APM Server. This would work both for kubernetes setups where APM Server runs self-hosted and in the future where APM Server could be hosted.

I'll file a separate issue for the APM Server work.

Users set the environment variables through the DownwardAPI.

Here's a table mapping the environment variable to the metadata event field:

Environment variable Field name
KUBERNETES_NODE_NAME system.kubernetes.node.name
KUBERNETES_POD_NAME system.kubernetes.pod.name
KUBERNETES_NAMESPACE system.kubernetes.namespace
KUBERNETES_POD_UID system.kubernetes.pod.uid

Elasticsearch documents will use kubernetes.node.name (and not system.kubernetes.node.name) etc., so it aligns with the fields generated by filebeat, except for a few:

  • container.* fields which we don't seem to have access to unfortunately.
  • annotations: off by default in filebeat because people use them to store all sorts of large objects
  • labels: could be very useful down the line, but lets start without as an MVP

Here's how a user would add the environment variables to their kubernetes pod spec:

         - name: KUBERNETES_NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: KUBERNETES_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: KUBERNETES_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: KUBERNETES_POD_UID
            valueFrom:
              fieldRef:
                fieldPath: metadata.uid

@elastic/apm-agent-devs Please link your issue or PR and then mark the checkbox when the implementation is merged:

Potential additional fields that we should not do now
Environment variable Field name
KUBERNETES_POD_IP system.kubernetes.pod.ip
KUBERNETES_NODE_IP system.kubernetes.node.ip
KUBERNETES_LABELS_* system.kubernetes.labels.*
          - name: KUBERNETES_POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          - name: KUBERNETES_NODE_IP
            valueFrom:
              fieldRef:
                fieldPath: status.hostIP
          - name: KUBERNETES_LABELS_APP
            valueFrom:
              fieldRef:
                fieldPath: metadata.labels['app']

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions