目前,我正在为我的学校项目做一个Kubernetes项目。之前,我在大约六个月前使用Next.js,Sping Boot 和MySQL开发了一个Web应用程序。我现在正在尝试将这个应用程序与Kubernetes集成。最初,我为每个组件创建了一个映像,并使用“docker run”成功测试了它。然而,我在将其转换为Kubernetes时遇到了困难。
我在Sping Boot Pod上收到的错误消息是:Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure.
我知道这个错误通常是由于连接到MySQL时出现的问题,例如缺少数据库或URL不正确。
下面是我用来创建MySQL和Sping Boot pod的YAML文件的一个示例。
apiVersion: v1
kind: Namespace
metadata:
name: lost_island
# lost_island_mysql
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
namespace: lost_island
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: lost_island_mysql
namespace: lost_island
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:latest
env:
- name: MYSQL_ROOT_PASSWORD
value: 1234
- name: MYSQL_DATABASE
value: lost_island
- name: MYSQL_PASSWORD
value: 1234
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
---
apiVersion: v1
kind: Service
metadata:
name: mysql-service
namespace: lost_island
spec:
selector:
app: mysql
ports:
- name: mysql-port
port: 3306
targetPort: 3306
type: NodePort
# lost_island_backend
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: lost_island_backend
namespace: lost_island
labels:
app: springboot
spec:
replicas: 1
selector:
matchLabels:
app: springboot
template:
metadata:
labels:
app: springboot
spec:
containers:
- name: springboot-app
image: skull/lost_island_backend
env:
- name: SPRING_DATASOURCE_URL
value: jdbc:mysql://mysql-service.lost_island.svc.cluster.local:3306/lost_island?serverTimezone=UTC&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&jdbcCompliantTruncation=false&useSSL=false
- name: SPRING_DATASOURCE_USERNAME
value: root
- name: SPRING_DATASOURCE_PASSWORD
value: 1234
ports:
- containerPort: 8080
- containerPort: 9092
1条答案
按热度按时间tf7tbtn21#
1.在同一命名空间
lost_island
中应用程序后端部署低于值就足够了
value: jdbc:mysql://mysql-service:3306/lost_island?serverTimezone=UTC&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&jdbcCompliantTruncation=false&useSSL=false
1.在mysql部署规范中打开容器端口
3306