I have a K8s deployment for a spring boot application I want to create a dump file .hprof
with the name of the pod ($hostname)
My JAVA_OPTIONS
parameters is a variable environment I want to refer to another variable inside the parent JAVA_Options
containers:
- env:
- name: SPRING_PROFILES_ACTIVE
value: prod,swagger
- name: JAVA_OPTS
value: ' -XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/$HOSTNAME.hprof'
What I expected is to have a variable that contains the hostname :
$ echo $JAVA_OPTS -XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/myPodNameStaging.hprof
What I have as a result is: $HOSTNAME.hprof
is set as a string.
I also tried many combinations $(HOSTNAME)
${HOSTNAME}
. Always I got the name of the variable and not the content value.
My question is: How to refer to the $HOSTNAME
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/v1beta2
kind: Deployment
metadata:
name: nginx-deployment
spec:
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
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.
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: v1
kind: Pod
metadata:
name: busybox
namespace: default
spec:
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.hprof
prod,swagger
-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprof
prod,swagger
-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprof
prod,swagger
-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprof
prod,swagger
-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprof
prod,swagger
-XX:MaxRAMPercentage=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/jvm-data/busybox.hprof
prod,swagger