Kubernetes-无法从Spring应用程序连接到mysql
我有kubernetes集群.我已经从kubectl启动了mysql.我有一个春季启动应用程序的图像.我对application.yml中使用的JDBC URL感到困惑.我已经通过描述容器,服务等尝试了多个IP地址.它因"通信链接失败"
I have kubernetes cluster. I have started mysql from kubectl. I have a image of spring boot application. I am confused with the JDBC url to be used in application.yml. I have tried multiple IP addresses by describing pods, services etc. It is getting errored out with "communication Link failure"
下面是我的mysql-deployment.yml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
#type: NodePort
ports:
- port: 3306
#targetPort: 3306
#nodePort: 31000
selector:
app: mysql
clusterIP: None
---
apiVersion: v1
kind: Secret
metadata:
name: mysql-secret
type: Opaque
data:
MYSQL_ROOT_PASSWORD: cGFzc3dvcmQ= #password
MYSQL_DATABASE: dGVzdA== #test
MYSQL_USER: dGVzdHVzZXI= #testuser
MYSQL_PASSWORD: dGVzdDEyMw== #test123
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
env:
# Use secret in real usage
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: MYSQL_ROOT_PASSWORD
- name: MYSQL_DATABASE
valueFrom:
secretKeyRef:
name: mysql-secret
key: MYSQL_DATABASE
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysql-secret
key: MYSQL_USER
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: MYSQL_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
您的K8S服务应公开端口和targetPort 3306,并在JDBC URL中使用该服务的名称:
jdbc:mysql://mysql/database
Your K8S service should expose port and targetPort 3306 and in your JDBC URL use the name of that service:
jdbc:mysql://mysql/database
如果您的MySQL是仅用于在K8S中运行的应用程序的后端服务,则不需要在服务清单中使用nodePort.
If your MySQL is a backend service only for apps running in K8S you don't need nodePort in the service manifest.
如果获得SQLException: Connection refused
或Connection timed out
或特定于MySQL的CommunicationsException: Communications link failure
,则表明该数据库根本无法访问.
If you get a SQLException: Connection refused
or Connection timed out
or a MySQL specific CommunicationsException: Communications link failure
, then it means that the DB isn't reachable at all.
这可能有以下一种或多种原因:
This can have one or more of the following causes:
-
JDBC URL中的IP地址或主机名错误.
IP address or hostname in JDBC URL is wrong.
本地DNS服务器无法识别JDBC URL中的主机名.
Hostname in JDBC URL is not recognized by local DNS server.
JDBC URL中的端口号丢失或错误.
Port number is missing or wrong in JDBC URL.
数据库服务器已关闭.
数据库服务器不接受TCP/IP连接.
DB server doesn't accept TCP/IP connections.
数据库服务器已用尽连接.
DB server has run out of connections.
Java和DB之间的某种情况正在阻止连接,例如防火墙或代理.
Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
我建议采取以下步骤来更好地理解问题:
I suggest these steps to better understand the problem:
-
连接到MySQL pod并验证
/etc/mysql/my.cnf
文件
Connect to MySQL pod and verify the content of the
/etc/mysql/my.cnf
file
从pod内连接到MySQL以验证其是否有效
Connect to MySQL from inside the pod to verify it works
从服务清单中删除clusterIP: None