manipulating environment variables in kubernetes

So this was a little bit more painful to figure this than i was expecting, so recording it, so i don't forget....

what i wanted to was access the name of the pod within kubernetes, assign to an environment variable and then mess around with it from another environment variable...

# Getting the pod name

the following configuration will allow me to assign the pod name into an environment variable in kubernetes

env:
  - name: MY_POD_NAME
  valueFrom:
    fieldRef:
    fieldPath: metadata.name

and now that it's assigned i can access it from anywhere within the container. The following command shows the environment variables for a pod

bash-3.2$ kubectl exec fabric-vp0-awi4s env
HOSTNAME=e33e7f4dceb3
MY_POD_NAME=fabric-vp0-awi4s
# using the environment variable in other environment variables

now as i originally wanted to do, i wanted to use that environment variables, so i can manipulate it, where necessary.. and this is achieved by doing "($MYVARIABLENAME)" e.g.

env:
  - name: MY_POD_NAME
    valueFrom:
      fieldRef:
       fieldPath: metadata.name
  - name: MY_POD_NAME_2
     value: "$(MY_POD_NAME)"

and this yields the desired result

bash-3.2$ kubectl exec fabric-vp0-awi4s env
HOSTNAME=e33e7f4dceb3
MY_POD_NAME=fabric-vp0-awi4s
MY_POD_NAME_2=fabric-vp0-awi4s

whoop whoop, now this frees me up to do what i was actually wanting to do

chris hay

Read more posts by this author.