1
votes

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

Based on the docs, I should create a service.

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.

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:

mysql -h11.22.33.44 -uuser -ppassword foo

...and it will hang and eventually the connection will time out.

I create the pod and service like so:

kubectl create -f ./mysql.yaml

mysql.yaml:

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"
1

1 Answers

2
votes

your service has a selector defined

selector:
  app: mysql

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 :

ports:
- protocol: TCP
  port: 3306
  targetPort: 3306