[root@k8s-master statefulset]# kubectl create -f web.yaml
service/nginx created
statefulset.apps/web created
[root@k8s-master statefulset]# kubectl get sts
NAME READY AGE
web 0/2 24s
[root@k8s-master statefulset]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d20h
nginx ClusterIP None <none> 80/TCP 29s
[root@k8s-master statefulset]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Pending 3m57s
[root@k8s-master statefulset]# kubectl describe pvc www-web-0
Name: www-web-0
Namespace: default
StorageClass:
Status: Pending
Volume:
Labels: app=nginx
Annotations: volume.alpha.kubernetes.io/storage-class: anything
Finalizers: [kubernetes.io/pvc-protection]
Capacity:
Access Modes:
VolumeMode: Filesystem
Used By: web-0
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal FailedBinding 0s (x21 over 4m49s) persistentvolume-controller no persistent volumes available for this claim and no storage class is set
发现没有启动成功,先删除pvc相关配置,重新创建sts
[root@k8s-master statefulset]# kubectl get sts
NAME READY AGE
web 2/2 45s
[root@k8s-master statefulset]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d20h
nginx ClusterIP None <none> 80/TCP 51s
[root@k8s-master statefulset]# kubectl get po
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 2m43s
web-1 1/1 Running 0 2m26s
(2)扩容缩容
# 扩容
$ kubectl scale statefulset web --replicas=5
# 缩容
$ kubectl patch statefulset web -p '{"spec":{"replicas":3}}'
# 扩容
[root@k8s-master statefulset]# kubectl scale sts web --replicas=5
statefulset.apps/web scaled
[root@k8s-master statefulset]# kubectl get sts
NAME READY AGE
web 2/5 86m
[root@k8s-master statefulset]# kubectl get sts
NAME READY AGE
web 2/5 86m
[root@k8s-master statefulset]# kubectl get sts
NAME READY AGE
web 3/5 86m
[root@k8s-master statefulset]# kubectl get sts
NAME READY AGE
web 4/5 87m
[root@k8s-master statefulset]# kubectl describe sts web
Name: web
Namespace: default
CreationTimestamp: Sun, 05 Jan 2025 20:47:07 +0800
Selector: app=nginx
Labels: <none>
Annotations: <none>
Replicas: 5 desired | 5 total
Update Strategy: RollingUpdate
Partition: 0
Pods Status: 5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Volume Claims: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 75s statefulset-controller create Pod web-2 in StatefulSet web successful
Normal SuccessfulCreate 57s statefulset-controller create Pod web-3 in StatefulSet web successful
Normal SuccessfulCreate 40s statefulset-controller create Pod web-4 in StatefulSet web successful
# 缩容之后,再进行查看
[root@k8s-master statefulset]# kubectl scale sts web --replicas=2
statefulset.apps/web scaled
[root@k8s-master statefulset]# kubectl describe sts web
Name: web
Namespace: default
CreationTimestamp: Sun, 05 Jan 2025 20:47:07 +0800
Selector: app=nginx
Labels: <none>
Annotations: <none>
Replicas: 2 desired | 3 total
Update Strategy: RollingUpdate
Partition: 0
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Volume Claims: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 2m12s statefulset-controller create Pod web-2 in StatefulSet web successful
Normal SuccessfulCreate 114s statefulset-controller create Pod web-3 in StatefulSet web successful
Normal SuccessfulCreate 97s statefulset-controller create Pod web-4 in StatefulSet web successful
Normal SuccessfulDelete 3s statefulset-controller delete Pod web-4 in StatefulSet web successful
Normal SuccessfulDelete 1s statefulset-controller delete Pod web-3 in StatefulSet web successful
Normal SuccessfulDelete 0s statefulset-controller delete Pod web-2 in StatefulSet web successful
(3)镜像更新
# 通过patch的方式进行更新
[root@k8s-master statefulset]# kubectl patch sts web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "nginx:1.9.2"}]'
statefulset.apps/web patched
[root@k8s-master statefulset]# kubectl get sts
NAME READY AGE
web 1/2 92m
[root@k8s-master statefulset]# kubectl get sts
NAME READY AGE
web 1/2 93m
[root@k8s-master statefulset]# kubectl rollout history sts web
statefulset.apps/web
REVISION CHANGE-CAUSE
1 <none>
2 <none>
[root@k8s-master statefulset]# kubectl rollout history sts web --revision=2
statefulset.apps/web with revision #2
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.9.2
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
(4)灰度发布
利用滚动更新中的 partition 属性,可以实现简易的灰度发布的效果
例如我们有 5 个 pod,如果当前 partition 设置为 3,那么此时滚动更新时,只会更新那些 序号 >= 3 的 pod
利用该机制,我们可以通过控制 partition 的值,来决定只更新其中一部分 pod,确认没有问题后再主键增大更新的 pod 数量,最终实现全部 pod 更新
(5)删除
# 删除 StatefulSet 和 Headless Service
# 级联删除:删除 statefulset 时会同时删除 pods
kubectl delete statefulset web
# 非级联删除:删除 statefulset 时不会删除 pods,删除 sts 后,pods 就没人管了,此时再删除 pod 不会重建的
kubectl deelte sts web --cascade=false
# 删除 service
kubectl delete service nginx