7、StatefulSets
Statefulset is an API object used to manage the workload of stateful applications.
Stateful set is used to manage deployment and extend a group of pods, and can provide sequence number and uniqueness guarantee for these pods.
Like deployment, stateful set manages a set of pods defined based on the same container. But unlike deployment, stateful sets maintain a fixed ID for each of their pods. These pods are created based on the same declaration, but cannot be replaced with each other: no matter how scheduled, each pod has a permanent ID.
Statefulset and other controllers use the same operating mode. You define the desired state in the statefulset object, and then the controller of the statefulset will achieve the state you want through various updates.
Using statefulsets
Statefulsets are valuable for applications that need to meet one or more of the following requirements:
Stable and unique network identifier. Stable and persistent storage. Orderly and elegant deployment and scaling. Orderly and automatic rolling update. Above, stability means that the whole process of pod scheduling or rescheduling is persistent. If the application does not need any stable identifiers or orderly deployment, deletion, or scaling, you should deploy the application using the workload provided by a set of stateless replica controllers, such as deployment or replicaset, which may be more suitable for your stateless application deployment needs.
limit
The storage of a given pod must be provided by the persistentvolume driver based on the requested storage class, or provided in advance by the administrator. Deleting or shrinking a statefulset does not delete its associated storage volume. This is done to ensure data security. It is usually more valuable than automatically clearing all related resources of statefulset. Statefulset currently requires headless service to be responsible for the network identification of pod. You need to be responsible for creating this service. When deleting statefulsets, statefulsets do not provide any guarantee of terminating pod. In order to realize the orderly and elegant termination of pod in statefulset, the statefulset can be scaled to 0 before deletion. Using rolling updates when using the default pod management policy (orderedready) may enter a damaged state that requires manual intervention to repair.
Example:
[[email protected] ~/yaml/test]# vim statefulsets.yaml
[[email protected] ~/yaml/test]# cat statefulsets.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc-0
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc-1
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc-2
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 3 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: nginx-pvc-0
volumes:
- name: www
persistentVolumeClaim:
claimName: nginx-pvc-1
volumes:
- name: www
persistentVolumeClaim:
claimName: nginx-pvc-2
[[email protected] ~/yaml/test]#
Create statefulsets
[[email protected] ~/yaml/test]# kubectl apply -f statefulsets.yaml
service/nginx created
statefulset.apps/web created
[[email protected] ~/yaml/test]#
View pod
[[email protected] ~/yaml/test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
ingress-demo-app-694bf5d965-8rh7f 1/1 Running 0 67m
ingress-demo-app-694bf5d965-swkpb 1/1 Running 0 67m
nfs-client-provisioner-dc5789f74-5bznq 1/1 Running 0 52m
web-0 1/1 Running 0 93s
web-1 1/1 Running 0 85s
web-2 1/1 Running 0 66s
[[email protected] ~/yaml/test]#
View statefulsets
[[email protected] ~/yaml/test]# kubectl get statefulsets.apps -o wide
NAME READY AGE CONTAINERS IMAGES
web 3/3 113s nginx nginx
[[email protected] ~/yaml/test]#
Note: the premise is to solve the dynamic allocation PV of kubernetes. Refer to the document:https://cloud.tencent.com/dev…
https://blog.csdn.net/qq_3392…
https://my.oschina.net/u/3981543
https://www.zhihu.com/people/…
https://segmentfault.com/u/hp…
https://juejin.cn/user/331578…
https://space.bilibili.com/35…
https://cloud.tencent.com/dev…
Zhihu, CSDN, open source China, Sifu, Nuggets, BiliBili, Tencent cloud
This article usesArticle synchronization assistantsynchronization