I have a k8s deployment which run a Flask app, and I require that app to now his own application name (which is in metadata.labels.app
). Then I would like to be able to get that information from the running pod, is it possible ?
I already tried to bash
into the running pod and run printenv
but there was no information about that precise deployment.
I also checked with the kubernetes client for python but it seems to be impossible to get only informations about current deployment.
I am missing something ? Is it even possible ?
Thank for your help
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: my-app
env: production
name: my-app
namespace: production
spec:
replicas: 1
selector:
matchLabels:
app: my-app
env: production
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: my-app
env: production
spec:
containers:
- image: gcr.io/github.com/my-company/my-app
imagePullPolicy: IfNotPresent
name: my-app-1
resources:
limits:
cpu: 100m
memory: 200Mi
requests:
cpu: 10m
memory: 100Mi
CodePudding user response:
You can expose information using env vars or files, see
- https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
- https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/
Example with single label:
apiVersion: v1
kind: Pod
metadata:
name: label-as-var
labels:
app: my-app
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: ["sh", "-c"]
args:
- while true; do
echo "$MY_APP";
sleep 10;
done;
env:
- name: MY_APP
valueFrom:
fieldRef:
fieldPath: metadata.labels['app']
result:
kubectl logs label-as-var
my-app
Example with downward api from docs, where all labels are accessible in file /etc/podinfo/labels
apiVersion: v1
kind: Pod
metadata:
name: kubernetes-downwardapi-volume-example
labels:
zone: us-est-coast
cluster: test-cluster1
rack: rack-22
annotations:
build: two
builder: john-doe
spec:
containers:
- name: client-container
image: k8s.gcr.io/busybox
command: ["sh", "-c"]
args:
- while true; do
if [[ -e /etc/podinfo/labels ]]; then
echo -en '\n\n'; cat /etc/podinfo/labels; fi;
if [[ -e /etc/podinfo/annotations ]]; then
echo -en '\n\n'; cat /etc/podinfo/annotations; fi;
sleep 5;
done;
volumeMounts:
- name: podinfo
mountPath: /etc/podinfo
volumes:
- name: podinfo
downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
- path: "annotations"
fieldRef:
fieldPath: metadata.annotations