Kubernetes cronjob email alerts

10/16/2018

I have few cronjobs configured and running in Kubernetes. How to setup up cronjob email alerts for success or failure in Kubernetes.

-- Sun
cron
kubernetes

1 Answer

10/16/2018

This could be as easy as setting up a bash script with kubectl to send an email if you see a job that is Failed state.

while true; do if `kubectl get jobs myjob -o jsonpath='{.status.conditions[?(@.type=="Failed")].status}' | grep True`; then mail email@address -s jobfailed; else sleep 1 ; fi; done

or on newer K8s:

while true; do kubectl wait --for=condition=failed job/myjob; mail@address -s jobfailed; done

How to tell whether a Job is complete: Kubernetes - Tell when Job is Complete

You can also setup something like Prometheus with Alertmanager in your Kubernetes cluster to monitor your Jobs.

Some useful info here and here.

-- Rico
Source: StackOverflow