Configuring Kubernetes Stateful Set

Generally, Kubernetes deployment is used to deploy applications. In contrast, a Stateful set is used to deploy Stateful applications, and all applications are made up of different states, depending on how they were configured. Databases, message queues, caches, etc., have a higher degree of State because they rely on storage. Authentication happens when users log into a website using their username and password. After authentication, the session ID and cookie are stored in the browser. For any subsequent requests that go into your web server,  the web server can identify those requests and server the website components to any user.

Kubernetes Stateful Sets is a pod controller that always maintains its identity even after rescheduling the pods deployed, scaled, and deleted in a guaranteed order. Every Kubernetes pod in a Stateful set gets its stable storage. You can use a Stateful set for applications that require a stable name and state. The difference between stateful and replica sets is that when pods are created by replicas and rescheduled, they get a new name and IP address. The stateful set ensures that the pod keeps the same name and the same IP address, even when it gets rescheduled. Stateful sets also allow you to run multiple pod replicas; some replicas are not the same and can have their own set of volume claims or, more specifically, their own set of volume claim templates. When we use a Stateful set, each pod gets its persistent volume, and when the pods are restarted, it gets attached to the same persistent volume with the fixed name. The replicas also do not have random names; each pod gets assigned an index.

Let’s see how you can run MySQL using a Stateful Set

  • Create your Persistent Volumes for your MySQL Stateful Set.
apiVersion: v1
kind: PersistentVolume
metadata:
 name: mysql1
spec:
 accessModes:
   - ReadWriteOnce
 storageClassName: manual
 capacity:
   storage: 2Gi
 hostPath:
   path: /mysql1
  • This will create two persistent volumes in your Node pool.
  • View all running persistent volumes using the kubectl get pv command.

  • Next, view all running Persistent Volume Claim using the kubectl get pvc command.
  • Next, create the secret for your node, which will be applied to the Stateful Set.
apiVersion: v1
kind: Secret
metadata:
 name: mysql-password
type: opaque
stringData:
 MYSQL_ROOT_PASSWORD: password
  • Apply the changes on your MySQL secret using the kubectl apply -f secret.yaml command.
  • Next, create your Stateful set for your Kubernetes resources with the headless Service. In the VolumeClaim template section, we define the persistent volume claim. So, when your Stateful set wants to create a new replica, it will use the template to create the persistent volume, and persistent volume claim resource for each part separately. When you’re done with the configuration, apply the changes using kubectl apply statefulset.yaml
apiVersion: v1
kind: Service
metadata:
 name: mysql
 labels:
   app: mysql
spec:
 ports:
 - port: 3306
 clusterIP: None
 selector:
   app: mysql


---


apiVersion: apps/v1
kind: StatefulSet
metadata:
 name: mysql-set
spec:
 selector:
   matchLabels:
     app: mysql
 serviceName: "mysql"
 replicas: 2
 template:
   metadata:
     labels:
       app: mysql
   spec:
     terminationGracePeriodSeconds: 10
     containers:
     - name: mysql
       image: mysql:5.7
       ports:
       - containerPort: 3306
       volumeMounts:
       - name: mysql-store
         mountPath: /var/lib/mysql
       env:
         - name: MYSQL_ROOT_PASSWORD
           valueFrom:
             secretKeyRef:
               name: mysql-password
               key: MYSQL_ROOT_PASSWORD
 volumeClaimTemplates:
 - metadata:
     name: mysql-store
   spec:
     accessModes:
       - ReadWriteOnce
     storageClassName: "manual"
     resources:
       requests:
         storage: 5Gi

  • Next, view your pods you created using the StatefulSet.  You’ll see that your pods are running, and some will also be creating with the ContainerCreating tag.
  • View all running pods using the kubectl get pods command.
  • Next, connect the MySQL server using the pods you created.
apiVersion: v1
kind: Pod
metadata:
  name: mysql-client
spec:
  containers:
  - name: mysql-container
    image: alpine
    command: ['sh','-c', "sleep 1800m"]
    imagePullPolicy: IfNotPresent

  • Next, confirm if your pod created using the kubectl get pods command

  • Login to your pod using the exec command kubectl exec it mysql-client /bin/sh
  • Next, add the MySQL client using by typing the apk add mysql-client in your terminal.

  • Connect to the MySQL instance using mysql -u root -p -h mysql-set-0.mysql.defualt. You’ll see that you’re logged on to your Server. After logging in, you can start creating your databases and tables.

Conclusion

Stateful sets create pods with stable identities and network identities because the host name never changes. Every pod name in a Stateful set combines the Stateful sets name and a number in an orderly sequence. With our Kubernetes headless service workflows, you can simply provide and enable a stable network identity for thousands of pods with just a simple command in a guaranteed order.