I have a case where i'd like to set the IP address of the pod as part of the container environment variables as well as the port (which can be hardcoded to default8080 for example)
something like below but I need to append the port as part of that too. so that APPLICATION_SERVER would result as 111...000:8080 something like this i guess.
- name: APPLICATION_SERVER
valueFrom:
fieldRef:
fieldPath: status.podIP
That information is available in the Downward API
The Values you are looking are available as env vars from the Downward API
apiVersion: v1
kind: Pod
metadata:
name: dapi-envars
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "sh", "-c"]
args:
- while true; do
echo -en '\n';
echo "Application Server $(APPLICATION_SERVER)";
sleep 10;
done;
env:
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: APPLICATION_SERVER
value: "$(MY_POD_IP):8080"
Output
kubectl logs dapi-envars
Application Server 10.244.0.7:8080
Application Server 10.244.0.7:8080
Application Server 10.244.0.7:8080
Application Server 10.244.0.7:8080
Application Server 10.244.0.7:8080
https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/
You can reuse defined environment variables to define new ones. For example, in your case you can do something like that:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: APPLICATION_SERVER
value: "$(POD_IP):8080"
Make sure that APPLICATION_SERVER
declared after POD_IP
, otherwise this will not work.