kubernetes statefulset

Principles

  • ensure the reproducibility:
    • for kubernetes pods of the same application to communicate with each other
    • find their storage
  • use case
    • Consul
    • Elasticsearch
    • Cassandra
  • StatefulSet keep a sticky identify for their kubernetes pods

Identity stickiness

Error management

Storage

Declaration

---
apiVersion: v1
kind: Service
metadata:
  name: db-cluster-headless-svc
  labels:
    app: db-cluster
spec:
  selector:
    app: db-cluster
  clusterIP: None
  ports:
    - port: 8080
      name: db
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: db-cluster
  labels:
    app: db-cluster
spec:
  replicas: 2
  serviceName: db-cluster-headless-svc
  selector:
    matchLabels:
      app: db-cluster
  template:
    metadata:
      labels:
        app: db-cluster
    spec:
      containers:
        - name: db
          image: foobar/k8s-training-statefulset:v1
          ports:
            - name: db
              containerPort: 8080
          volumeMounts:
            - name: data
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 50Mi