<p>Hi I've deployed single MySQL db instance in Azure via the YAML file in Azure Kubernetes service. I can get into the container via CLI when I'm inside my cluster. I would like to connect with db instance via external client like MySQL Workbench or Sqlelectron or others, outside the cluster. As I found out it's possible via correctly exposing DB instance by Service configuration.</p>
<p>My deployment of single instance MySQL DB instance is:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
    name: mysql-db-testing-service
    namespace: testing
spec:
    type: ClusterIP
    ports:
    - port: 3306
      #targetPort: 3306
    selector:
        app: mysql-db-testing
---
apiVersion: apps/v1
kind: Deployment
metadata:
    name: mysql-db-testing
    namespace: testing
spec:
    selector:
        matchLabels:
            app: mysql-db-testing
    replicas: 1
    strategy:
        type: Recreate
    template:
        metadata:
            labels:
                app: mysql-db-testing
        spec:
            containers: 
            - name: mysql-db-container-testing 
              image: mysql:8.0.31
              env: 
              - name: MYSQL_ROOT_PASSWORD
                value: test12345
              ports:
              - containerPort: 3306
                name: mysql-port
              volumeMounts:
              - mountPath: &quot;/var/lib/mysql&quot;
                name: mysql-persistent-storage
            volumes:
            - name: mysql-persistent-storage
              persistentVolumeClaim:
                claimName: azure-managed-disk-pvc-mysql-testing
            nodeSelector:
                env: preprod 
</code></pre>
<p>As I've mentioned I can get to the container via CLI:</p>
<p><a href="https://i.stack.imgur.com/TDQtX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TDQtX.png" alt="enter image description here" /></a></p>
<p>Console output regarding the working pod with db looks like:
<a href="https://i.stack.imgur.com/Cz7vj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Cz7vj.png" alt="enter image description here" /></a></p>
<p>Console output regarding the service:</p>
<p><a href="https://i.stack.imgur.com/BIBad.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BIBad.png" alt="v" /></a></p>
<p>Is there something missing in my deployment YAML file or maybe there are missing some fields? How can I expose db to the outside world? I would be grateful for help.</p>

<p>You have used the <strong>type: ClusterIP</strong> so it won't expose the MYSQL outside the cluster ideally, your Microservices running in the cluster will be able to access it however you can not use it externally.</p>
<p>To expose the service we generally have to use the <strong>type: LoadBalancer</strong>. It will directly expose your <strong>MySQL</strong> service internet and from your <strong>local</strong> <strong>workbench</strong>, you can connect to <strong>DB</strong> running on K8s.</p>
<p>If you really don't want to expose the MySQL service directly to internet you can deploy the <strong>adminer</strong>.</p>
<p>So traffic will flow like</p>
<pre><code>internet &gt; adminer &gt; internal communication &gt; MySQL service &gt; MySQL POD
</code></pre>
<p>YAML file to deploy and get the UI output directly in the browser, it will ask of <strong>MySQL</strong> DB <strong>username</strong>, <strong>password</strong>, Host (mysql-db-testing-service.testing.svc.cluster.local) to connect</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: adminer
  labels:
    app: adminer
spec:
  selector:
    matchLabels:
      app: adminer
  template:
    metadata:
      labels:
        app: adminer
    spec:
      containers:
        - name: adminer
          image: adminer:4.6.3
          ports:
            - containerPort: 8080
          env:
            - name: ADMINER_DESIGN
              value: &quot;pappu687&quot;
---
apiVersion: v1
kind: Service
metadata:
  name: adminer-svc
spec:
  type: ClusterIP(Internally to cluster)/LoadBalancer (Expose to internet)
  selector:
    app: adminer
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
</code></pre>
<p>Port-forward for local access or use service <code>type: LoadBalancer</code></p>
<pre><code>kubectl port-forward svc/adminer-svc 8080:8080
</code></pre>
<p>Open <code>localhost:8080</code> in browser</p>
