1

My DockerFile looks like :

    FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

and my yml file looks like :

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

i have build docker image using below command :

docker build -t dockerimage:1.1 .

and running the docker image like :

docker run -p 8080:8080 --network=host dockerimage:1.1

When i deploy this image in kubernetes environment i am getting error :

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)

Also i have done port forwarding :

Forwarding from 127.0.0.1:13306 -> 3306

Any suggestion what is wrong with the above configuration ?

1
  • You need a separate StatefulSet and Service for your database. Since the application is running "in the cluster" somewhere, there's no way for it to reach back to your local machine. The database host name will not be localhost and you'll need to configure that in your Deployment somewhere as well. Commented Jun 25, 2021 at 13:13

1 Answer 1

2

you need to add a service type clusterIP to your database like that:

MySQL Service:

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
  labels:
    app: mysql
spec:
  ports:
    - port: 3306
  selector:
    app: mysql
    tier: mysql
  clusterIP: None

MySQL PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: my-db-pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

MySQL Deployment

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

Now on your Spring application what you need to access to the database is :

Spring Boot deployment

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

Create your Secret :

apiVersion: v1
kind: Secret
data:
  MYSQL_ROOT_USERNAME: <BASE64-ENCODED-PASSWORD>
  MYSQL_ROOT_URL: <BASE64-ENCODED-DB-NAME>
  MYSQL_ROOT_USERNAME: <BASE64-ENCODED-DB-USERNAME>
  MYSQL_ROOT_PASSWORD: <BASE64-ENCODED-DB-PASSWORD>
metadata:
  name: mysql-secret

Spring Boot Service:

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    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.