<p>When my Kubernetes pods get terminated (due to restarting or completely stopping), I would like to invoke some kind of a lifecycle hook that will notify me of the termination through email. Something like the following:</p>

<pre><code>  onTermination:
    args:
    - '/bin/sh'
    - '-c'
    - | 
       &lt;smtp login and send email script&gt;
</code></pre>

<p>How I can get an email when my pod is restarted or stop in Kubernetes?</p>

Raghunath Babalsure
<p>Only way I know that you can get an email if there is something wrong with Cluster/Node/Pod are monitoring tools.</p>

<p>You can use i.e paid software with free trial like <a href="https://sysdig.com/" rel="nofollow noreferrer">sysdig</a> or <a href="https://www.datadoghq.com/" rel="nofollow noreferrer">datadog</a>.
If you want to learn Kubernetes metrics you can use <a href="https://github.com/kubernetes/kube-state-metrics" rel="nofollow noreferrer">kube-state-metrics</a> with <a href="https://prometheus.io/" rel="nofollow noreferrer">Prometheus</a> (AlertManager) and i.e <a href="https://grafana.com/" rel="nofollow noreferrer">Grafana</a> as backend.</p>

<p>Here you have some steps which may be useful.</p>

<ol>
<li>Install kube-state-metrics.</li>
<li>Install Prometheus</li>
<li>Install Grafana</li>
<li>Connect to Prometheus (kubectl Port Forwarding or expose Prometheus as a Service)</li>
<li>Connect to Grafana (kubectl port forwarding)</li>
<li>In Grafana you have something like Alerts > Notification Chanels. There you can define how notification can be send (one of the option is email).</li>
<li>Create dashboard with desired metrics and add alerting to it.</li>
</ol>

<p>You can also check InfluxDB or Stackdriver as data source.</p>

<p>Tutorials which may help you <br/>
<a href="https://devopscube.com/setup-prometheus-monitoring-on-kubernetes/" rel="nofollow noreferrer">https://devopscube.com/setup-prometheus-monitoring-on-kubernetes/</a> <br/>
<a href="https://itnext.io/kubernetes-monitoring-with-prometheus-in-15-minutes-8e54d1de2e13" rel="nofollow noreferrer">https://itnext.io/kubernetes-monitoring-with-prometheus-in-15-minutes-8e54d1de2e13</a></p>
