Execute a Script in a Container on Startup in OpenShift
- Confirm the process ID (PID) of the target process in a running container. It is common, though not required, that Liberty is PID 1.
- Edit the pod or deployment YAML. For example:
oc edit deployment deployment1
- Add a
lifecycle.postStart.exec.command
element to the target container that executes a diagnostic script. For example, the following sleeps 5 seconds and then gathers 5 javacores 10 seconds apart (changePID
as required):spec: containers: - name: ... lifecycle: postStart: exec: command: ["/bin/sh", "-c", "echo 'sleep 5; PID=1; i=0; while [ $i -le 5 ]; do kill -3 $PID; sleep 10; i=$(( i + 1 )); done' > /tmp/diag.sh; chmod +x /tmp/diag.sh; /tmp/diag.sh &"]
- Save the YAML and the pod/deployment will restart
Notes:
- The script is run in the background because Kubernetes won't set the
container to
RUNNING
until the postStart script "completes".