Kubernetes指南
Linux性能优化实战eBPF 核心技术与实战SDN指南个人博客
EN
EN
  • Overview
  • Introduction
    • Kubernetes Introduction
    • Kubernetes Concepts
    • Kubernetes 101
    • Kubernetes 201
    • Kubernetes Cluster
  • Concepts
    • Concepts
    • Architecture
    • Design Principles
    • Components
      • etcd
      • kube-apiserver
      • kube-scheduler
      • kube-controller-manager
      • kubelet
      • kube-proxy
      • kube-dns
      • Federation
      • kubeadm
      • hyperkube
      • kubectl
    • Objects
      • Autoscaling
      • ConfigMap
      • CronJob
      • CustomResourceDefinition
      • DaemonSet
      • Deployment
      • Ingress
      • Job
      • LocalVolume
      • Namespace
      • NetworkPolicy
      • Node
      • PersistentVolume
      • Pod
      • PodPreset
      • ReplicaSet
      • Resource Quota
      • Secret
      • SecurityContext
      • Service
      • ServiceAccount
      • StatefulSet
      • Volume
  • Setup
    • Setup Guidance
    • kubectl Install
    • Single Machine
    • Feature Gates
    • Best Practice
    • Version Support
    • Setup Cluster
      • kubeadm
      • kops
      • Kubespray
      • Azure
      • Windows
      • LinuxKit
      • kubeasz
    • Setup Addons
      • Addon-manager
      • DNS
      • Dashboard
      • Monitoring
      • Logging
      • Metrics
      • GPU
      • Cluster Autoscaler
      • ip-masq-agent
  • Extension
    • API Extension
      • Aggregation
      • CustomResourceDefinition
    • Access Control
      • Authentication
      • RBAC Authz
      • Admission
    • Scheduler Extension
    • Network Plugin
      • CNI
      • Flannel
      • Calico
      • Weave
      • Cilium
      • OVN
      • Contiv
      • SR-IOV
      • Romana
      • OpenContrail
      • Kuryr
    • Container Runtime
      • CRI-tools
      • Frakti
    • Storage Driver
      • CSI
      • FlexVolume
      • glusterfs
    • Network Policy
    • Ingress Controller
      • Ingress + Letsencrypt
      • minikube Ingress
      • Traefik Ingress
      • Keepalived-VIP
    • Cloud Provider
    • Device Plugin
  • Cloud Native Apps
    • Apps Management
      • Patterns
      • Rolling Update
      • Helm
      • Operator
      • Service Mesh
      • Linkerd
      • Linkerd2
    • Istio
      • Deploy
      • Traffic Management
      • Security
      • Policy
      • Metrics
      • Troubleshooting
      • Community
    • Devops
      • Draft
      • Jenkins X
      • Spinnaker
      • Kompose
      • Skaffold
      • Argo
      • Flux GitOps
  • Practices
    • Overview
    • Resource Management
    • Cluster HA
    • Workload HA
    • Debugging
    • Portmap
    • Portforward
    • User Management
    • GPU
    • HugePage
    • Security
    • Audit
    • Backup
    • Cert Rotation
    • Large Cluster
    • Big Data
      • Spark
      • Tensorflow
    • Serverless
  • Troubleshooting
    • Overview
    • Cluster Troubleshooting
    • Pod Troubleshooting
    • Network Troubleshooting
    • PV Troubleshooting
      • AzureDisk
      • AzureFile
    • Windows Troubleshooting
    • Cloud Platform Troubleshooting
      • Azure
    • Troubleshooting Tools
  • Community
    • Development Guide
    • Unit Test and Integration Test
    • Community Contribution
  • Appendix
    • Ecosystem
    • Learning Resources
    • Domestic Mirrors
    • How to Contribute
    • Reference Documents
由 GitBook 提供支持
在本页
  • API version comparison table
  • Simple Example
  • Updating a StatefulSet
  • Partitions
  • Pod Management Policies
  • Parallel Example
  • Zookeeper
  • Caveats for StatefulSets
  1. Concepts
  2. Objects

StatefulSet

StatefulSet is specifically designed to address issues related to stateful services (while Deployments and ReplicaSets cater to stateless services), making it an essential tool when dealing with

  • Stable, persistent storage allowing Pods to access the same persistent data even after rescheduling, using Persistent Volumes Claims (PVC) to implement.

  • Stable networking IDs, meaning that the PodName and HostName remain unchanged even after a Pod is rescheduled, achieved through Headless Service (a Service without a Cluster IP).

  • Sequential Deployment and Scaling, indicating an orderly deployed or scaled Pod, guided by a predefined sequence (from 0 to N-1, where all preceding Pods have to be in Running and Ready status before the next Pod is run), implemented using init containers.

  • An ordered scale-down and deletion (from N-1 to 0).

From these application scenarios, we can deduce that a StatefulSet consists of:

  • A Headless Service defining a networking ID (DNS domain).

  • volumeClaimTemplates used for creating PersistentVolumes.

  • The StatefulSet explicitly defining the application.

Each Pod within a StatefulSet follows this DNS format: statefulSetName-{0..N-1}.serviceName.namespace.svc.cluster.local, where:

  • serviceName refers to the name of the Headless Service.

  • 0..N-1 is the sequence number of the Pod, starting from 0 and continuing to N-1.

  • statefulSetName is the name of the StatefulSet.

  • namespace indicates the namespace where the service resides. The Headless Service and StatefulSet need to be in the same namespace.

  • .cluster.local is the Cluster Domain.

API version comparison table

Kubernetes Version
Deployment Version

v1.5-v1.6

extensions/v1beta1

v1.7-v1.15

apps/v1beta1

v1.8-v1.15

apps/v1beta2

v1.9+

apps/v1

Simple Example

[Code snippet omitted for brevity]

You can perform other operations as well:

# Scaling up
$ kubectl scale statefulset web --replicas=5

# Scaling down
$ kubectl patch statefulset web -p '{"spec":{"replicas":3}}'

# Image updating (currently, direct image updates are unsupported, patchwork is used to achieve it indirectly)
$ kubectl patch statefulset web --type='json' -p='[{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"gcr.io/google_containers/nginx-slim:0.7"}]'

# Deleting a StatefulSet and Headless Service 
$ kubectl delete statefulset web
$ kubectl delete service nginx

# After the StatefulSet is deleted, the PVC will remain. If the data is no longer needed, it should be removed as well
$ kubectl delete pvc www-web-0 www-web-1

Updating a StatefulSet

From v1.7 and onwards, Kubernetes supports automatic updating of StatefulSets via the spec.updateStrategy setting. Currently, two strategies are supported:

  • OnDelete: When .spec.template is updated, old Pods aren't deleted immediately. Instead, users need to manually delete these old Pods, after which new Pods are automatically created. This is the default update strategy and is compatible with the behavior in versions v1.6 and earlier.

  • RollingUpdate: When .spec.template is updated, old Pods are automatically deleted, and new Pods are created simultaneously. In the update process, these Pods go through deletion, creation, and stabilization into Ready status one at a time in reverse order, before moving on to the next Pod update.

Partitions

RollingUpdate also supports Partitions, which can be set using .spec.updateStrategy.rollingUpdate.partition. Once partition is set, only Pods with a sequence number equal to or greater than the partition will be rolled out for .spec.template updates, while the remaining Pods are left unchanged (even when deleted, they will be recreated using the previous version).

[Example and code snippet omitted for brevity]

Pod Management Policies

From v1.7 and onwards, you can set the Pod management policy using .spec.podManagementPolicy, with two options available:

  • OrderedReady: This default policy sequentially creates each Pod and waits for it to be Ready before creating the next one.

  • Parallel: Pods are created or deleted simultaneously without waiting for other Pods to reach Ready status before launching all Pods.

Parallel Example

[Code snippet omitted for brevity]

You can observe that all Pods are created simultaneously.

[Code snippet omitted for brevity]

Zookeeper

[Code snippet omitted for brevity]

kubectl create -f zookeeper.yaml

Caveats for StatefulSets

  1. Recommended for use in Kubernetes v1.9 or later.

  2. All Pod Volumes must either use PersistentVolumes or be pre-created by an administrator.

  3. To ensure data safety, deleting a StatefulSet does not delete the Volumes.

  4. A StatefulSet requires a Headless Service to define the DNS domain. This should be created before the StatefulSet.

上一页ServiceAccount下一页Volume

最后更新于1年前

Let's consider a simple example using an nginx service :

Another example showing the StatefulSet's powerful functions is .

Detailed usage instructions can be found at the tutorial.

web.yaml
zookeeper.yaml
zookeeper stateful application