【问题标题】:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure on kubernetes MySQL and Spring Bootcom.mysql.cj.jdbc.exceptions.CommunicationsException:kubernetes MySQL 和 Spring Boot 上的通信链路故障
【发布时间】:2021-11-10 04:43:39
【问题描述】:

我在我认为是我的 .yaml 文件时遇到了一些问题。 Docker-compose 工作正常,容器按预期运行。 但是在对文件进行 kompose 转换后,在 k8s 上没有产生预期的结果,我得到 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure。

docker 容器中没有现有的容器,并且在 kompose convert 之前使用了 docker-compose down。

mysql pod 工作正常,可以访问。 但是 spring 无法连接到它....

在 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:

错误:

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

后端网络.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: {}

gohm'c 发布的解决方案是我的端口不正确。

接下来面对这个问题,我需要指定一个集群/负载吗?

$ kubectl 公开部署 spring-boot-jpa-app --type=NodePort 来自服务器的错误(AlreadyExists):服务“spring-boot-jpa-app”已经存在

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

【问题讨论】:

    标签: docker kubernetes docker-compose kompose


    【解决方案1】:

    mysql-docker-container服务端口是3307,你可以试试:

    env:
    ...
    - name: spring.datasource.url
      value: jdbc:mysql://mysql-docker-container:3307/db_fromSpring
    

    【讨论】:

    • 非常感谢!多么敏锐的眼光!接下来我有这个错误,我需要一个nodeport吗?
    • 我尝试端口转发以及重新启动所有内容,但它似乎不起作用。请给我一根骨头好吗?它与 .yaml 文件有关吗?
    • 你已经部署了k8s服务(种类:Service),不需要kubectl expose再做同样的事情。试试kubectl port-forward service/spring-boot-jpa-app 8087:8087。然后你可以浏览到 localhost:8087 如果你没有指定 k8s 服务的默认类型是 ClusterIP。仅当您想在每个节点上打开端口时才使用 NodePort。在 minikube 的情况下,这通常是不必要的,除非您出于某种目的故意想要它。
    • 非常感谢!!我会消化它们,请您分享调试过程中的思维过程以及开始时如何消化它们?你有什么媒体可以让我了解更多信息吗?或者你能建议我学习它的材料吗?
    • 从在k8s中构建fundamental knowledge开始平均时间开始练习deployment。使用更接近真实世界的东西,例如EKS Anywherek3sk0s
    猜你喜欢
    • 2019-11-30
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多