我正在为我的类做项目,项目的目标是用Mariadb数据库实现简单的后端应用程序。我选择Java Spring Boot作为后端。
当我在docker容器中测试数据库时,后端工作。
但是当我转向使用Kubernetes时,我的后端无法连接到数据库。
在我运行命令kubectl apply -f .
后端pod将继续产卵和死亡,因为它不会连接到数据库。
这里是我所有用于部署的Kubernetes文件。
mariadb-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mariadb
labels:
app: mariadb
type: database
spec:
replicas: 1
selector:
matchLabels:
app: mariadb
template:
metadata:
labels:
app: mariadb
type: database
spec:
containers:
- name: mariadb
image: mariadb
ports:
- containerPort: 3306
env:
- name: MARIADB_ROOT_PASSWORD
value: rootpass
- name: MARIADB_DATABASE
value: pastebin
- name: MARIADB_USER
value: dev
- name: MARIADB_PASSWORD
value: devpass
mariadb-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mariadb
labels:
name: mariadb
spec:
ports:
- protocol: TCP
port: 3306
targetPort: 3306
type: ClusterIP
selector:
app: mariadb
name: mariadb
backend-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: scalable-p1-backend
labels:
app: scalable-p1-backend
spec:
replicas: 1
selector:
matchLabels:
app: scalable-p1-backend
template:
metadata:
labels:
app: scalable-p1-backend
spec:
containers:
- name: scalable-p1-backend
image: ghcr.io/cs-muic/scalable-p1-taextreme/scalable-p1-backend:latest
env:
- name: SPRING_DATASOURCE_URL
value: jdbc:mariadb://mariadb:3306/pastebin
- name: SPRING_DATASOURCE_USERNAME
value: dev
- name: SPRING_DATASOURCE_PASSWORD
value: rootpass
imagePullSecrets:
- name: scalable-p1-taextreme-secret
backend-svc.yaml
kind: Service
apiVersion: v1
metadata:
name: scalable-p1-backend
spec:
selector:
app: scalable-p1-backend
type: ClusterIP
ports:
- protocol: TCP
port: 80
targetPort: 5000
我已经多次编辑这些文件,并尝试遵循我发现的每个解决方案,但似乎不起作用。
后端日志错误信息。
java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=mariadb-svc)(port=3306)(type=master) : Socket fail to connect to host:mariadb-svc, port:3306. Connection refused (Connection refused)
你试过了吗
- name: SPRING_DATASOURCE_URL
value: jdbc:mariadb://mariadb.default.svc:3306/pastebin