DEV Community

Cover image for Metadata about the workloads with Downward API
Ashok Nagaraj
Ashok Nagaraj

Posted on

2 2

Metadata about the workloads with Downward API

The need

There will be scenarios when the running containter needs information about the pod - namespace, pod-name, labels applied .. available from within the pod.
The container is not aware of it's runtime - if it is docker, mesos, kubernetes or something else

Alternative solutions

One can query the Kubernetes API server and get the data, but there are issues with this approach:

  • requires intelligent scripting &/or client SDKs
  • the pod itself should be self-aware to an extent to query the context => it is not really portable (or in easier terms there is some hardcoding or assumptions somewhere)
Downward API

Downward API allows 2 ways of exposing pod metadata to containers
a. as environment variables

cat /tmp/pod-info.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: test-container
    image: busybox
    command: [ "/bin/sh", "-c", "env | grep MY_POD" ]
    env:
    - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
    - name: MY_POD_NAMESPACE
      valueFrom:
        fieldRef:
          fieldPath: metadata.namespace
    - name: MY_POD_IP
      valueFrom:
        fieldRef:
          fieldPath: status.podIP

❯ kubectl logs pod/test-pod
MY_POD_NAMESPACE=default
MY_POD_IP=10.244.2.5
MY_POD_NAME=test-pod
Enter fullscreen mode Exit fullscreen mode
  1. as file contents
cat /tmp/pod-info.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-vol
  labels:
    env: stage
    team: acme
  annotations:
    build: "1.22"
    commitHash: "abcd1234"
spec:
  containers:
    - name: test-container
      image: busybox
      command: ["sh", "-c", "sleep 5; cat /var/tmp/pod-*.txt" ]
      volumeMounts:
        - name: podinfo
          mountPath: /var/tmp
          readOnly: false
  volumes:
    - name: podinfo
      downwardAPI:
        items:
          - path: "pod-labels.txt"
            fieldRef:
              fieldPath: metadata.labels
          - path: "pod-annotations.txt"
            fieldRef:
              fieldPath: metadata.annotations

❯ kubectl logs pod/test-pod-vol
build="1.22"
commitHash="abcd1234"
kubernetes.io/config.seen="2022-04-18T05:07:43.287165252Z"
kubernetes.io/config.source="api"env="stage"
team="acme"%
Enter fullscreen mode Exit fullscreen mode
More info

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more