Reputation: 5149
I try to connect a local spring boot application with an internal mysql-db in k8-cluster. I used the k8 cluster locally in docker desktop.
My deployment.yml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: kazi-db
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql/mysql-server:8.0
name: mysql
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: kazi-db-secret
key: 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
- name: mysql-initdb
configMap:
name: kazi-initdb
my service.yml:
apiVersion: v1
kind: Service
metadata:
name: kazi-db
spec:
ports:
- port: 3306
selector:
app: kazi-db
type: LoadBalancer
The service description:
kubectl get services kazi-db
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kazi-db LoadBalancer 10.107.75.107 localhost 3306:31627/TCP 3d
my spring-boot application.yml
## JDBC part
spring:
config:
activate:
on-profile: local
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/kazi?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: emo
password: 123qwe
jpa:
show-sql: true
database-platform: org.hibernate.dialect.MySQL8Dialect
hibernate:
ddl-auto: update
liquibase:
change-log: classpath:liquibase/changelog.xml
I used localhost because k8 has a localhost as externalIp defined.
kubectl describe services kazi-db
Name: kazi-db
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=kazi-db
Type: LoadBalancer
IP Families: <none>
IP: 10.107.75.107
IPs: <none>
LoadBalancer Ingress: localhost
Port: <unset> 3306/TCP
TargetPort: 3306/TCP
NodePort: <unset> 31627/TCP
Endpoints: <none>
Session Affinity: None
External Traffic Policy: Cluster
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Type 10m service-controller ClusterIP -> LoadBalancer
I have a local mysql-db too an i can connect the application without problems.
I can connect to the db-cluster with kubectl exec -it kazi-db-554bd6d787-2gb88 -- /bin/bash
an create some table, but the application not.
When i start the spring-boot application:
java.sql.SQLSyntaxErrorException: Unknown database 'kazi'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.18.jar:8.0.18]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.18.jar:8.0.18]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.18.jar:8.0.18]
although the db kazi exists in db-cluster.
But how i have to connect to internal k8 cluster db?
I'm using docker desktop v20.10.11
with k8s v1.22.4
. I tried it with minikube version: v1.24.0
too. Of both, i have the same issue.
Upvotes: 0
Views: 964
Reputation: 5149
i resolve it with port forwarding: kubectl port-forward <pod-name> 3306:3306 -n default
Upvotes: 1