StatefulSets

StatefulSets are similar to Deployment with features like

  1. Ordered creation and deletion of pods
  2. Unique network identity and name

and other features.

Pod names are suffixed with sequence index number to make name predictable.

Resource file similar to Deployment as shown below.

mysql.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysqldb
spec:
  template:
    metadata:
      labels:
        type: db
    spec:
      containers:
        - name: mysql
          image: mysql
          env:
            - name: MYSQL_ALLOW_EMPTY_PASSWORD
              value: "yes"
  replicas: 2
  selector:
    matchLabels:
      type: db
  serviceName: mysqldb
  podManagementPolicy: Parallel # for pod creation parallely

Creating StatefulSet by running command k create -f mysql.yaml creates 2 pods. We can access those pod individually using headless service mysqldb at urls mysql-0.mysqldb.default.svc.cluster.local and mysql-1.mysqldb.default.svc.cluster.local.

References

  1. https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/