gpt4 book ai didi

java - spring boot如何在k8s中连接localhost mysql?

转载 作者:行者123 更新时间:2023-12-02 08:47:53 25 4
gpt4 key购买 nike

我是 k8s 新手,正在使用 docker-mac-desktop k8s 测试 Spring Boot 应用程序。

我在本地连接mysql时spring boot时可以连接成功。

改造为docker k8s无法连接。

错误是:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]

is是本地监听端口:

redis-ser  2008    f    6u  IPv4 0x135c39363e412e7f      0t0  TCP 127.0.0.1:6379 (LISTEN)
kubectl 2909 f 7u IPv4 0x135c39365cf8a6ff 0t0 TCP 127.0.0.1:8500 (LISTEN)
kubectl 2909 f 8u IPv6 0x135c39365e7fb2cf 0t0 TCP [::1]:8500 (LISTEN)
mysqld 4091 f 28u IPv4 0x135c39364db92abf 0t0 TCP 127.0.0.1:3306 (LISTEN)
mysqld 4091 f 32u IPv4 0x135c39364dc7e6ff 0t0 TCP 127.0.0.1:33060 (LISTEN)
com.docke 41823 f 20u IPv4 0x135c39365b9a4d1f 0t0 TCP 127.0.0.1:6443 (LISTEN)
com.docke 41823 f 25u IPv6 0x135c39365e7f9a4f 0t0 TCP *:32003 (LISTEN)
com.docke 41926 f 8u IPv4 0x135c393660b3785f 0t0 TCP 127.0.0.1:60701 (LISTEN)

Spring Boot数据库配置:

spring:
datasource:
url: jdbc:mysql://mysqldb:3306/foo
# mysqldb.default.svc.cluster.local tried
#
driver-class-name: com.mysql.cj.jdbc.Driver
username: f

Spring Boot pod.yml:

apiVersion: v1
kind: Pod
metadata:
name: user-service
labels:
app: user-service
spec:
containers:
- name: user-service
image: user_service:v1

map 数据库引用:https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services

mysql_svc.yml:

apiVersion: v1
kind: Service
metadata:
name: mysqldb
spec:
ports:
- port: 3306
targetPort: 3306

---
kind: Endpoints
apiVersion: v1
metadata:
name: mysqldb
subsets:
- addresses:
- ip: 192.168.0.101
ports:
- port: 3306

而ip来自ifconfig

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
inet6 fe80::c2c:cc27:8121:fbac%en0 prefixlen 64 secured scopeid 0x5
inet 192.168.0.101 netmask 0xffffff00 broadcast 192.168.0.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active

如何从 k8s pod 连接本地主机数据库? :)

最佳答案

我已经解决了这个问题。

更改了 mysql_svc.yml

apiVersion: v1
kind: Service
metadata:
name: mysqldb
spec:
type: ExternalName
externalName: host.docker.internal
ports:
- port: 3306
targetPort: 3306

映射本地端口

更改数据源配置:

spring.datasource.url=jdbc:mysql://mysqldb/foo

然后连接成功。

引用answer

关于java - spring boot如何在k8s中连接localhost mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60964196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com