How kubernetes service work in case of Database (mysql)?

11/14/2019

I'm new to k8 and i would like to create a mysql k8 service.

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?

  • will all the records will be synchronized to all pods?
  • while fetching will it collect records from all pods?

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 pod1 and for the http get request will to go for pod2 for get the record?

so how something like this will workout?

 enter image description here

-- Tony
database-design
docker
kubernetes
mysql
node.js

1 Answer

11/14/2019

You should use a StatefulSet 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

There is actually a statefulset example 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.

-- Patrick W
Source: StackOverflow