<p>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 "<strong>communication Link failure</strong>"</p>

<p>Below is my <code>mysql-deployment.yml</code></p>

<pre><code>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
</code></pre>

Satyanvesh Muppaneni
<p>Your K8S service should expose port and targetPort 3306 and in your JDBC URL use the name of that service:
<code>jdbc:mysql://mysql/database</code></p>

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

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

<p>This can have one or more of the following causes:</p>

<ul>
<li><p>IP address or hostname in JDBC URL is wrong.</p></li>
<li><p>Hostname in JDBC URL is not recognized by local DNS server.</p></li>
<li><p>Port number is missing or wrong in JDBC URL.</p></li>
<li><p>DB server is down.</p></li>
<li><p>DB server doesn't accept TCP/IP connections.</p></li>
<li><p>DB server has run out of connections.</p></li>
<li><p>Something in between Java and DB is blocking connections, e.g. a firewall or proxy. </p></li>
</ul>

<p>I suggest these steps to better understand the problem: </p>

<ul>
<li><p>Connect to MySQL pod and verify the content of the
<code>/etc/mysql/my.cnf</code> file</p></li>
<li><p>Connect to MySQL from inside the pod to verify it works</p></li>
<li><p>Remove <code>clusterIP: None</code> from Service manifest</p></li>
</ul>
