<p>I am trying to implement Probe in Kubernetes</p>
<p>I understand that the probe returns success if <code>http_status_code &gt;= 200 and &lt;=399</code></p>
<pre><code>  containers:
    - name: myimage
      image: myimage:45
      imagePullPolicy: Never
      readinessProbe:
        httpGet:
          path: /
          port: 5004
</code></pre>
<p>However, our application returns status code 404 (by design) in case of <strong>success</strong>.</p>
<p>How can I make the probe to succeed in this case?</p>
<p>Is there any way to achieve this?</p>

Sathish Kumar
<p>Change the probe to <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-liveness-command" rel="nofollow noreferrer">execute a command</a> instead of an http request. Kubernetes executes the command inside the container and determines the success based on the exit code of the command (0 for success, non-zero for failure)</p>
<ol>
<li>Create a script <code>/health.sh</code> that uses curl to call your health check endpoint through <code>localhost</code>. Include a condition that sets the exit code to 0 (success) if the return code is 404, and 1 (failure) otherwise</li>
</ol>
<pre class="lang-bash prettyprint-override"><code>#!/bin/sh
if [ $(curl -s -o /dev/null -w &quot;%{http_code}&quot; http://localhost:5004) -eq &quot;404&quot; ]; then
  exit 0
else 
  exit 1
fi
</code></pre>
<ol start="2">
<li>Configure the probe with a command instead of a http request</li>
</ol>
<pre class="lang-yaml prettyprint-override"><code>readinessProbe:
  exec:
    command:
    - /bin/sh
    - -c
    - /health.sh
</code></pre>
<p>If you don't want to modify the container, you can define the script inline in the command section like below</p>
<pre class="lang-yaml prettyprint-override"><code>readinessProbe:
  exec:
    command:
    - /bin/sh
    - -c
    - if [ $(curl -s -o /dev/null -w &quot;%{http_code}&quot; http://localhost:5004) -eq &quot;404&quot; ]; then exit 0; else exit 1; fi
</code></pre>
