<p>I'm new to k8s, using docker-mac-desktop k8s test spring boot app.</p>

<p>I'm can connect success when spring boot in local connect mysql.</p>

<p>transform to docker k8s can not connected.</p>

<p>error is:</p>

<pre><code>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.&lt;init&gt;(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]
</code></pre>

<p>is is local listening port:</p>

<pre><code>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)
</code></pre>

<p>spring boot database config:</p>

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

<p>spring boot pod.yml:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: user-service
  labels:
    app: user-service
spec:
  containers:
    - name: user-service
      image: user_service:v1
</code></pre>

<p>map database ref: <a href="https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services" rel="nofollow noreferrer">https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services</a></p>

<p>mysql_svc.yml:</p>

<pre><code>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
</code></pre>

<p>while the ip is come from <code>ifconfig</code></p>

<pre><code>en0: flags=8863&lt;UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500
    options=400&lt;CHANNEL_IO&gt;
    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&lt;PERFORMNUD,DAD&gt;
    media: autoselect
    status: active
</code></pre>

<p>how can connect from k8s pod connect localhost database? :)</p>

F___
<p>I'm solved this issue.</p>

<p>changed mysql_svc.yml</p>

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

<p>map local port</p>

<p>change datasource config:</p>

<pre><code>spring.datasource.url=jdbc:mysql://mysqldb/foo
</code></pre>

<p>then connect success.</p>

<p>ref <a href="https://stackoverflow.com/questions/49289009/during-local-development-with-kubernetes-minikube-how-should-i-connect-to-postg?rq=1">answer</a></p>
