Kubernetes - cannot connect to MySQL pod from other pod inside cluster although service exists
Kubernetes - cannot connect to MySQL pod from other pod inside cluster although service exists
https://stackoverflow.com/questions/63932282/kubernetes-cannot-connect-to-mysql-pod-from-other-pod-inside-cluster-although
Asked 5 years, 4 months ago Modified 5 years, 4 months ago Viewed 2k times 0
so I’ve got a Kubernetes cluster where I set up a deployment with two pods. In one pod, there is a MySQL container running and in another pod an Ubuntu container. In the Ubuntu container, I want to execute a Python script, that connects to the MySQL container.
But when I try to connect to the other pod, it says:
mysql.connector.errors.InterfaceError: 2003: Can’t connect to MySQL server on ‘mysql-service:3306’ (111 Connection refused) My service looks like this:
apiVersion: v1 kind: Service metadata: name: mysql-service labels: app: mysql-db-app spec: selector: app: mysql-db-app ports: - port: 3306 targetPort: 3306 This is my mysql-deployment.yaml:
apiVersion: apps/v1 kind: Deployment metadata: labels: io.kompose.service: mysql-db-app name: mysql-db-app spec: selector: matchLabels: io.kompose.service: mysql-db-app strategy: {} template: metadata: labels: io.kompose.service: mysql-db-app spec: containers: - image: mysql:8 name: mysql-db-app env: - name: MYSQL_ROOT_PASSWORD value: root ports: - containerPort: 3306 volumeMounts: - mountPath: /var/lib/mysql name: mysql-data volumes: - name: mysql-data persistentVolumeClaim: claimName: mysql-pvc restartPolicy: Always Do you know how to solve this problem?
mysqldockerkubernetes Share Improve this question Follow edited Sep 17, 2020 at 7:08 asked Sep 17, 2020 at 6:16 johnsonhudson’s user avatar johnsonhudson 333 bronze badges Is your mysql container running properly ? Can you also paste the deployment.yaml ? – avinashjha CommentedSep 17, 2020 at 6:59 the deployment.yaml of the mysql-pod? @AvinashKumar – johnsonhudson CommentedSep 17, 2020 at 7:00 yes @johnsonhudson – avinashjha CommentedSep 17, 2020 at 7:01 why are you missing the service type of mysql-service ? Can you try with clusterIP ? – avinashjha CommentedSep 17, 2020 at 7:02 ClusterIP doesn’t help @AvinashKumar – johnsonhudson CommentedSep 17, 2020 at 7:07 Add a comment 1 Answer Sorted by:
Highest score (default) 4
Looks like there is an issue with the label selector. Update your service to this
apiVersion: v1 kind: Service metadata: name: mysql-service labels: io.kompose.service: mysql-db-app spec: selector: io.kompose.service: mysql-db-app ports: - port: 3306 targetPort: 3306 The service label selector should match with the label selectors defined in deployment.yaml
Share Improve this answer Follow answered Sep 17, 2020 at 7:10 avinashjha’s user avatar avinashjha 65066 silver badges1919 bronze badges Sign up to request clarification or add additional context in comments.
2 Comments
johnsonhudson Over a year ago IT WORKS! had to delete the type: CluserIP of the previous answer that got deleted
Benson Macharia Over a year ago Spent a whole two days coz of a mismatch between the labels. Much appreciation for this