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

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

<pre><code>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
</code></pre>

<p>or on newer K8s:</p>

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

<p>How to tell whether a Job is complete: <a href="https://stackoverflow.com/questions/44686568/kubernetes-tell-when-job-is-complete">Kubernetes - Tell when Job is Complete</a></p>

<p>You can also setup something like <a href="https://prometheus.io/" rel="nofollow noreferrer">Prometheus</a> with <a href="https://prometheus.io/docs/alerting/alertmanager/" rel="nofollow noreferrer">Alertmanager</a> in your Kubernetes cluster to monitor your Jobs.</p>

<p>Some useful info <a href="https://medium.com/@tristan_96324/prometheus-k8s-cronjob-alerts-94bee7b90511" rel="nofollow noreferrer">here</a> and <a href="https://itnext.io/kubernetes-monitoring-with-prometheus-in-15-minutes-8e54d1de2e13" rel="nofollow noreferrer">here</a>.</p>
