<p>I have a StatefulSet that matchs the one described in the
<a href="https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/#deploy-mysql" rel="nofollow noreferrer">StatefulSet Kubernetes tutorial</a> which creates a mysql master/slaves structure. I have the following kubernetes objects:</p>

<pre><code>    NAME          READY   STATUS    RESTARTS   AGE
pod/mysql-0   2/2     Running   7          23h
pod/mysql-1   2/2     Running   6          23h
pod/mysql-2   2/2     Running   6          23h

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/kubernetes   ClusterIP   10.152.183.1    &lt;none&gt;        443/TCP    21d
service/mysql        ClusterIP   None            &lt;none&gt;        3306/TCP   23h
service/mysql-read   ClusterIP   10.152.183.89   &lt;none&gt;        3306/TCP   23h

NAME                     READY   AGE
statefulset.apps/mysql   3/3     23h
</code></pre>

<p>EDIT: The mysql and mysql-read services connect with all the mysql pods.</p>

<p>The node is on my computer, I'm using microk8s.</p>

<p>Now I have a little program runnning on my computer (not in the cluster) that I would like to connect to the master (mysql-0) inside the StatefulSet. Id need something like a service that only connect with mysql-0. <strong>Any suggestions on how to do it?</strong></p>

<p>EDIT: The idea is to find a solution that allow me to deploy the cluster just with the .yaml files. It isn't interesting to find one that involves more commands than <code>kubectl apply</code></p>

<p>The program is the following one:</p>

<pre><code>import mysql.connector

mydb = mysql.connector.connect(
  host="localhost??",
  user="root",
  passwd="",
  database="testtable"
)

mycursor = mydb.cursor()

sql = "INSERT INTO testtable (name) VALUES (%s)"
val = ("holaquetal")
mycursor.execute(sql, val)

mydb.commit()
</code></pre>

<p>I thought I could add a different label to the mysql-0 pod and add a NodePort service that looks for that label, but I dont want to do it by command line. <strong>Is it possible to add one label to mysql-0 inside the StatefulSet yaml?</strong></p>

<p>Another idea could be to do a query DNS to the DNS server that microk8s provides, looking for "mysql.mysql-0", but I don't know how to connect the program to the DNS server, so that I can use <code>host=mysql.mysql-0</code> or the whole CNAME.</p>

Manu Ruiz Ruiz
<p>If the node is on your computer so can use <code>port-forward</code> to forward the port of mysql from the pod to your local. Try this:</p>

<p><code>kubectl port-forward mysql-0 3306:3306</code></p>

<p>or forward directly to your master service</p>

<p><code>kubectl port-forward svc/mysql-read 3306:3306</code></p>
