<p>I am new for pod health check with Readiness and Liveness. Recently I am working on Readiness. The scenario is as following:</p>
<p>The pod is a RestAPI service, it needs to connect to Database and store information in DB. So if RestAPI service wants to offer service, it needs to make sure database connection is successfully.</p>
<p>Si in our pod Readiness logic implementation, we use HTTP-Get and check if DB connection is connected, if it is okay, then HTTP-Get returns Ok, otherwise Readiness will be failed.</p>
<p>Not sure if the above logic is reasonable? Or is there any other approach for this logic processing?</p>
<p>Apart from Readiness, how about Liveness? Do I need to check DB connection in order to check Liveness is okay?</p>
<p>Any idea and suggestion are appreciated</p>

<p><strong>readiness</strong> and <strong>liveness</strong> is mostly for service you are running inside container, there could be a scenario where your DB is up but there is issue with the <strong>application</strong> at that time also your <strong>readiness</strong> will be <strong>Up</strong> as DB is running, in ideal scenario if application not working it should stop accepting traffic.</p>
<p>i would recommend using the <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">Init container</a> or <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/" rel="nofollow noreferrer">Life cycle hook</a> for checking the condition of the Database first if it's up process will move ahead and your application or deployment will come into the picture.</p>
<p>If the application works well your readiness and liveness will <strong>HTTP-OK</strong> and the service start accepting traffic.</p>
<p><strong>init container example</strong></p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox
    command: ['sh', '-c', 'echo The app is running! &amp;&amp; sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
  - name: init-mydb
    image: busybox
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
</code></pre>
<p><strong>Extra Notes</strong></p>
<p>There is actually no need to check the DB readiness at all.</p>
<p>As your application will be trying to connect with the <strong>Database</strong> so if <strong>DB</strong> is <strong>not</strong> <strong>UP</strong> your application won't respond <strong>HTTP-OK</strong> so your application won't start, and readiness keep failing for application.</p>
<p>As soon as your Database comes up your application will create a successful connection with DB and it will give <strong>200</strong> responses and readiness will mark POD ready.</p>
<p>there is no extra requirement to setup the readiness for Db and based on that start POD.</p>
