- Create a K8S cluster (Please check -> https://github.com/mehmetmgrsl/k8s-prometheus-grafana)
kubectl apply -f mysql-cm.yaml
# Create the ConfigMapkubectl apply -f mysql-svcs.yaml
# Create Serviceskubectl apply -f mysql-statefulset.yaml
# Create the StatefulSet
>>> kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-0 2/2 Running 0 8m25s
mysql-1 2/2 Running 0 5m6s
mysql-2 2/2 Running 0 115s
- Send a query to writer
kubectl run mysql-client --image=mysql:5.7 -i --rm --restart=Never --\
mysql -h mysql-0.mysql <<EOF
CREATE DATABASE test;
CREATE TABLE test.messages (message VARCHAR(250));
INSERT INTO test.messages VALUES ('hello');
EOF
- Send a query to reader pool
kubectl run mysql-client --image=mysql:5.7 -i -t --rm --restart=Never --\
mysql -h mysql-read -e "SELECT * FROM test.messages"
+---------+
| message |
+---------+
| hello |
+---------+
kubectl delete pod mysql-1
The StatefulSet also recreates Pods if they're deleted.
The StatefulSet controller notices that no mysql-1
Pod exists anymore, and creates a new one with the same name and linked to the same PersistentVolumeClaim.
kubectl delete pod mysql-1
pod "mysql-1" deleted
kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-0 2/2 Running 0 18m
mysql-1 2/2 Running 0 37s # created again
mysql-2 2/2 Running 0 12m