kubenetes中的pod删除策略 级联删除与非级联删除
StatefulSet 有状态应用[有状态应用]
有状态:StatefulSet
- 集群节点之间的关系。
- 数据不完全一致。
- 实例之间不对等的关系。
- 依靠外部存储的应用。
- 通过dns维持身份
- 每个pod都有特定的名称和网络标识(如pod名是由statefulSet名+有序的数字组成(0、1、2..))
redis是有状态应用
StatefulSet(有状态集,缩写为sts)常用于部署有状态的且需要有序启动的应用程序,比如在进行SpringCloud项目容器化时,Eureka的部署是比较适合用StatefulSet部署方式的,可以给每个Eureka实例创建一个唯一且固定的标识符,并且每个Eureka实例无需配置多余的Service,其余Spring Boot应用可以直接通过Eureka的Headless Service即可进行注册。
Eureka的statefulset的资源名称是
eureka,eureka-0
eureka-1
eureka-2
Service:headless service,没有ClusterIP [每个]
eureka-svc Eureka-0.eureka-svc.NAMESPACE_NAME eureka-1.eureka-svc …
连接 eureka的资源名称为:eureka
statefullset示例
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"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
[root@k8s-master01 statefullset]# kubectl create -f statefullset.yaml
service/nginx created
statefulset.apps/web created
#扩容:
replicas: 2 #[定义副本集数量]
[root@k8s-master01 statefullset]# kubectl get pod
web-0 1/1 Running 0 1m
web-1 1/1 Running 0 1m
#命令扩容
[root@k8s-master01 statefullset]# kubectl scale --replicas=3 sts web
statefulset.apps/web scaled
#检查扩容
[root@k8s-master01 statefullset]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 15m
web-1 1/1 Running 0 12m
web-2 1/1 Running 0 35s
#检查扩容流程:
[root@k8s-master01 ~]# kubectl scale --replicas=5 sts web
statefulset.apps/web scaled
[root@k8s-master01 ~]# kubectl get pod -l app=nginx -w
NAME READY STATUS RESTARTS AGE
nginx-68db656dd8-cc4jv 1/1 Running 0 36h
nginx-68db656dd8-hvj8x 1/1 Running 0 36h
web-0 1/1 Running 0 18h
web-1 1/1 Running 0 18h
web-2 0/1 Pending 0 0s
web-2 0/1 Pending 0 0s
web-2 0/1 ContainerCreating 0 0s
web-2 1/1 Running 0 18s
web-3 0/1 Pending 0 0s
web-3 0/1 Pending 0 0s
web-3 0/1 ContainerCreating 0 0s
web-3 1/1 Running 0 18s
web-4 0/1 Pending 0 0s
web-4 0/1 Pending 0 0s
web-4 0/1 ContainerCreating 0 0s
web-4 1/1 Running 0 17s
statefulset 更新策略
#默认更新策略:
updateStrategy:
rollingUpdate:
partition: 0 # 设置为1 一次更新一个,如果设置为 0 则是随机更新
type: RollingUpdate #--- statefulset默认更新策略 滚动更新,有一个更新失败就不会继续更新,deployment是随机更新模式
statefulset更新策略:
1. RollingUpdate 滚动更新 [从下往上更新,倒序更新]
2. OnDelete 手动更新 [需要删除一个pod才会触发更新策略]
默认更新策略RollingUpdate示例:
执行命令: kubectl edit sts web
找到:
containers:
- image: nginx
改为:
containers:
- image: nginx:1.15.2
# 查看更新过程:
[root@k8s-master01 ~]# kubectl get pod -l app=nginx -w
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 20h
web-1 1/1 Running 0 20h
web-2 1/1 Running 0 89m
web-2 1/1 Terminating 0 90m
web-2 0/1 Terminating 0 90m
web-2 0/1 Terminating 0 90m
web-2 0/1 Terminating 0 90m
web-2 0/1 Pending 0 0s
web-2 0/1 Pending 0 0s
web-2 0/1 ContainerCreating 0 0s
web-2 1/1 Running 0 19s
web-1 1/1 Terminating 0 20h
web-1 0/1 Terminating 0 20h
web-1 0/1 Terminating 0 20h
web-1 0/1 Terminating 0 20h
web-1 0/1 Pending 0 0s
web-1 0/1 Pending 0 0s
web-1 0/1 ContainerCreating 0 0s
web-1 1/1 Running 0 20s
web-0 1/1 Terminating 0 20h
web-0 0/1 Terminating 0 20h
web-0 0/1 Terminating 0 20h
web-0 0/1 Terminating 0 20h
web-0 0/1 Pending 0 0s
web-0 0/1 Pending 0 0s
web-0 0/1 ContainerCreating 0 0s
web-0 1/1 Running 0 19s
# 查看更新结果:
[root@k8s-master01 ~]# kubectl get pod -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 59s
web-1 1/1 Running 0 88s
web-2 1/1 Running 0 118s
# 验证结果:
[root@k8s-master01 ~]# kubectl exec -it web-0 -- sh
# nginx -v
nginx version: nginx/1.15.2
OnDelete策略示例:
# 修改为 OnDelete 策略
# kubectl edit sts web
找到:
updateStrategy:
rollingUpdate:
partition: 0
type: RollingUpdate
改为:
updateStrategy:
type: OnDelete
# 修改镜像版本来查看 是否此策略会更新版本
image: nginx
#改为:
image: nginx:1.15.3
#保存 [删除 rollingUpdate: 与 partition: 0 并且将 type: RollingUpdate 改为 type: OnDelete ]
#检查:
[root@k8s-master01 ~]# kubectl get pod |grep web
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 6m58s
web-1 1/1 Running 0 7m17s # <-- 并未更新
web-2 1/1 Running 0 7m48s
#再次修改镜像版本并删除其中一个pod:
[root@k8s-master01 ~]# kubectl edit sts web #把 nginx:1.15.3 改为 nginx:1.15.2
statefulset.apps/web edited
[root@k8s-master01 statefullset]# kubectl delete pod web-1
pod "web-1" deleted
# 检查删除的pod更新状态:
[root@k8s-master01 statefullset]# kubectl get pod web-1 -o yaml|grep image
- image: nginx:1.15.3
imagePullPolicy: Always
image: nginx:1.15.3
imageID: docker-pullable://nginx@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
# 检查未删除的pod版本:
[root@k8s-master01 statefullset]# kubectl get pod web-0 -o yaml|grep image
- image: nginx
imagePullPolicy: Always
image: nginx:latest
imageID: docker-pullable://nginx@sha256:353c20f74d9b6aee359f30e8e4f69c3d7eaea2f610681c4a95849a2fd7c497f9
# 再删除这个未删除的版本检查删除后是否会更新,刚删除的web-1 现在删除web-0:
[root@k8s-master01 statefullset]# kubectl delete pod web-0
pod "web-0" deleted
#检查删除后的web-0 是否更新了版本
[root@k8s-master01 statefullset]# kubectl get pod web-0 -o yaml|grep image
- image: nginx:1.15.3
imagePullPolicy: Always
image: nginx:1.15.3 # -- 确定已经更新了版本
imageID: docker-pullable://nginx@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
这就是Ondelete的更新策略
灰度发布常用策略 partition
#OnDelete与策略解释:
updateStrategy:
type: OnDelete # 删除pod才会更新
#partition策略解释 RollingUpdate :
updateStrategy:
rollingUpdate:
partition: 2 #大于2的才会被更新
type: RollingUpdate #自动更新
级联删除和非级联删除
#级联删除:
#删除 statefullset 时同时删除 pod
[root@k8s-master01 statefullset]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-68db656dd8-cc4jv 1/1 Running 0 10d
nginx-68db656dd8-hvj8x 1/1 Running 0 10d
web-0 1/1 Running 0 3m2s
web-1 1/1 Running 0 2m42s
[root@k8s-master01 statefullset]# kubectl get sts
NAME READY AGE
web 2/2 39s
# kubectl delete sts web
[root@k8s-master01 statefullset]# kubectl get sts
No resources found in default namespace.
#非级联删除 [极少使用] 删除 statefullset 不删除 pod
[root@k8s-master01 statefullset]# kubectl get sts
NAME READY AGE
web 2/2 3s
[root@k8s-master01 statefullset]# kubectl delete sts web --cascade=orphan
statefulset.apps "web" deleted
[root@k8s-master01 statefullset]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-68db656dd8-cc4jv 1/1 Running 0 10d
nginx-68db656dd8-hvj8x 1/1 Running 0 10d
web-0 1/1 Running 0 3m43s
web-1 1/1 Running 0 3m35s
kubectl delete sts web --cascade=orphan #非级联删除 ,但是pod不会被删除,会变成孤儿pod
kubectl delete pod web-0 web-1 #这时候删除的pod都不会被重建。
#注意 非级联删除 pod是不会被删除的,他会变成孤儿pod,此时使用 kubectl delete pod web-0 web-1 删除不会再创建
在日常中基本不会用到非级联删除,因为非级联删除还会遗留pod来手工操作删除
微信赞赏
支付宝赞赏