<p>I am working on a Kubernetes project which I am trying to add a MySQL replicated database to. I am basing it off this tutorial (<a href="https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/</a>).</p>
<p>If I wanted to give a flask app access to this mysql database that is created, is it possible to do so through an normal connection? If so what value should I use for the host?</p>
<p>Here is relevant code that is going to create a connection from the flask app:</p>
<pre><code>import pymysql.cursors

connection = pymysql.connect(host='mysql-controller:3306',
                             user='root',
                             password='',
                             database='pod1out',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)


def test_get():
    with connection.cursor() as cursor:
        # Read a single record
        sql = &quot;SELECT * FROM `User`&quot;
        cursor.execute(sql, ('webmaster@python.org',))
        result = cursor.fetchone()
        print(result)
</code></pre>
<p>Here are the relevant sections of the yaml file</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  namespace: thesis
  labels:
    app: mysql
data:
  primary.cnf: |
    [mysqld]
    log-bin
  replica.cnf: |
    [mysqld]
    super-read-only
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: thesis
  labels:
    app: mysql
spec:
  ports:
    - name: mysql
      port: 3306
  clusterIP: None
  selector:
    app: mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-controller
  namespace: thesis
  labels:
    app: mysql
spec:
  ports:
    - name: mysql
      port: 3306
  selector:
    app: mysql
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  namespace: thesis
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 3
  template:
    metadata:
      labels:
        app: mysql
    spec:
      initContainers:
        - name: init-mysql
          image: mysql:5.7
          command:
            - bash
            - &quot;-c&quot;
            - |
              set -ex
              [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
              ordinal=${BASH_REMATCH[1]}
              echo [mysqld] &gt; /mnt/conf.d/server-id.cnf
              echo server-id=$((100 + $ordinal)) &gt;&gt; /mnt/conf.d/server-id.cnf
              if [[ $ordinal -eq 0 ]]; then
                cp /mnt/config-map/primary.cnf /mnt/conf.d/
              else
                cp /mnt/config-map/replica.cnf /mnt/conf.d/
              fi
          volumeMounts:
            - name: conf
              mountPath: /mnt/conf.d
            - name: config-map
              mountPath: /mnt/config-map
        - name: clone-mysql
          image: gcr.io/google-samples/xtrabackup:1.0
          command:
            - bash
            - &quot;-c&quot;
            - |
              set -ex
              [[ -d /var/lib/mysql/mysql ]] &amp;&amp; exit 0
              [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
              ordinal=${BASH_REMATCH[1]}
              [[ $ordinal -eq 0 ]] &amp;&amp; exit 0
              ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
              xtrabackup --prepare --target-dir=/var/lib/mysql
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
              subPath: mysql
            - name: conf
              mountPath: /etc/mysql/conf.d
      containers:
        - name: mysql
          image: mysql:5.7
          env:
            - name: MYSQL_ALLOW_EMPTY_PASSWORD
              value: &quot;1&quot;
          ports:
            - name: mysql
              containerPort: 3306
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
              subPath: mysql
            - name: conf
              mountPath: /etc/mysql/conf.d
          resources:
            requests:
              cpu: 500m
              memory: 1Gi
          livenessProbe:
            exec:
              command: [&quot;mysqladmin&quot;, &quot;ping&quot;]
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
          readinessProbe:
            exec:
              # Check we can execute queries over TCP (skip-networking is off).
              command: [&quot;mysql&quot;, &quot;-h&quot;, &quot;127.0.0.1&quot;, &quot;-e&quot;, &quot;SELECT 1&quot;]
            initialDelaySeconds: 5
            periodSeconds: 2
            timeoutSeconds: 1
        - name: xtrabackup
          image: gcr.io/google-samples/xtrabackup:1.0
          ports:
            - name: xtrabackup
              containerPort: 3307
          command:
            - bash
            - &quot;-c&quot;
            - |
              set -ex
              cd /var/lib/mysql

              if [[ -f xtrabackup_slave_info &amp;&amp; &quot;x$(&lt;xtrabackup_slave_info)&quot; != &quot;x&quot; ]]; then
                cat xtrabackup_slave_info | sed -E 's/;$//g' &gt; change_master_to.sql.in
                rm -f xtrabackup_slave_info xtrabackup_binlog_info
              elif [[ -f xtrabackup_binlog_info ]]; then
                [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
                rm -f xtrabackup_binlog_info xtrabackup_slave_info
                echo &quot;CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
                      MASTER_LOG_POS=${BASH_REMATCH[2]}&quot; &gt; change_master_to.sql.in
              fi

              if [[ -f change_master_to.sql.in ]]; then
                echo &quot;Waiting for mysqld to be ready (accepting connections)&quot;
                until mysql -h 127.0.0.1 -e &quot;SELECT 1&quot;; do sleep 1; done

                echo &quot;Initializing replication from clone position&quot;
                mysql -h 127.0.0.1 \
                      -e &quot;$(&lt;change_master_to.sql.in), \
                              MASTER_HOST='mysql-0.mysql', \
                              MASTER_USER='root', \
                              MASTER_PASSWORD='', \
                              MASTER_CONNECT_RETRY=10; \
                            START SLAVE;&quot; || exit 1
                mv change_master_to.sql.in change_master_to.sql.orig
              fi

              exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
                &quot;xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root&quot;
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
              subPath: mysql
            - name: conf
              mountPath: /etc/mysql/conf.d
          resources:
            requests:
              cpu: 100m
              memory: 100Mi
      volumes:
        - name: conf
          emptyDir: {}
        - name: config-map
          configMap:
            name: mysql
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: [&quot;ReadWriteOnce&quot;]
        resources:
          requests:
            storage: 10Gi
</code></pre>

EoinHanan
<p>i am not sure do you mean by the normal connection.</p>
<p>however if both the flask app and the MySQL database are running in the same Kubernetes cluster you can use to <strong>service name</strong> as the host of MySQL.</p>
<p>In your case service name is <code>mysql-controller</code> so you can use it as the host. if your both the flask app and MySQL are not in same namespace it might it won't work just with the service name in that case you have to pass the full-service name.</p>
<p>For example</p>
<p>MySQL running in <strong>thesis</strong> namespace you can give hostname as : <strong>mysql-controller.thesis.svc.cluster.local</strong></p>
<p>you can use the service name <strong>mysql</strong> as both of your services have the same label and selectors.</p>
