<p>I'm new to k8 and i would like to create a mysql k8 service. </p>

<p>So my question is, if i create a k8 service with multiple pods which has mysql database, how the data will be inserted and also how fetch request processed?</p>

<ul>
<li>will all the records will be synchronized to all pods?</li>
<li>while fetching will it collect records from all pods?</li>
</ul>

<p>after little bit of googling i came to know that some load balancing kinda thing is happening in background...so suppose if a record is saved to mysql database which is in <strong>pod1</strong> and for the http get request will to go for <strong>pod2</strong> for get the record?</p>

<p>so how something like this will workout?</p>

<p><a href="https://i.stack.imgur.com/axIPE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/axIPE.png" alt=" enter image description here"></a></p>

<p>You should use a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a> for this purpose. Statefulsets include the ability to specify a pod to reach through the service using the internal DNS. An example of this using a statefulset called mysql being exposed using a service called mysql in the default namesapce, you can reach a specific pod (such as the master for writes) using mysql-0.mysql.default.svc.cluster.local</p>

<p>There is actually a <a href="https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/" rel="nofollow noreferrer">statefulset example</a> that uses mysql specifically on kubernetes.io. This example leverages the properties of the statefulset to ensure that pod-0 will be your master so you can reliably send writes to it and send read requests to one of the read-only servers.</p>
