<p>I am using minikube to deploy mysql and spring bootapp in the same namespace. For some reason I am unable to connect to the database from the app pod. Posting it here as I don't know where to go from here. Appreciate any help.</p>
<p><strong>Can connect from my Laptop</strong></p>
<p>I am able to connect to same mysql on minikube from my laptop both through MySQL workbench and through mysql command. I can run the app on my machine and the app is able to apply liquibase migrations to the database in the pod.</p>
<p><strong>App cannot connect</strong></p>
<p>When I try to apply deployment.yaml file, spring throws a huge stacktrace but these two lines summarise it all I think.</p>
<p><code>The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.</code></p>
<p><code>Caused by: java.net.ConnectException: Connection refused</code></p>
<p><strong>Cannot connect even through MySQL client installed on the  app pod</strong></p>
<p>I installed MySQL client in the app pod using <code>apt-get install -y mysql-client</code> and then tried to connect to the MySQL db using the following commmands. I've also tried capture the dns log kubedns using <code>kubectl logs -n kube-system -l k8s-app=kube-dns</code>. The output of that command is attached below each mysql command.</p>
<p><em>Service name</em></p>
<pre><code>mysql -u root -h syms-mysql -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on 'syms-mysql:3306' (111)
</code></pre>
<p>log</p>
<pre><code>[INFO] 172.17.0.1:60559 - 27285 &quot;AAAA IN syms-mysql.syms.svc.cluster.local.cluster.local. udp 65 false 512&quot; NXDOMAIN qr,aa,rd 158 0.000323333s
[INFO] 172.17.0.1:60559 - 31377 &quot;A IN syms-mysql.syms.svc.cluster.local.cluster.local. udp 65 false 512&quot; NXDOMAIN qr,aa,rd 158 0.000450412s
[INFO] 172.17.0.1:1440 - 42659 &quot;A IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 100 0.000298098s
[INFO] 172.17.0.1:1440 - 17839 &quot;AAAA IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 144 0.000576619s
[INFO] 172.17.0.1:65385 - 46870 &quot;A IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 100 0.000476572s
[INFO] 172.17.0.1:65385 - 39937 &quot;AAAA IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 144 0.000647759s
[INFO] 172.17.0.1:3786 - 45586 &quot;AAAA IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 144 0.000515706s
[INFO] 172.17.0.1:3786 - 60960 &quot;A IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 100 0.00063402s
[INFO] 172.17.0.1:7212 - 14821 &quot;AAAA IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 144 0.000255618s
[INFO] 172.17.0.1:7212 - 53452 &quot;A IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 100 0.000563956s
</code></pre>
<p><em>DNS name</em></p>
<pre><code>bash-4.4# mysql -u root -h syms-mysql.syms.svc.cluster.local -p
Enter password: 
ERROR 2003 (HY000): Can't connect to MySQL server on 'syms-mysql.syms.svc.cluster.local:3306' (111)
</code></pre>
<p>log</p>
<pre><code>[INFO] 172.17.0.1:7212 - 14821 &quot;AAAA IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 144 0.000255618s
[INFO] 172.17.0.1:7212 - 53452 &quot;A IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 100 0.000563956s
[INFO] 172.17.0.1:45103 - 8087 &quot;AAAA IN syms-mysql.syms.svc.cluster.local.syms.svc.cluster.local. udp 74 false 512&quot; NXDOMAIN qr,aa,rd 167 0.001098626s
[INFO] 172.17.0.1:45103 - 25752 &quot;A IN syms-mysql.syms.svc.cluster.local.syms.svc.cluster.local. udp 74 false 512&quot; NXDOMAIN qr,aa,rd 167 0.000285655s
[INFO] 172.17.0.1:14956 - 9219 &quot;AAAA IN syms-mysql.syms.svc.cluster.local.svc.cluster.local. udp 69 false 512&quot; NXDOMAIN qr,aa,rd 162 0.000281106s
[INFO] 172.17.0.1:14956 - 1818 &quot;A IN syms-mysql.syms.svc.cluster.local.svc.cluster.local. udp 69 false 512&quot; NXDOMAIN qr,aa,rd 162 0.000927891s
[INFO] 172.17.0.1:14683 - 58630 &quot;AAAA IN syms-mysql.syms.svc.cluster.local.cluster.local. udp 65 false 512&quot; NXDOMAIN qr,aa,rd 158 0.000198551s
[INFO] 172.17.0.1:14683 - 7423 &quot;A IN syms-mysql.syms.svc.cluster.local.cluster.local. udp 65 false 512&quot; NXDOMAIN qr,aa,rd 158 0.000984013s
[INFO] 172.17.0.1:2028 - 8676 &quot;A IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 100 0.000984814s
[INFO] 172.17.0.1:2028 - 20460 &quot;AAAA IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 144 0.001115167s
</code></pre>
<p><em>Cluster IP</em></p>
<pre><code>bash-4.4# mysql -u root -h 10.110.201.105 -p
Enter password: 
ERROR 2003 (HY000): Can't connect to MySQL server on '10.110.201.105:3306' (111)
</code></pre>
<p>log</p>
<pre><code>[INFO] 172.17.0.1:7212 - 14821 &quot;AAAA IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 144 0.000255618s
[INFO] 172.17.0.1:7212 - 53452 &quot;A IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 100 0.000563956s
[INFO] 172.17.0.1:45103 - 8087 &quot;AAAA IN syms-mysql.syms.svc.cluster.local.syms.svc.cluster.local. udp 74 false 512&quot; NXDOMAIN qr,aa,rd 167 0.001098626s
[INFO] 172.17.0.1:45103 - 25752 &quot;A IN syms-mysql.syms.svc.cluster.local.syms.svc.cluster.local. udp 74 false 512&quot; NXDOMAIN qr,aa,rd 167 0.000285655s
[INFO] 172.17.0.1:14956 - 9219 &quot;AAAA IN syms-mysql.syms.svc.cluster.local.svc.cluster.local. udp 69 false 512&quot; NXDOMAIN qr,aa,rd 162 0.000281106s
[INFO] 172.17.0.1:14956 - 1818 &quot;A IN syms-mysql.syms.svc.cluster.local.svc.cluster.local. udp 69 false 512&quot; NXDOMAIN qr,aa,rd 162 0.000927891s
[INFO] 172.17.0.1:14683 - 58630 &quot;AAAA IN syms-mysql.syms.svc.cluster.local.cluster.local. udp 65 false 512&quot; NXDOMAIN qr,aa,rd 158 0.000198551s
[INFO] 172.17.0.1:14683 - 7423 &quot;A IN syms-mysql.syms.svc.cluster.local.cluster.local. udp 65 false 512&quot; NXDOMAIN qr,aa,rd 158 0.000984013s
[INFO] 172.17.0.1:2028 - 8676 &quot;A IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 100 0.000984814s
[INFO] 172.17.0.1:2028 - 20460 &quot;AAAA IN syms-mysql.syms.svc.cluster.local. udp 51 false 512&quot; NOERROR qr,aa,rd 144 0.001115167s
</code></pre>
<p><strong>MySQL Manifests</strong></p>
<p><em>Service</em></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: syms-mysql
spec:
  ports:
  - protocol: TCP
    port: 3306
    targetPort: 3306
  selector:
    app: syms-mysql-db
</code></pre>
<p><em>Deployment</em></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: syms-mysql
  labels:
    app: syms-mysql-db
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:8.0.31
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: syms-mysql-secret
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: syms-mysql-pvc
</code></pre>
<p><em>Persistent Volume</em></p>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: syms-mysql-pv
  namespace: syms
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: &quot;/mnt/data&quot;
</code></pre>
<p><em>Persistent Volume Claim</em></p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: syms-mysql-pvc
  namespace: syms
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
</code></pre>
<p><strong>App Manifests</strong></p>
<p><em>Deployment</em></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: syms-api
  namespace: syms
spec:
  selector:
    matchLabels:
      app: syms-api
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: syms-api
    spec:
      containers:
      - image: syms-api:0.0.1-SNAPSHOT
        name: syms-api
        env:
        - name: SPRING_DATASOURCE_PASSWORD
          value: test1234
        - name: SPRING_DATASOURCE_URL
          value: jdbc:mysql://syms-mysql:3306/syms?createDatabaseIfNotExist=true&amp;useSSL=false&amp;allowPublicKeyRetrieval=true
        - name: SPRING_DATASOURCE_USERNAME
          value: root
        ports:
        - containerPort: 8090
          name: syms-api
</code></pre>
<p><em>Service</em></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: syms-api
  namespace: syms
spec:
  ports:
  - port: 8090
  selector:
    app: syms-api
</code></pre>
<p><strong>Edit 1</strong></p>
<p>Attached below is the app docker file I am using.</p>
<pre><code>FROM adoptopenjdk/openjdk16

WORKDIR /code

ENV PORT 80

RUN java -version

RUN apt-get update &amp;&amp; \
    apt-get install -y mysql-client

ENTRYPOINT [&quot;tail&quot;, &quot;-f&quot;, &quot;/dev/null&quot;]
</code></pre>
<p><strong>Network Policies</strong></p>
<p>I wasn't sure if I needed to create any network policies, but this is what the documentation says.</p>
<p><code>By default, if no policies exist in a namespace, then all ingress and egress traffic is allowed to and from pods in that namespace. The following examples let you change the default behavior in that namespace.</code></p>
<p><a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/#default-policies" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/network-policies/#default-policies</a></p>

ShaggyInjun
<p>Please update mysql deployment's selector &quot;app: mysql&quot; and template labels to match the one specified in the service.</p>
