<p>
Hi there,
I have a docker container that is a php backend. I have created a kubernetes pod of this container. This is what my yml file looks like:</p>

<pre><code> apiVersion: v1
 kind: Pod
 metadata:
  name: backend
  spec:
  containers:
 - name: backend
  image: 000.dkr.ecr.eu-west-1.amazonaws.com/fullstackapp
  ports:
  - containerPort: 8000
</code></pre>

<p>However I want to be able to connect my MySql database (which is also a docker container) to the backend in the same pod.  However I have no idea how to go about doing this.  Any help would be appreciated!</p>

<p>Well,</p>

<p>Since you have dockerized your app (you made an docker image), you also must use a docker image for your MySql database.</p>

<p>But here is the kicker, you need also to create <strong>services</strong> for your app pod and your MySql pod.</p>

<p>You can find all the details in the k8 <a href="https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/" rel="nofollow noreferrer">documentation</a> (which is really good)</p>

<p>To make myself clear:</p>

<p>1.) First create a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer">deployment</a> object for your app.</p>

<p>2.) Then make a <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">service</a> for your app.</p>

<p>You rinse and repeat for the MySql database.</p>

<p>1.) You need the <strong>deployment</strong> object (and not the pod kind), because the deployment object keeps you pod alive when one breaks, for instance if you have tree replicas (pods) the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/" rel="nofollow noreferrer">replicaSet</a> that the deployment object uses, will make sure that there are three replicas of your app.</p>

<p>2.) <strong>Services</strong> will group your pods (via <strong>labels</strong>), because the pods that the deployment object will generate will have a short life (<strong>ephemeral</strong>), meaning their IP address will be unstable and you wont be able to rely on them.</p>

<p>So, you will use services that will give you a cluster IP (<strong>virtual IP</strong>), that other objects can use. For instance; when your app wants to connect to the MySQL database.</p>

<p>You can use the <strong>name</strong> of the MySQL <strong>service</strong> in your apps configuration files.</p>

<p>So, basically that's how you would connect a <strong>MySQL pod</strong> to you <strong>apps pod</strong>.</p>

<p>Take a look at the <a href="https://www.katacoda.com/" rel="nofollow noreferrer">katacode</a> project, they give you a playground to learn this kind of stuff.</p>

<p>Tom</p>
