<p>I'm looking to put together a precise set of conditions that indicate that a container has failed.</p>

<p>1) Readiness check is not enough -- a container could be in init or container creating state, and, thus, healthy.</p>

<p>2) Terminating state is not enough either -- a container could be terminating as part of a rolling reboot. In addition, the reason for the terminating state is of type 'string', so could be anything, and I haven't been able to find an exhaustive list.</p>

<p>3) Waiting state is not enough either - it has the same problem that the reason is a string, and there is no exhaustive list of possibilities. Though, checking for ["CrashLoopBackOff", "ErrImagePull", "ImagePullBackOff"] does give a definitive answer that the container did indeed fail.</p>

<p>The use case, if that's important, is that I want to notify the user if the deployment/statefulset is in a failing state, but avoid false-positives on creation  and rolling reboots. I also consider the case when rolling reboot results in CrashLoopBackOff of new containers a failure. Thus, I'm looking to containers to build out the healthiness status of the deployments/statefulsets.</p>

Lana Nova
<p>First of all I'm assuming 2 things:</p>

<ol>
<li>By container you actually are referring to a pod with a single container in it.</li>
<li>Your use case allows you to detect status externally</li>
</ol>

<p>In this case, you have several options for determining the state of failed pods from outside of the pod/container itself. </p>

<ol>
<li>use Kubectl to get a list of failed containers</li>
</ol>

<p>Get failed containers from all namespaces:</p>

<p><code>kubectl get pods --field-selector=status.phase=Failed --all-namespaces</code></p>

<ol start="2">
<li>Use kubectl to Detect the state of pod</li>
</ol>

<p>Simple: 
<code>kubectl get pod mypod | tail -n 1 | awk '{ print $3 }'</code></p>

<p>Example Output: </p>

<pre><code>"Failed"
"Running"
</code></pre>

<p>The Simple command can be used to do a simple bash comparison such as:</p>

<pre><code>if [ $(`kubectl get pod mypod | tail -n 1 | awk '{ print $3 }'`) == "Failed" ]; 
    then echo "Container has failed!"; 
fi
</code></pre>

<p>Read more about pod lifecycles</p>

<p><a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/</a></p>
