<p>My DockerFile looks like :</p>
<pre><code>    FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;/app.jar&quot;]
</code></pre>
<p>and my yml file looks like :</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: imagename
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      bb: web
  template:
    metadata:
      labels:
        bb: web
    spec:
      containers:
        - name: imagename
          image: imagename:1.1
          imagePullPolicy: Never
          env:
          - name: MYSQL_USER
            value: root
          ports:
          - containerPort: 3306
---
apiVersion: v1
kind: Service
metadata:
  name: imagename
  namespace: default
spec:
  type: NodePort
  selector:
    bb: web
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30001
</code></pre>
<p>i have build docker image using below command :</p>
<pre><code>docker build -t dockerimage:1.1 .
</code></pre>
<p>and running the docker image like :</p>
<pre><code>docker run -p 8080:8080 --network=host dockerimage:1.1
</code></pre>
<p>When i deploy this image in kubernetes environment i am getting error :</p>
<pre><code>ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization. 
com.mysql.cj.jdbc.exceptions.CommunicationsException: 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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
</code></pre>
<p>Also i have done port forwarding :</p>
<pre><code>Forwarding from 127.0.0.1:13306 -&gt; 3306
</code></pre>
<p>Any suggestion what is wrong with the  above configuration ?</p>

Reema Joshi
<p>you need to add a service type clusterIP to your database like that:</p>
<h3>MySQL Service:</h3>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: mysql-service
  labels:
    app: mysql
spec:
  ports:
    - port: 3306
  selector:
    app: mysql
    tier: mysql
  clusterIP: None
</code></pre>
<h3>MySQL PVC:</h3>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: my-db-pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
</code></pre>
<h3>MySQL Deployment</h3>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql-deployment
spec:
  selector:
    matchLabels:
      app: mysql-deployment
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql-deployment
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: MYSQL_ROOT_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>Now on your Spring application what you need to access to the database is :</p>
<h3>Spring Boot deployment</h3>
<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: MYSQL_ROOT_USERNAME # Name of the environment variable
          valueFrom:          # Get the value of environment variable from kubernetes secrets
            secretKeyRef:
              name: mysql-secret
              key: MYSQL_ROOT_USERNAME
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: MYSQL_ROOT_PASSWORD
        - name: MYSQL_ROOT_URL
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: MYSQL_ROOT_PASSWORD
</code></pre>
<h3>Create your Secret :</h3>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Secret
data:
  MYSQL_ROOT_USERNAME: &lt;BASE64-ENCODED-PASSWORD&gt;
  MYSQL_ROOT_URL: &lt;BASE64-ENCODED-DB-NAME&gt;
  MYSQL_ROOT_USERNAME: &lt;BASE64-ENCODED-DB-USERNAME&gt;
  MYSQL_ROOT_PASSWORD: &lt;BASE64-ENCODED-DB-PASSWORD&gt;
metadata:
  name: mysql-secret
</code></pre>
<h3>Spring Boot Service:</h3>
<pre class="lang-yaml prettyprint-override"><code>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>
