Kubernetes -- unable to connect to mysql from spring application

7/18/2018

I have kubernetes cluster. I have started mysql from kubectl. I have a image of spring boot application. I am confused with the JDBC url to be used in application.yml. I have tried multiple IP addresses by describing pods, services etc. It is getting errored out with "communication Link failure"

Below is my mysql-deployment.yml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  #type: NodePort
  ports:
  - port: 3306
    #targetPort: 3306
    #nodePort: 31000
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: v1    
kind: Secret
metadata:
  name: mysql-secret
type: Opaque
data:
  MYSQL_ROOT_PASSWORD: cGFzc3dvcmQ= #password
  MYSQL_DATABASE: dGVzdA== #test
  MYSQL_USER: dGVzdHVzZXI= #testuser
  MYSQL_PASSWORD: dGVzdDEyMw== #test123
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_ROOT_PASSWORD
        - name: MYSQL_DATABASE
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_DATABASE
        - name: MYSQL_USER
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_USER
        - name: MYSQL_PASSWORD
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_PASSWORD          
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
-- Satyanvesh Muppaneni
kubernetes
mysql

2 Answers

7/18/2018

below is my output of kubectl get svc

kubectl get svc

NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          9d
mysql        ClusterIP   None           <none>        3306/TCP         2h
registry     NodePort    10.110.33.13   <none>        8761:31881/TCP   7d
-- Satyanvesh Muppaneni
Source: StackOverflow

7/18/2018

Your K8S service should expose port and targetPort 3306 and in your JDBC URL use the name of that service: jdbc:mysql://mysql/database

If your MySQL is a backend service only for apps running in K8S you don't need nodePort in the service manifest.

If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException: Communications link failure, then it means that the DB isn't reachable at all.

This can have one or more of the following causes:

  • IP address or hostname in JDBC URL is wrong.

  • Hostname in JDBC URL is not recognized by local DNS server.

  • Port number is missing or wrong in JDBC URL.

  • DB server is down.

  • DB server doesn't accept TCP/IP connections.

  • DB server has run out of connections.

  • Something in between Java and DB is blocking connections, e.g. a firewall or proxy. 

I suggest these steps to better understand the problem:

  • Connect to MySQL pod and verify the content of the /etc/mysql/my.cnf file

  • Connect to MySQL from inside the pod to verify it works

  • Remove clusterIP: None from Service manifest

-- Nicola Ben
Source: StackOverflow