<p>I'm new in kubernetes and docker world :)</p>
<p>I try to deploy our application in docker in kubernetes, but i can't connect to external mysql database..
my steps:
1, Install kubernetes with kubeadm in our new server.
2, Create a docker image from our application with <code>mvn spring-boot:build-image</code>
3, I create a deployment and service yaml to use image.</p>
<p>Deployment YAML:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: &quot;1&quot;
  labels:
    app: demo-app
  name: demo-app
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-app
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: demo-app
    spec:
     containers:
     - image: demo/demo-app:0.1.05-SNAPSHOT
       imagePullPolicy: IfNotPresent
       name: demo-app-service
       env:
         - name: SPRING_DATASOURCE_URL
           value: jdbc:mysql://mysqldatabase/DBDEV?serverTimezone=Europe/Budapest&amp;useLegacyDatetimeCode=false
       ports:

       - containerPort: 4000
       volumeMounts:
       - name: uploads
         mountPath: /uploads
       - name: ssl-dir
         mountPath: /ssl
     volumes:
     - name: ssl-dir
       hostPath:
         path: /var/www/dev.hu/backend/ssl
     - name: uploads
       hostPath:
         path: /var/www/dev.hu/backend/uploads
     restartPolicy: Always
</code></pre>
<p>Service YAML:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  labels:
    app: demo-app
  name: demo-app
  namespace: default
spec:
  ports:
    - port: 4000
      name: spring
      protocol: TCP
      targetPort: 4000
  selector:
    app: demo-app
  sessionAffinity: None
  type: LoadBalancer
</code></pre>
<p>4, Create an endpoints and Service YAML, to communicate to outside:</p>
<pre><code>kind: Endpoints
apiVersion: v1
metadata:
  name: mysqldatabase
subsets:
  - addresses:
    - ip: 10.10.0.42
    ports:
    - port: 3306
---
kind: Service
apiVersion: v1
metadata:
  name: mysqldatabase
spec:
  type: ClusterIP
  ports:
  - port: 3306
    targetPort: 3306
</code></pre>
<p>But it's not working, when i going to see logs i see spring cant connect to database.</p>
<pre><code>Caused by: java.net.UnknownHostException: mysqldatabase
        at java.net.InetAddress.getAllByName0(InetAddress.java:1281)
        at java.net.InetAddress.getAllByName(InetAddress.java:1193)
        at java.net.InetAddress.getAllByName(InetAddress.java:1127)
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
</code></pre>
<p>thanks for any helps</p>

<p>hold on. you don't create endpoints yourself. endpoints are registered by kubernetes when a service has matching pods. right now, you have deployed your application and exposed it via a service.</p>
<p>if you want to connect to your mysql database via service it needs to be deployed and kubernetes as well. if it is not hosted on kubernetes you will need a hostname or the ip address of the database and adapt your SPRING_DATASOURCE_URL accordingly!</p>
