StatefulSet:
1. StatefulSet is the workload API object used to manage stateful applications.
Manages the deployment and scaling of a set of Pods, and provides guarantees about the
ordering and uniqueness of these Pods
2.
Like a Deployment, a StatefulSet manages Pods that are based on an identical container spec. Unlike a
Deployment, a StatefulSet maintains a sticky identity for each of their Pods. These pods are created from the
same spec, but are not interchangeable: each has a persistent identifier that it maintains across any
rescheduling.
3.
Example:
Let’s just start with a simple example and we are tasked to deploy a database server. So we install and setup
MySQL on the server and create a database. Our database is now operational. Other applications can now
write data to our database.
1.
To withstand failures we are tasked to deploy a High Availability solution. So we deploy additional servers
and install MySQL on those as well. We have a blank database on the new servers.
2.
So how do we replicate the data from the original database to the new databases on the new servers.
Before we get into that,
3.
4. So back to our question on how do we replicate the database to the databases in the new server.
5. There are different topologies available.
The most straight forward one being a single master multi slave topology, where all writes come in to the
master server and reads can be served by either the master or any of the slaves servers.
So the master server should be setup first, before deploying the slaves. Once the slaves are deployed,
perform an initial clone of the database from the master server to the first slave.
6.
After the initial copy enable continuous replication from the master to that slave so that the database on the
slave node is always in sync with the database on the master.
7.
Note that both these slaves are configured with the address of the master host. When replication is
initialized you point the slave to the master with the master’s hostname or address.
8.
9. That way the slaves know where the master is.
𝐒𝐭𝐚𝐭𝐞 𝐏𝐫𝐞𝐬𝐞𝐫𝐯𝐚𝐭𝐢𝐨𝐧:
StatefulSets are designed to manage stateful applications, which means they maintain the state of the application between restarts or rescheduling of pods.
2. 𝐒𝐭𝐚𝐛𝐥𝐞 𝐍𝐞𝐭𝐰𝐨𝐫𝐤 𝐈𝐝𝐞𝐧𝐭𝐢𝐭𝐲:
Each pod in a StatefulSet gets a unique and stable hostname, like web-0, web-1, based on the ordinal index. This helps in predictable scaling and deletion.
3. 𝐒𝐭𝐚𝐛𝐥𝐞 𝐒𝐭𝐨𝐫𝐚𝐠𝐞:
StatefulSets ensure that the volume (or persistent storage) is attached to the correct pod, no matter where the pod gets scheduled in the cluster.
4. 𝐎𝐫𝐝𝐞𝐫𝐞𝐝 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬:
Scaling up or down and updates in StatefulSets are done in a predictable manner. For instance, if you have three replicas (web-0, web-1, web-2), they will be terminated in the order of web-2, web-1, web-0.
5. 𝐔𝐬𝐞 𝐂𝐚𝐬𝐞 𝐰𝐢𝐭𝐡 𝐃𝐚𝐭𝐚𝐛𝐚𝐬𝐞𝐬:
For databases, where the order of operations (like startup, shutdown) and data persistency are crucial, StatefulSets offer advantages. For instance, in a multi-node database cluster, you might want a certain sequence to ensure data consistency.