【问题标题】:How to run spring boot mysql application docker image on kubernetes?如何在 kubernetes 上运行 spring boot mysql 应用程序 docker 镜像?
【发布时间】:2021-09-08 20:36:45
【问题描述】:

我的 DockerFile 看起来像:

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

我的 yml 文件看起来像:

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

我已经使用以下命令构建了 docker 映像:

docker build -t dockerimage:1.1 .

并像这样运行 docker 映像:

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

当我在 kubernetes 环境中部署此映像时出现错误:

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)

我也做了端口转发:

Forwarding from 127.0.0.1:13306 -> 3306

有什么建议上面的配置有什么问题吗?

【问题讨论】:

  • 您的数据库需要单独的 StatefulSet 和 Service。由于应用程序在某个地方“在集群中”运行,因此它无法返回到您的本地计算机。数据库主机名不会是 localhost,您还需要在 Deployment 中的某个地方进行配置。

标签: spring-boot docker kubernetes dockerfile kubernetes-pod


【解决方案1】:

您需要像这样将服务类型 clusterIP 添加到您的数据库中:

MySQL 服务:

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 部署

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

现在在您的 Spring 应用程序上,您需要访问数据库的是:

Spring Boot 部署

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

创建你的秘密:

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 服务:

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    

【讨论】:

    猜你喜欢
    • 2020-03-12
    • 2018-10-04
    • 2021-11-08
    • 2019-07-13
    • 1970-01-01
    • 2019-01-16
    • 2019-08-01
    • 2018-07-11
    • 2021-08-12
    相关资源
    最近更新 更多