<p>I have a Docker container with MariaDB running in Microk8s (running on a single Unix machine).</p>
<pre><code># Hello World Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mariadb
spec:
  selector:
    matchLabels:
      app: mariadb
  template:
    metadata:
      labels:
        app: mariadb
    spec:
      containers:
      - name: mariadb
        image: mariadb:latest
        env:
        - name: MARIADB_ROOT_PASSWORD
          value: sa
        ports:
        - containerPort: 3306
</code></pre>
<p>These are the logs:</p>
<pre><code>(...)
2021-09-30  6:09:59 0 [Note] mysqld: ready for connections.
Version: '10.6.4-MariaDB-1:10.6.4+maria~focal'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
</code></pre>
<p>Now,</p>
<ul>
<li>connecting to port 3306 on the machine does not work.</li>
<li>connecting after exposing the pod with a service (any type) on port 8081 also does not work.</li>
</ul>
<p>How can I get the connection through?</p>

DomJourneyman
<p>The answer has been written in comments section, but to clarify I am posting here solution as Community Wiki.</p>
<p>In this case problem with connection has been resolved by setting <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#selector" rel="nofollow noreferrer"><code>spec.selector</code></a>.</p>
<blockquote>
<p>The <code>.spec.selector</code> field defines how the Deployment finds which Pods to manage. In this case, you select a label that is defined in the Pod template (<code>app: nginx</code>).</p>
</blockquote>
<blockquote>
<p><code>.spec.selector</code> is a required field that specifies a <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="nofollow noreferrer">label selector</a> for the Pods targeted by this Deployment.</p>
</blockquote>
