Last active
March 30, 2023 19:51
-
-
Save anapsix/22ccafce5d41e16be6e2ade7381f6dab to your computer and use it in GitHub Desktop.
K8s CronJob with execution timeout implemented via livenessProbe
This file contains hidden or 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: do-something-job | |
namespace: scheduled-tasks | |
spec: | |
schedule: "*/2 * * * *" | |
successfulJobsHistoryLimit: 3 | |
failedJobsHistoryLimit: 3 | |
# "concurrencyPolicy" should be set to "Forbid" | |
# otherwise "successfulJobsHistoryLimit" and "failedJobsHistoryLimit" are meaningless | |
# use "concurrencyPolicy: Replace" if you don't need to retain job history | |
concurrencyPolicy: Forbid | |
jobTemplate: | |
spec: | |
backoffLimit: 3 | |
template: | |
spec: | |
# using "restartPolicy: Never" ensures failed pods are around to check logs, etc.. | |
# job controller will start new pods to replace failed ones | |
restartPolicy: Never | |
# for the sake of this exercise low "terminationGracePeriodSeconds" makes sense | |
# elsewhere, higher value should be used | |
terminationGracePeriodSeconds: 2 | |
containers: | |
- name: task1 | |
imagePullPolicy: Always | |
image: alpine:3.8 | |
livenessProbe: | |
exec: | |
command: | |
- sh | |
- -c | |
- > | |
start_time="$(cat /start_time)"; | |
now_time="$(date +%s)"; | |
elapsed_time=$((${now_time}-${start_time})); | |
[[ ${EXEC_TIMEOUT} -gt ${elapsed_time} ]] && | |
date +%s > /livenessProbe | |
initialDelaySeconds: 3 | |
periodSeconds: 2 | |
timeoutSeconds: 2 | |
successThreshold: 1 | |
failureThreshold: 1 | |
resources: | |
limits: | |
memory: "256Mi" | |
env: | |
- name: EXEC_TIMEOUT | |
value: "15" | |
command: | |
- sh | |
- -c | |
- > | |
date +%s > /start_time; | |
function doWork() { | |
max=$((RANDOM%15)); | |
while [[ ${count:-0} -lt ${max} ]]; do echo $count; let ++count; sleep 1; done | |
}; | |
doWork && echo >&2 "All Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment