<p>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.</p>
<p>My deployment.yml:</p>
<pre><code>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
</code></pre>
<p>my service.yml:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: kazi-db
spec:
  ports:
    - port: 3306
  selector:
    app: kazi-db
  type: LoadBalancer
</code></pre>
<p>The service description:</p>
<pre><code>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
</code></pre>
<p>my spring-boot application.yml</p>
<pre><code>## 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&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;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

</code></pre>
<p>I used localhost because k8 has a localhost as externalIp defined.</p>
<pre><code>kubectl describe services kazi-db
Name:                     kazi-db
Namespace:                default
Labels:                   &lt;none&gt;
Annotations:              &lt;none&gt;
Selector:                 app=kazi-db
Type:                     LoadBalancer
IP Families:              &lt;none&gt;
IP:                       10.107.75.107
IPs:                      &lt;none&gt;
LoadBalancer Ingress:     localhost
Port:                     &lt;unset&gt;  3306/TCP
TargetPort:               3306/TCP
NodePort:                 &lt;unset&gt;  31627/TCP
Endpoints:                &lt;none&gt;
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason  Age   From                Message
  ----    ------  ----  ----                -------
  Normal  Type    10m   service-controller  ClusterIP -&gt; LoadBalancer    

</code></pre>
<p>I have a local mysql-db too an i can connect the application without problems.
I can connect to the db-cluster with <code>kubectl exec -it kazi-db-554bd6d787-2gb88  -- /bin/bash </code> an create some table, but the application not.</p>
<p>When i start the spring-boot application:</p>
<pre><code>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]
</code></pre>
<p>although the db kazi exists in db-cluster.</p>
<p>But how i have to connect to internal k8 cluster db?</p>
<p>I'm using docker desktop <code>v20.10.11</code> with k8s <code>v1.22.4</code>. I tried it with <code>minikube version: v1.24.0</code> too. Of both, i have the same issue.</p>

emoleumassi
<p>i resolve it with port forwarding: <code>kubectl port-forward &lt;pod-name&gt; 3306:3306 -n default</code></p>
