Kubernetes StatefulSets

prerequisites

What Is A StatefulSet

A StatefulSet Example

ZooKeeper Service

Headless Service

apiVersion: v1
kind: Service
metadata:
namespace: default
name: zk-hs
labels:
app: zk-hs
spec:
ports:
- port: 2888
name: server
- port: 3888
name: leader-election
clusterIP: None
selector:
app: zk

StatefulSet Spec

apiVersion: apps/v1
kind: StatefulSet
metadata:
namespace: default
name: zk

# StatefulSet spec
spec:
serviceName: zk-hs
selector:
matchLabels:
app: zk # It has to match .spec.template.metadata.labels
replicas: 5
podManagementPolicy: OrderedReady
updateStrategy:
type: RollingUpdate

# volumeClaimTemplates creates a Persistent Volume for each StatefulSet Pods.
volumeClaimTemplates:
- metadata:
name: datadir
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: standard
resources:
requests:
storage: 10Gi

# Pod spec
template:
metadata:
labels:
app: zk
spec:
affinity:
nodeAffinity:
...
podAntiAffinity:
...
# Containers running in each Pod
containers:
- name: k8szk
image: gcr.io/google_samples/k8szk:v3
ports:
- containerPort: 2181
name: client
- containerPort: 2888
name: server
- containerPort: 3888
name: leader-election
env:
...
readinessProbe:
exec:
command:
- "zkOk.sh"
initialDelaySeconds: 10
timeoutSeconds: 5
livenessProbe:
...
volumeMounts:
- name: datadir
mountPath: /var/lib/zookeeper

Metadata

Stateful Set Spec and Pod Template

Pod Selector

Replica

Pod Identify

Pod Management Policy

Update Strategy

Pod Affinity

volumeClaimTemplates

What Is Next

Reference

--

--

A software engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store