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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay