Define Environment Variables referring to variable environment for a Container [kubernetes] Define Environment Variables referring to variable environment for a Container [kubernetes] kubernetes kubernetes

Define Environment Variables referring to variable environment for a Container [kubernetes]


This is not possible. You will need to define these some other way or otherwise handle the interpolation yourself. A common option is something like this:

command:- sh- -c- |  JAVA_OPTS="asdfasdf" java -whatever

But that does require fully overriding the command from the underlying container which is annoying. That said, you can do limited replacements in volume paths so probably handle it at that level instead for this particular case.


I'm trying this using nginx, however the variable substitution is working, perhaps you can try it:

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2kind: Deploymentmetadata:  name: nginx-deploymentspec:  selector:    matchLabels:      app: nginx  replicas: 2 # tells deployment to run 2 pods matching the template  template:    metadata:      labels:        app: nginx    spec:      containers:      - name: nginx        image: nginx:1.14.2        ports:        - containerPort: 80        env:        - name: POD_HOSTNAME          valueFrom:            fieldRef:              fieldPath: status.podIP        - name: SPRING_PROFILES_ACTIVE          value: prod,swagger        - name: JAVA_OPTS          value: -XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/$(POD_HOSTNAME).hprof

And after that I can see the content of JAVA_OPTS:

root@nginx-deployment-5bc5fcdc8b-f4ldx:/# echo $JAVA_OPTS-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/10.32.17.138.hprof


The hostname of pods in Kubernetes is always the same as the pod name and on Linux we have a env variable $HOSTNAME. So you can use it to achieve what you need:

Here is an example that may suit your needs:

apiVersion: v1kind: Podmetadata:  name: busybox  namespace: defaultspec:  containers:  - name: busybox    image: k8s.gcr.io/busybox:1.24     env:    - name: SPRING_PROFILES_ACTIVE      value: prod,swagger        command: [ "sh", "-c"]    args:    - while true; do        export JAVA_OPTS="-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/$HOSTNAME.hprof";        printenv JAVA_OPTS;        printenv SPRING_PROFILES_ACTIVE;        sleep 10;      done;    imagePullPolicy: IfNotPresent  restartPolicy: Always

If you run kubectl get logs busybox you can see this output:

-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprofprod,swagger-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprofprod,swagger-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprofprod,swagger-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprofprod,swagger-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprofprod,swagger-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprofprod,swagger