Reputation: 8636
When we create a yml for the replication controller, we can give labels for the pod that is being created.
apiVersion: v1
kind: ReplicationController
metadata:
name: redis
spec:
template:
metadata:
labels:
app: redis
tier: backend
Can the containers that reside in this pod access those label values?
Upvotes: 1
Views: 2058
Reputation: 7337
Check out the Downward API, which allows the container to know more about itself.
Upvotes: 5
Reputation: 109
One way to access information of pod from inside the container is by using environment variables. The yaml file for pod is:
apiVersion: v1
kind: Pod
metadata:
name: pod-env
spec:
containers:
- name: test-container
image: ubuntu
command: [ "sh", "-c"]
args:
- while true; do
echo -en '\n';
printenv MY_NODE_NAME MY_POD_NAME;
printenv MY_POD_IP
sleep 1000;
done;
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
restartPolicy: Never
The mY_POD_NAME environment variable gets its value from the pod's field spec.nodeName. Likewise we can also container's fields as values. To verify this connect to the pod using the command:
kubectl exec -it pod-env -- /bin/bash
and print the environment variable:
printenv MY_POD_NAME
Other way of passing information from pod to container is using downwardAPI : https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/
Upvotes: 2