<p>I am a newbie on Kubernetes and try to generate 2 pods including front-end application and back-end mysql. First I make a yaml file which contains both application and mysql server like below,</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: blog-system
spec:
  containers:
  - name: blog-app
    image: blog-app:latest
    imagePullPolicy: Never 
    ports:
      - containerPort: 8080
    args: ["-t", "-i"]
    link: blog-mysql
  - name: blog-mysql
    image: mysql:latest
    env:
      - name: MYSQL_ROOT_PASSWORD
        value: password
      - name: MYSQL_PASSWORD
        value: password
      - name: MYSQL_DATABASE
        value: test
    ports:
      - containerPort: 3306
</code></pre>

<p>The mysql jdbc url of front-end application is <code>jdbc:mysql://localhost:3306/test</code>. And pod generation is successful. The application and mysql are connected without errors. And this time I seperate application pod and mysql pod into 2 yaml files.</p>

<p>== pod-app.yaml</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: blog-app
spec:
  selector:
    app: blog-mysql
  containers:
  - name: blog-app
    image: app:latest
    imagePullPolicy: Never
    ports:
      - containerPort: 8080
    args: ["-t", "-i"]
    link: blog-mysql
</code></pre>

<p>== pod-db.yaml</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: blog-mysql
  labels: 
    app: blog-mysql
spec:
  containers:
  - name: blog-mysql
    image: mysql:latest
    env:
      - name: MYSQL_ROOT_PASSWORD
        value: password
      - name: MYSQL_PASSWORD
        value: password
      - name: MYSQL_DATABASE
        value: test
    ports:
      - containerPort: 3306
</code></pre>

<p>But the front-end application can not connect to mysql pod. It throws the connection exceptions. I am afraid the mysql jdbc url has some incorrect values or the yaml value has inappropriate values. I hope any advices.</p>

Joseph Hwang
<p>In the working case since same pod has two containers they are able to talk using localhost but in the second case since you have two pods you can not use localhost anymore. In this case you need to use the pod IP of the mysql pod in the frontend application. But problem with using POD IP is that it may change. Better is to expose mysql pod as <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">service</a> and use service name instead of IP in the frontend application. Check this <a href="https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/" rel="nofollow noreferrer">guide</a></p>
