I want to use the time that a pod is deployed as an environment variable within the pod. I cannot seem to make this work though. Anybody have an example on how to do this or maybe some pointers?
just out of curiosity, why do you want to do this? The state of all kubernetes objects is stored in etcd and can be accessed via the k8s API.
availableReplicas: 2
conditions:
- lastTransitionTime: "2020-09-22T09:40:02Z"
lastUpdateTime: "2020-09-22T09:42:02Z"
message: ReplicaSet "nginx-demo-5f4b9dd57d" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
- lastTransitionTime: "2020-09-23T12:57:46Z"
lastUpdateTime: "2020-09-23T12:57:46Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available```
If you are using simple pods, then this solution won't work for you. Because pod's env field becomes immutable once created.
But if you are using managed pods ( i.e. deployment, statefulset etc), you can use kubectl set env
command to update environment variables.
$ kubectl get deployment nginx-deployment -o=jsonpath='{.metadata.creationTimestamp}'
2020-10-07T13:24:49Z
$ kubectl set env deployment/nginx-deployment CREATIONTIME=$(kubectl get deployment nginx-deployment -o=jsonpath='{.metadata.creationTimestamp}')
deployment.apps/nginx-deployment env updated
$ kubectl exec -it nginx-deployment-7657f96b7f-6w8lf -- env
... ... ...
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=nginx-deployment-7657f96b7f-6w8lf
NGINX_VERSION=1.15.4-1~stretch
NJS_VERSION=1.15.4.0.2.4-1~stretch
CREATIONTIME=2020-10-07T13:24:49Z
... ... ...
Deployment file I used - Link