$ for i in 0 1 2; do kubectl exec "mariadb-sts-$i" -- bash -c "hostname"; done
mariadb-sts-0
mariadb-sts-1
mariadb-sts-2
$ for i in 0 1 ; do kubectl exec "mariadb-sts-$i" -- hostname -f; done
mariadb-sts-0.mariadb-service.default.svc.cluster.local
mariadb-sts-1.mariadb-service.default.svc.cluster.local
$ kubectl get pods -w
NAME READY STATUS RESTARTS AGE
mariadb-sts-0 1/1 Running 0 4m27s
mariadb-sts-1 1/1 Running 0 4m21s
mariadb-sts-2 1/1 Running 0 4m16s
mariadb-sts-2 1/1 Terminating 0 4m38s
# Scale up
$ kubectl scale sts mariadb-sts --replicas=4
# Watch the Pods
$ kubectl get pods -w
NAME READY STATUS RESTARTS AGE
mariadb-sts-0 1/1 Running 0 7m19s
mariadb-sts-1 1/1 Running 0 7m13s
mariadb-sts-2 0/1 Pending 0 0s
mariadb-sts-2 0/1 Pending 0 0s
mariadb-sts-2 0/1 ContainerCreating 0 0s
mariadb-sts-2 1/1 Running 0 5s
mariadb-sts-3 0/1 Pending 0 0s
mariadb-sts-3 0/1 Pending 0 0s
mariadb-sts-3 0/1 Pending 0 1s
mariadb-sts-3 0/1 ContainerCreating 0 1s
mariadb-sts-3 1/1 Running 0 4s
# Alternatively here we can use the kubectl edit command to change the number of replicas
We can conclude that when scaling down, the last replica is terminated, while when scaling up the number prefix is increased in an ordinal way, and no Pod is created after the previous Pod is in the “Running” state (compare Pod 2 and 3).
Pods can be deleted with the kubectl delete command and will be recreated with the same ordinal number prefix
# Since Pods retain their sticky identity
# let's remove Pod with ordinal index 0
$ kubectl delete pod mariadb-sts-0
pod "mariadb-sts-0" deleted
# Watch the Pods during deletion
$ kubectl get pods -w
NAME READY STATUS RESTARTS AGE
mariadb-sts-0 1/1 Running 0 31m
mariadb-sts-1 1/1 Running 0 31m
mariadb-sts-2 1/1 Running 0 24m
mariadb-sts-3 1/1 Running 0 24m
mariadb-sts-0 1/1 Terminating 0 31m
mariadb-sts-0 0/1 Terminating 0 31m
mariadb-sts-0 0/1 Terminating 0 31m
mariadb-sts-0 0/1 Terminating 0 31m
mariadb-sts-0 0/1 Pending 0 0s
mariadb-sts-0 0/1 Pending 0 0s
mariadb-sts-0 0/1 ContainerCreating 0 0s
mariadb-sts-0 1/1 Running 0 4s
Since PersistentVolumeClaims are dynamically provisioned, we can look for them too
$ kubectl get pvc -l app=mariadb
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
datadir-mariadb-sts-0 Bound pvc-bdc2be42-f3b6-467e-a3b2-ad5bf882c556 300M RWO standard 33m
datadir-mariadb-sts-1 Bound pvc-f05d44c6-0bfe-4ba4-a6c4-8312341b8367 300M RWO standard 33m
datadir-mariadb-sts-2 Bound pvc-6573cd4d-8e83-4273-8764-33fb2bce6633 300M RWO standard 33m
Each Pod has its own persistent volume created, so let’s test that. Create sample data in the first Pod
$ kubectl exec -it mariadb-sts-0 -- mariadb -uroot -psecret -e "create database if not exists mytest0; use mytest0; create table t(t int); insert into t values (1),(2); select * from t;"
+------+
| t |
+------+
| 1 |
| 2 |
+------+