This content originally appeared on DEV Community and was authored by Ashok Nagaraj
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
- 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"%
More info
This content originally appeared on DEV Community and was authored by Ashok Nagaraj
Ashok Nagaraj | Sciencx (2022-04-18T05:09:40+00:00) Metadata about the workloads with Downward API. Retrieved from https://www.scien.cx/2022/04/18/metadata-about-the-workloads-with-downward-api/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.