概念:
StatefulSet是用来管理有状态应用的工作负载API对象,kubectl 中可以简写sts ,sts每一个pod生成一个唯一的标识符,sts_name-number,number从0开始。
StatefulSet会关联卷(volume),删除是不会删除卷,之后的PV,PVC的随笔中会陆续介绍。
StatefulSet需要headless service,需要你去常见该服务,之后service随笔也会陆续介绍。
StatefulSet不保证pod的数量,顺序创建,倒叙删除。
StatefulSet的命名需要遵守DNS子域名规范:
- 不能超过253个字符
- 只能包含小写字母,数字,以及'-'和'.'
- 必须咦字母数开头
- 必须以字母数字结尾
模板:
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx" #必须匹配 .spec.template.metadata.labels
replicas: 3 # 默认是1
selector:
matchLabels:
app: nginx #必须匹配 .spec.template.metadata.labels
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14
#image: nginx
ports:
- containerPort: 80
name: web
创建:
kubectl create -f sts-test.yaml
查看:
[root@k8s-master01 ns-slx-study]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 17m
web-1 1/1 Running 0 17m
web-2 1/1 Running 0 17m
[root@k8s-master01 ns-slx-study]# kubectl get sts
NAME READY AGE
web 3/3 45m
查看所有yaml:
[root@k8s-master01 ns-slx-study]# kubectl get sts web -o yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"StatefulSet","metadata":{"annotations":{},"name":"web","namespace":"default"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"nginx"}},"serviceName":"nginx","template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx:1.14","name":"web","ports":[{"containerPort":80}]}]}}}}
creationTimestamp: "2023-02-23T23:28:57Z"
generation: 7
name: web
namespace: default
resourceVersion: "291331"
uid: 4aa0221f-c60d-4f71-a2d2-07f8184f9191
spec:
podManagementPolicy: OrderedReady
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
serviceName: nginx
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.14
imagePullPolicy: IfNotPresent
name: web
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
updateStrategy:
rollingUpdate:
partition: 0
type: RollingUpdate
status:
availableReplicas: 3
collisionCount: 0
currentReplicas: 3
currentRevision: web-54b86cbdbb
observedGeneration: 7
readyReplicas: 3
replicas: 3
updateRevision: web-54b86cbdbb
updatedReplicas: 3
不同于Deployment的strategy,sts的回滚策略字段为updateStrategy。
扩缩容:
扩容:
#将sts的副本增加到5
kubectl scale sts web --relicas=5
#可以使用命令查看扩容的过程
[root@k8s-master01 ns-slx-study]# kubectl get pod -w -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 28m
web-1 1/1 Running 0 28m
web-2 1/1 Running 0 28m
web-3 0/1 Pending 0 0s
web-3 0/1 Pending 0 0s
web-3 0/1 ContainerCreating 0 0s
web-3 0/1 ContainerCreating 0 1s
web-3 1/1 Running 0 51s
web-4 0/1 Pending 0 0s
web-4 0/1 Pending 0 0s
web-4 0/1 ContainerCreating 0 0s
web-4 0/1 ContainerCreating 0 1s
web-4 1/1 Running 0 62s
缩容:
#将sts web副本缩容到2个
[root@k8s-master01 ns-slx-study]# kubectl scale sts web --replicas=2
#查看缩容的过程
[root@k8s-master01 ns-slx-study]# kubectl get pod -w -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 32m
web-1 1/1 Running 0 32m
web-2 1/1 Running 0 32m
web-3 1/1 Running 0 4m26s
web-4 1/1 Running 0 3m35s
web-4 1/1 Terminating 0 3m38s
web-4 1/1 Terminating 0 3m38s
web-4 0/1 Terminating 0 3m39s
web-4 0/1 Terminating 0 3m39s
web-4 0/1 Terminating 0 3m39s
web-3 1/1 Terminating 0 4m30s
web-3 1/1 Terminating 0 4m30s
web-3 0/1 Terminating 0 4m31s
web-3 0/1 Terminating 0 4m31s
web-3 0/1 Terminating 0 4m31s
web-2 1/1 Terminating 0 32m
web-2 1/1 Terminating 0 32m
web-2 0/1 Terminating 0 32m
web-2 0/1 Terminating 0 32m
web-2 0/1 Terminating 0 32m
更新:
更新策略:
On DElete策略:(不常用)
OnDelete 更新策略实现了传统(1.7 版本之前)的行为,它也是默认的更新策略。当我们选择这个更新策略并修改 StatefulSet 的.spec.template 字段时,StatefulSet 控制器不会自动更新 Pod,必须手动删除 Pod 才能使控制器创建新的 Pod
Rolling Upddate 策略:
RollingUpdate(滚动更新)更新策略会自动更新一个 StatefulSet 中所有的 Pod,采用与序号索引相反的顺序进行滚动更新
命令:
#我这里主要使用的rollingUpdate方式更新:
#命令行的方式:
kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate"}}}'
#更改后的SetatefulSet:
kubectl get sts web -o yaml | grep -A 1 "updateStrategy"
#以上都是1.7版本之前的操作,在1.7版本之后是默认rollingUpdate方式更新
#建议使用edit或者yaml文件的方式更新
#更新后会倒叙的方式依次删除重建
[root@k8s-master01 ns-slx-study]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 81s
web-1 1/1 Running 0 84s
web-2 1/1 Running 0 32s
web-3 0/1 ContainerCreating 0 1s
web-4 1/1 Running 0 4s
分段更新:
#比如我们定义一个分区"partition":3,可以使用 patch 或 edit 直接对 StatefulSet 进行设置:
使用命令更改:kubectl edit sts web
updateStrategy:
rollingUpdate:
partition: 3
type: RollingUpdate
这里面的parition的字段为3 ,保存退出,再更改镜像
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: web
ports:
- containerPort: 80
protocol: TCP
再更改镜像,是从最后的开始往3号位置更新到最后,是从最后开始更新,到3号结束
更改完之后是这样的:
- image: nginx:1.14
imagePullPolicy: IfNotPresent
image: nginx:1.14
imageID: docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
- image: nginx:1.14
imagePullPolicy: IfNotPresent
image: nginx:1.14
imageID: docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
- image: nginx:1.14
imagePullPolicy: IfNotPresent
image: nginx:1.14
imageID: docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
- image: nginx
imagePullPolicy: IfNotPresent
image: nginx:latest
imageID: docker-pullable://nginx@sha256:6650513efd1d27c1f8a5351cbd33edf85cc7e0d9d0fcb4ffb23d8fa89b601ba8
- image: nginx
imagePullPolicy: IfNotPresent
image: nginx:latest
imageID: docker-pullable://nginx@sha256:6650513efd1d27c1f8a5351cbd33edf85cc7e0d9d0fcb4ffb23d8fa89b601ba8
#下面的两个镜像和上面的是不一样的
删除:
删除StatefulSet有两种方式:级联删除,非级联删除
使用非级联删除时,StatefulSet的pod不会被删除(很少使用)
使用级联删除时,StatefulSet和它的pod都会被删除
1,非级联删除
使用kubectl delete sts xxx 删除StatefulSet的时候,只需要后面加上--cascade=false参数,就会采用非级联删除,此时删除StatefulSet不会删除它的pod
[root@k8s-master01 ns-slx-study]# kubectl get pod #查看pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 11m
web-1 1/1 Running 0 11m
web-2 1/1 Running 0 11m
web-3 1/1 Running 0 9m41s
web-4 1/1 Running 0 9m44s
[root@k8s-master01 ns-slx-study]# kubectl delete sts web --cascade=false #使用非级联删除
warning: --cascade=false is deprecated (boolean value) and can be replaced with --cascade=orphan.
statefulset.apps "web" deleted
[root@k8s-master01 ns-slx-study]# kubectl get sts #查看以及没有sts,以及被删除
No resources found in default namespace.
[root@k8s-master01 ns-slx-study]# kubectl get pod #管理的pod并没有被删除
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 12m
web-1 1/1 Running 0 12m
web-2 1/1 Running 0 12m
web-3 1/1 Running 0 10m
web-4 1/1 Running 0 10m
#由于此时删除了StatefulSet,它管理的pod变成了孤儿因此单独删除pod时,不会被重建
[root@k8s-master01 ns-slx-study]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 12m
web-1 1/1 Running 0 12m
web-2 1/1 Running 0 12m
web-3 1/1 Running 0 10m
web-4 1/1 Running 0 10m
[root@k8s-master01 ns-slx-study]# kubectl delete po/web-1
pod "web-1" deleted
[root@k8s-master01 ns-slx-study]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 15m
web-2 1/1 Running 0 15m
web-3 1/1 Running 0 13m
web-4 1/1 Running 0 13m
2,级联删除
省略--cascade=false参数就是级联删除
[root@k8s-master01 ns-slx-study]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 11s
web-1 1/1 Running 0 10s
web-2 1/1 Running 0 8s
[root@k8s-master01 ns-slx-study]# kubectl delete sts web
statefulset.apps "web" deleted
[root@k8s-master01 ns-slx-study]# kubectl get pod
No resources found in default namespace.
[root@k8s-master01 ns-slx-study]#
标签:web,StatefulSet,kubectl,扩缩容,nginx,Running,pod,模板
From: https://www.cnblogs.com/slx-yyds/p/17149993.html