Statefulset is a resource in Kubernetes that we use for stateful applications. It is a special type of Replicaset object for applications running in our cluster. Statefulset provide consistency and predictability to application deployment with stateful data. By default containers are inherently ephemeral, when a container dies it loses all the data or content running on it. we use volumes to persist the state of containers running in a pod after the pod dies.A storage class is needed for dynamic provisioning purposes whenever we use persistent volumes with Statefulset. We use
volumeClaimTemplates to map to a storage that will be used for provisioning and storing data when using statefulset. Statefulset is capable of preserving a Persistent volume while a pod is being replaced. When statefulset are created the pods associated to the stateful applications are assigned an ordered numbers starting from 0 to N. Also ,when a Statefulset is terminated the pods are terminated from N to 0 by default. we can specify
podManagementPolicy to be equal to
Parallel if we want to delete all pods ate the same time. If a single pod in the statefulset is deleted, another one will be created. Due to how the Statefulset are named, Kubernetes easily associates specific pods with the network names and persistent volumes. Below is an example of a statefulset.
The service that will be used by the Statefulset Pods.
apiVersion: v1 kind: Service metadata: name: atribe-svc labels: app: hello spec: ports: - port: 80 name: web clusterIP: None selector: app: hello
StatefulSet Configuration with 3 replicas.
kind: StatefulSet apiVersion: apps/v1beta1 metadata: name: Aquatribe-StatefulSet spec: serviceName: atribe-svc replicas: 3 template: metadata: labels: app: hello spec: terminationGracePeriodSeconds: 10 containers: - name: hello image: jonbaier/httpwhalesay:0.2 command: ["node", "index.js", "Hello World!."] ports: - containerPort: 80 name: web volumeMounts: - name: www mountPath: /usr/share/nginx/html volumeClaimTemplates: - metadata: name: www annotations: volume.beta.kubernetes.io/storage-class: solidstate spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi