• 周三. 12 月 11th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

k8s中的pod更新策略

admin

11 月 28, 2021
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的更新策略    

.zstitle { 280px; text-align: center; font-size: 16px }
.zsimgweixin { 280px }
.zsimgali { 280px; padding: 0px 0px 50px 0px }
.zsleft { float: left }
.zsdiv { display: flex; justify-content: center;}
.zs { font-size: 26px }
.zspaddingright { padding: 0px 100px 0px 0px }

微信赞赏


支付宝赞赏

发表回复