title: Spring boot not able to connect mysql in kubernetes cluster
body: <p>In kubernetes cluster Spring boot application not able to connect mysql on more than one containers.</p>
  
  <p>Ex. I have Mysql Container Running and i have spring boot application which connect to mysql container, everything works fine if i have one spring boot application</p>
  
  <p><a href="https://i.stack.imgur.com/uc8KU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uc8KU.png" alt="enter image description here"></a></p>
  
  <p>But if i scale the Spring boot component to ex 4 pods then one of them will not be able to connect to mysql container</p>
  
  <p>Please help on this</p>
  
  <p><strong>Logs</strong></p>
  
  <pre><code>Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
  
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
          at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
          at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
          at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
          at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
          at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
          at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
          at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
          at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:850) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
          ... 58 common frames omitted
  Caused by: java.net.UnknownHostException: some-mysql
  </code></pre>
  
  <p><strong>Kubernetes Mysql Deployment File</strong></p>
  
  <pre><code>apiVersion: v1
  kind: PersistentVolume
  metadata:
    name: mysql-pv
    labels:
      type: local
  spec:
    storageClassName: standard
    capacity:
      storage: 250Mi
    accessModes:
      - ReadWriteOnce
    hostPath:                       
      path: "/mnt/data"
    persistentVolumeReclaimPolicy: Retain
  --- 
  apiVersion: v1
  kind: PersistentVolumeClaim 
  metadata:                          
    name: mysql-pv-claim
    labels:
      app: practice-app
  spec:                              
    storageClassName: standard
    accessModes:
      - ReadWriteOnce                
    resources:
      requests:
        storage: 250Mi
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: some-mysql
    labels:                         
      app: practice-app
  spec:
    ports:
      - port: 3306
    selector:                       
      app: practice-app
      tier: mysql
    clusterIP: None
  ---
  apiVersion: apps/v1
  kind: Deployment                    
  metadata:
    name: some-mysql           
    labels:                           
      app: practice-app
  spec:
    selector:
      matchLabels:                    
        app: practice-app
        tier: mysql
    strategy:
      type: Recreate
    template:                         
      metadata:
        labels:                       
          app: practice-app
          tier: mysql
      spec:                           
        containers:
        - image: mysql:5.6            
          name: mysql
          env:                        
          - name: MYSQL_ROOT_PASSWORD 
            valueFrom:                
              secretKeyRef:
                name: mysql-root-pass
                key: password
          - name: MYSQL_DATABASE
            valueFrom:
              secretKeyRef:
                name: mysql-db-url
                key: database
          - name: MYSQL_USER
            valueFrom:
              secretKeyRef:
                name: mysql-user-pass
                key: username
          - name: MYSQL_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-user-pass
                key: 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>
  
  <p><strong>Kubernetes Spring Boot Deployment file</strong></p>
  
  <pre><code>apiVersion: apps/v1           # API version
  kind: Deployment              # Type of kubernetes resource
  metadata:
    name: order-app-server    # Name of the kubernetes resource
    labels:                     # Labels that will be applied to this resource
      app: order-app-server
  spec:
    replicas: 1                 # No. of replicas/pods to run in this deployment
    selector:
      matchLabels:              # The deployment applies to any pods mayching the specified labels
        app: order-app-server
    template:                   # Template for creating the pods in this deployment
      metadata:
        labels:                 # Labels that will be applied to each Pod in this deployment
          app: order-app-server
      spec:                     # Spec for the containers that will be run in the Pods
        imagePullSecrets:
        - name: testXxxxxsecret
        containers:
        - name: order-app-server
          image: XXXXXX/order:latest
          ports:
          - containerPort: 8080 # The port that the container exposes
  
          env:                  # Environment variables supplied to the Pod
          - name: SPRING_DATASOURCE_USERNAME # Name of the environment variable
            valueFrom:          # Get the value of environment variable from kubernetes secrets
              secretKeyRef:
                name: mysql-user-pass
                key: username
          - name: SPRING_DATASOURCE_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-user-pass
                key: password
          - name: SPRING_DATASOURCE_URL
            valueFrom:
              secretKeyRef:
                name: mysql-db-url
                key: url
  ---
  apiVersion: v1                # API version
  kind: Service                 # Type of the kubernetes resource
  metadata:                     
    name: order-app-server-service    # Name of the kubernetes resource
    labels:                     # Labels that will be applied to this resource
      app: order-app-server
  spec:                         
    type: LoadBalancer              # The service will be exposed by opening a Port on each node and proxying it. 
    selector:
      app: order-app-server   # The service exposes Pods with label `app=polling-app-server`
    ports:                      # Forward incoming connections on port 8080 to the target port 8080
    - name: http
      port: 8080    
  </code></pre>
  
  <p><strong>Spring boot application yml</strong></p>
  
  <pre><code>spring:
    application:
       name: order-service
  
    datasource:
       url: jdbc:mysql://some-mysql:3306/practicecommerce?autoReconnect=true&amp;failOverReadOnly=false&amp;maxReconnects=10&amp;useSSL=false&amp;allowPublicKeyRetrieval=true
       username: prac
       password: prac
  
  
    jpa:
      hibernate:
        naming:
          physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
        ddl-auto: update
  
      properties:
        hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
      show-sql: true
  
  server:
    port: 8080
  
  </code></pre>