Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Terminate a side-car container in Kubernetes Job
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: great-job
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: count
image: busybox
command: ["/bin/sh", "-c"]
args:
- |
sleep 2s
trap "touch /var/log/terminated" EXIT
i=0; while [ $i -lt 10 ]; do echo "$i: $(date)" >> /var/log/app.log; i=$((i+1)); sleep 1; done
volumeMounts:
- name: varlog
mountPath: /var/log
- name: count-log
image: busybox
command: ["/bin/sh", "-c"]
args:
- |
tail -f /var/log/app.log & CHILD_PID=$!
(while true; do if [[ -f "/var/log/terminated" ]]; then kill $CHILD_PID; echo "Killed $CHILD_PID because the main container terminated."; fi; sleep 1; done) &
wait $CHILD_PID
if [[ -f "/var/log/terminated" ]]; then exit 0; echo "Job completed. Exiting..."; fi
volumeMounts:
- name: varlog
mountPath: /var/log
volumes:
- name: varlog
emptyDir: {}
restartPolicy: OnFailure
backoffLimit: 5
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.