<p>I have 2 services. One containing 2 pod replicas for a web application which is dependent on another backend service having pod (2 replicas) for MySQL containers.</p>

<p>The web application uses environment variables set by backend DB service. I've all the json inside the same directory.</p>

<p>Is there any way to express the dependencies so that kubectl always create (and run) backend pods and services before it starts the web application service? I have used kubedeam to create the cluster.</p>

Gaurav Kumar
<p>I can suggest two solutions:</p>

<p>First, to attach an init container to the web servers that waits until MySQL is up and running. The deployment would be something like this:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 2
  template:
    metadata:
      labels:
        app: web
    spec:
      initContainers:
      - name: init-wait
        image: alpine
        command: ["sh", "-c", "for i in $(seq 1 300); do nc -zvw1 mysql 3306 &amp;&amp; exit 0 || sleep 3; done; exit 1"]
      containers:
      - name: web
        image: web-server
        ports:
        - containerPort: 80
          protocol: TCP
</code></pre>

<p>It uses netcat to try to start a TCP connection to the mysql service on the port 3306 every 3 seconds. Once it achieves to connect, the init-container ends and the web server starts normally.</p>

<p>The second option is to use <a href="https://github.com/Mirantis/k8s-AppController" rel="noreferrer">Mirantis AppController</a>. It allows you to create dependency objects as you need between server and database deployments. Check their repo for a full documentation.</p>
