Terminate a side-car container in Kubernetes Job
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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