<p>First time using Kubernetes. I have an API and a database, and I want the two pods to communicate with each other. </p>

<p>Based on the docs, I should create a service.</p>

<p>I have created a service for each of the two pods, though still not able to connect to the pod using the services IP address. </p>

<p>For example if the MySQL service that is created has an IP address of 11.22.33.44, I can run the following command to try to connect to the pod of that service:</p>

<pre><code>mysql -h11.22.33.44 -uuser -ppassword foo
</code></pre>

<p>...and it will hang and eventually the connection will time out.</p>

<p>I create the pod and service like so:</p>

<pre><code>kubectl create -f ./mysql.yaml
</code></pre>

<p><strong>mysql.yaml</strong>:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3306
---
apiVersion: v1
kind: Pod
metadata:
  name: mysql
spec:
  containers:
    - name: mysql
      image: my-custom-mysql-image:latest
      ports:
      - containerPort: 3306
        protocol: TCP
        name: mysql
      env:
      - name: MYSQL_DATABASE
        value: "foo"
      - name: MYSQL_USER
        value: "user"
      - name: MYSQL_ROOT_PASSWORD
        value: "password"
      - name: MYSQL_HOST
        value: "127.0.0.1"
</code></pre>

<p>your service has a selector defined</p>

<pre><code>selector:
  app: mysql
</code></pre>

<p>yet your Pod has no labels whatsoever, hence the service can not identify it as its backend and has no endpoint to direct traffic for ClusterIP. You should also stick to standard port number on service as well, so like this :</p>

<pre><code>ports:
- protocol: TCP
  port: 3306
  targetPort: 3306
</code></pre>
