<p>I'm newbie to k8s and trying to understand how we can connect the external application(i.e outside kubernetes cluster) to db pod. Please consider external application as my standalone java program(JDBC program)</p>
<p>Eg : I have myserver server pod in my k8s cluster and I can access it using 'kubectl exec...'.</p>
<p>Now, If I want to use mysql server and connect to my external application like standalone java jdbc application to perform simple CRUD operation, how do we connect to mysql pod ? What could be the connection string for the same?</p>
<p>Do I need to first expose mysql server pod as nodeport service and use that IP:port in my connection string? Or is there any other way. Any hint or help would be appreciated.</p>
<p>Thanks in advance !</p>

Newbie
<p>1st, you need to create a service in K8s which routes traffic from client to your mysql pods.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql # labels should be the same as the ones used in the Pod's definition.
  ports:
    - protocol: TCP
      port: 3306 # port of the service.
      targetPort: 3306 # Port that the pods have exposed.
</code></pre>
<p>Now, if your external app is running on local machine &amp; you want it to communicate with the database, use <code>kubectl port-forward</code> command which will create a local session between your machine &amp; K8s pods</p>
<pre><code># kubectl port-forward svc/&lt;service-name&gt; &lt;local-port&gt;:&lt;service-port&gt;
kubectl port-forward svc/mysql-service 3306:3306
</code></pre>
<p>For production, you will need to expose your mysql service over HTTP using Ingress. For this, you will need an Ingress Controller e.g Nginx and an Ingress resource.</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: db-ingress
spec:
  rules:
  - http:
      paths:
      - path: /db # path at which you want to expose your service
        pathType: Prefix
        backend:
          service:
            name: mysql-service # name of the service
            port:
              number: 3306 # port of the service
</code></pre>
<p>For reference, check out these links</p>
<ul>
<li><a href="https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service</a></li>
<li><a href="https://kubernetes.io/docs/concepts/services-networking/ingress/#the-ingress-resource" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/ingress/#the-ingress-resource</a></li>
<li><a href="https://kubernetes.github.io/ingress-nginx/" rel="nofollow noreferrer">https://kubernetes.github.io/ingress-nginx/</a></li>
<li><a href="https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/#forward-a-local-port-to-a-port-on-the-pod" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/#forward-a-local-port-to-a-port-on-the-pod</a></li>
</ul>
