0

I am facing some issues on I believe to be my .yaml file. Docker-compose works fine and the containers ran as expected. But after kompose convert on the file did not yield desired result on k8s, and I am getting com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure.

There are no existing container in docker containers and docker-compose down was used prior in kompose convert.

mysql pod work fine, and able to access. spring is however unable to connect to it....

in docker-compose.yaml

        version: '3'

services:

  mysql-docker-container:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=1
      - MYSQL_DATABASE=db_fromSpring
      - MYSQL_USER=springuser
      - MYSQL_PASSWORD=ThePassword
  
    networks:
      - backend
    ports: 
      - 3307:3306
    volumes:
      - /data/mysql

  spring-boot-jpa-app:
    command: mvn clean install -DskipTests
    image: bnsbns/spring-boot-jpa-image
 
    depends_on:
      - mysql-docker-container
      
    environment:
      - spring.datasource.url=jdbc:mysql://mysql-docker-container:3306/db_fromSpring
      - spring.datasource.username=springuser
      - spring.datasource.password=ThePassword
    networks:
      - backend

    ports:
      - "8087:8080"
    volumes:
      - /data/spring-boot-app
 
networks:
  backend:

Error:

2021-09-15 04:37:47.542 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

backend-network.yaml

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  creationTimestamp: null
  name: backend
spec:
  ingress:
  - from:
    - podSelector:
        matchLabels:
          io.kompose.network/backend: "true"
  podSelector:
    matchLabels:
      io.kompose.network/backend: "true"

mysql-docker-container-claim0-persistentvolumeclaim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: mysql-docker-container-claim0
  name: mysql-docker-container-claim0
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

mysql-docker-container-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: mysql-docker-container
  name: mysql-docker-container
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: mysql-docker-container
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert
        kompose.version: 1.21.0 (992df58d8)
      creationTimestamp: null
      labels:
        io.kompose.network/backend: "true"
        io.kompose.service: mysql-docker-container
    spec:
      containers:
      - env:
        - name: MYSQL_DATABASE
          value: db_fromSpring
        - name: MYSQL_PASSWORD
          value: ThePassword
        - name: MYSQL_ROOT_PASSWORD
          value: "1"
        - name: MYSQL_USER
          value: springuser
        image: mysql:latest
        imagePullPolicy: ""
        name: mysql-docker-container
        ports:
        - containerPort: 3306
        resources: {}
        volumeMounts:
        - mountPath: /data/mysql
          name: mysql-docker-container-claim0
      restartPolicy: Always
      serviceAccountName: ""
      volumes:
      - name: mysql-docker-container-claim0
        persistentVolumeClaim:
          claimName: mysql-docker-container-claim0
status: {}

mysql-docker-container-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: mysql-docker-container
  name: mysql-docker-container
spec:
  ports:
  - name: "3307"
    port: 3307
    targetPort: 3306
  selector:
    io.kompose.service: mysql-docker-container
status:
  loadBalancer: {}

springboot-app-jpa-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: spring-boot-jpa-app
  name: spring-boot-jpa-app
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: spring-boot-jpa-app
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert
        kompose.version: 1.21.0 (992df58d8)
      creationTimestamp: null
      labels:
        io.kompose.network/backend: "true"
        io.kompose.service: spring-boot-jpa-app
    spec:
      containers:
      - args:
        - mvn
        - clean
        - install
        - -DskipTests
        env:
        - name: spring.datasource.password
          value: ThePassword
        - name: spring.datasource.url
          value: jdbc:mysql://mysql-docker-container:3306/db_fromSpring
        - name: spring.datasource.username
          value: springuser
        image: bnsbns/spring-boot-jpa-image
        imagePullPolicy: ""
        name: spring-boot-jpa-app
        ports:
        - containerPort: 8080
        resources: {}
        volumeMounts:
        - mountPath: /data/spring-boot-app
          name: spring-boot-jpa-app-claim0
      restartPolicy: Always
      serviceAccountName: ""
      volumes:
      - name: spring-boot-jpa-app-claim0
        persistentVolumeClaim:
          claimName: spring-boot-jpa-app-claim0
status: {}

springboot-jpa-app-persistence-claim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: spring-boot-jpa-app-claim0
  name: spring-boot-jpa-app-claim0
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

springboot-app-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: spring-boot-jpa-app
  name: spring-boot-jpa-app
spec:
  ports:
  - name: "8087"
    port: 8087
    targetPort: 8080
  selector:
    io.kompose.service: spring-boot-jpa-app
status:
  loadBalancer: {}

solution as posted by gohm'c was that i had the incorrect port.

facing this issue next, do i need to specific a cluster/load?

$ kubectl expose deployment spring-boot-jpa-app --type=NodePort Error from server (AlreadyExists): services "spring-boot-jpa-app" already exists

minikube service spring-boot-jpa-app
|-----------|---------------------|-------------|--------------|
| NAMESPACE |        NAME         | TARGET PORT |     URL      |
|-----------|---------------------|-------------|--------------|
| default   | spring-boot-jpa-app |             | No node port |
|-----------|---------------------|-------------|--------------|
  service default/spring-boot-jpa-app has no node port

1 Answers1

1

The mysql-docker-container service port is 3307, can you try:

env:
...
- name: spring.datasource.url
  value: jdbc:mysql://mysql-docker-container:3307/db_fromSpring
gohm'c
  • 13,492
  • 1
  • 9
  • 16
  • Thanks so much ! What a sharp eye ! I have this error next, do I need to have a nodeport? – invertedOwlCoding Sep 15 '21 at 06:58
  • i tried to port-forward as well as restart everything, but it did not seem to work. Could you please throw me a bone? Is it something to do with the .yaml files? – invertedOwlCoding Sep 15 '21 at 08:41
  • You have already deployed k8s service (kind: Service), there is no need for `kubectl expose` again to do the same. Try `kubectl port-forward service/spring-boot-jpa-app 8087:8087`. You can then browse to localhost:8087 If you didn't specify the default type for k8s service is ClusterIP. You use NodePort only if you want to open the port on every node. In case of minikube this is normally un-necessary unless you intentionally want it for a purpose. – gohm'c Sep 15 '21 at 08:56
  • Thanks very much !! I will digest them, could you please share the thought process in debugging and how do you digest them when starting out? Do you have any medium where I can speak with to learn more? Or could you please suggest materials for me to learn it? – invertedOwlCoding Sep 15 '21 at 10:00
  • Start with building the [fundamental knowledge](https://kubernetes.io/docs/concepts/overview/) in k8s the **mean time** start practicing [deployment](https://kubernetes.io/docs/tutorials/kubernetes-basics/). Use something closer to real-world like [EKS Anywhere](https://stackoverflow.com/a/69201473/14704799), [k3s](https://rancher.com/docs/k3s/latest/en/quick-start/), [k0s](https://docs.k0sproject.io/v1.22.1+k0s.1/install/). – gohm'c Sep 16 '21 at 03:03