<p>I wanna run a microservice which use DB. DB need to deploy in the same kubernetes cluster as well using PVC/PV. What is the kubernetes service name/command to use to implement such logic:</p>
<ol>
<li>Deploy the DB instance</li>
<li>If 1 is successful, then deploy the microservice, else return to 1 and try (if 100 times fail - then stop and alarm)</li>
<li>If 2 is successful, use work with it, autoscale if needed (autoscale kubernetes option)</li>
</ol>
<p>I concern mostly about 1-2: the service cannot work without the DB, but at the same time need to be in different pods ( or am I wrong and it's better to put 2 containers: DB and service at the same pod?)</p>

<p>I would say you should add <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#init-containers-in-use" rel="nofollow noreferrer">initContainer</a> to your microservice, which would search for the DB service, and whenever it's gonna be ready, then  the microservice will be started.</p>
<p>e.g.</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! &amp;&amp; sleep 3600']
  initContainers:
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', &quot;until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done&quot;]
</code></pre>
<p>As for the command simply use the <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply" rel="nofollow noreferrer">kubectl apply</a> with your yamls (with initContainer configured in your application).</p>
<p>If you want to do that in more automative way you can think about using <a href="https://fluxcd.io/" rel="nofollow noreferrer">fluxCD</a>/<a href="https://argoproj.github.io/argo-cd/" rel="nofollow noreferrer">argoCD</a>.</p>
<hr />
<p>As for the question from comments, <code>containers that run before the main container runs and the main container must be in the same pod?</code></p>
<p>Yes, they have to be in the same pod. As the init container is going to work unless, f.e. the database service will be avaliable, then the main container is gonna start. There is great example with that in above initContainer documentation.</p>
