百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 博客教程 > 正文

kubernetes Deployment之滚动更新策略

connygpt 2024-08-22 12:49 11 浏览

1.Deployment控制器详细信息中包含了其更新策略的相关配置。kubectl describe命令中输出的StrategyType、RollingUpdateStrategy字段等;

root@kubernetes-master01:~# kubectl describe deploy sleep 
Name:                   sleep
Namespace:              default
CreationTimestamp:      Wed, 25 May 2022 15:03:14 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=sleep
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
# Deployment默认更新策略就是RollingUpdate默认更新策略是25%

1.Deployment的更新策略:

Deployment控制器支持两种更新策略: 滚动更新(rolling update)和重建创建(recreate)也称为单批次更新。
1.重建(Recreate),当更新策略设定为Recreate,在更新镜像时,它会先删除现在正在运行的Pod,等彻底杀死后,重新创建新的RS(ReplicaSet)然后启动对应的Pod,在整个更新过程中,会造成服务一段时间无法提供服务。也称之为单批次更新。
2.滚动更新(Rolling Update)滚动更新是默认的更新策略,一次仅更新一批Pod,当更新的Pod就绪后再更新另一批,直到全部更新完成为止;该策略实现了不间断服务的目标,但是在更新过程中,不同客户端得到的响应内容可能会来自不同版本的应用。会出现新老版本共存状态。

2.ReCreate实践;

Recreate分为三个步骤:
1.杀死所有旧版本的Pod,此时Pod无法正常对外提供服务;
2.创建新的RS,启动新的Pod;
3.等待Pod就绪,对外提供服务;

2.1应用配置示例

# 须在Spec字段中明确定义strategy滚动更新策略和type类型
root@kubernetes-master01:~# cat nginx-deployment-test.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-nginx-test
  namespace: default
spec:
  strategy:         # 滚动更新策略
    type: Recreate  # Recreate表示的是单批次更新。
  replicas: 2
  selector:
    matchLabels:
      app: nginx-deployment

  template:
    metadata:
      labels:
        app: nginx-deployment
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        #imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
root@kubernetes-master01:~# kubectl apply -f nginx-deployment-test.yaml 

2.2访问测试,访问前端的Service。查看Nginx的版本,测试服务是否会中断;

# 跟上任何符号会报错Nginx的错误页,现版本是1.16。当然是为了证实nginx版本。
root@kubernetes-master01:~# curl 10.107.246.117/v
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

2.3现版本是1.16,现在测试滚动更新;

1.修改Yaml配置文件修改编辑配置文件的spec.containers.image字段修改Nginx的版本号
2.通过set image来修改
3.通过kubectl edit deployment  deployment名称

我是通过set image来修改。
root@kubernetes-master01:~# kubectl set image deployment/deployment-nginx-test nginx=nginx:latest
deployment.apps/deployment-nginx-test image updated

2.4访问测试,的确中间是有业务访问间断的,因此在生产环境不建议这种方式。在更新的过程中旧Pod是处于Terminating状态。通常只有当应用的新旧版本不兼容(例如依赖的后端数据的格式不同且无法兼容)时才会使用Recreate重建策略。

root@kubernetes-master01:~# while sleep 0.5; do curl http://10.107.246.117/version;done
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
折叠 

3.RollingUpdate实践

滚动更新时,应用升级期间还要确保可用的Pod对象数量不低于阈值以确保可用持续处理客户端的服务请求,变动的方式和Pod对象的数量范围将通过spec.strategy.rollingUpdate.maxSurge和spec.strategy.rollingUpdate.maxUnavailable两个属性同时进行定义;

滚动更新(RollingUpdate)一次仅更新一批Pod,当更新的Pod就绪后,再更新另一批,直到全部更新完成为止,该策略实现了不间断服务的目标,在更新过程中可能会出现不同的应用版本且并存,同时提供服务的情况。

1.创建新的RS,然后根据新的镜像运行新的Pod。
2.删除旧的Pod,启动新的Pod,当新Pod就绪后,继续删除旧Pod,启动新Pod。
3.持续第二步过程,一直到所有Pod都被更新成功。

3.1准备应用配置文件

#  type字段须配置为RollingUpdate
spec:
  strategy:
    type: RollingUpdate
# 应用配置文件,现版本Nginx是1.16版本,要滚动到1.21.5;
root@kubernetes-master01:~# cat nginx-deployment-test.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-nginx-test
  namespace: default
spec:
  strategy:
    type: RollingUpdate
  replicas: 2
  selector:
    matchLabels:
      app: nginx-deployment

  template:
    metadata:
      labels:
        app: nginx-deployment
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        #imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
root@kubernetes-master01:~# kubectl apply -f nginx-deployment-test.yaml 
deployment.apps/deployment-nginx-test created

# Pod也是运行正常的。
root@kubernetes-master01:~# kubectl get pods -l app=nginx-deployment
NAME                                     READY   STATUS    RESTARTS   AGE
deployment-nginx-test-65b579f8d5-gdpd5   1/1     Running   0          3m14s
deployment-nginx-test-65b579f8d5-spq9w   1/1     Running   0          3m14s
折叠 

3.2,我们现在访问前端的Service;OK版本一直是1.16;

root@kubernetes-master01:~# while sleep 0.5; do curl http://10.103.162.78/v;done
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>

3.3修改Yaml配置文件进行滚动升级;并观察是否业务中断;

# Pod状态;1.16版本的Pod是65b579f8d5,现在是76f7b87b7c
root@kubernetes-master01:~# kubectl get pods
deployment-nginx-test-76f7b87b7c-n2jmm       1/1     Running             0          35s
deployment-nginx-test-76f7b87b7c-qvxhg       0/1     ContainerCreating   0          11s

# 滚动升级状态;
# 我修改的是配置文件,并非set image;
root@kubernetes-master01:~# kubectl apply -f nginx-deployment-test.yaml  && kubectl rollout status deploy deployment-nginx-test 
deployment.apps/deployment-nginx-test configured
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 old replicas are pending termination...
deployment "deployment-nginx-test" successfully rolled out

# 查看Replicaset;现在65b579的Pod数量被置于0
root@kubernetes-master01:~# kubectl get replicaset
deployment-nginx-test-577977f4b6       2         2         2       7m
deployment-nginx-test-65b579f8d5       0         0         0       17m

3.4观察访问

# 业务访问正常,没有中断,但是一段时间会出现新老版本共存状态;
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
^C

4.应用的回滚

4.1.我们可以通过命令来查看更新的历史版本;

root@kubernetes-master01:~# kubectl  rollout history  deployment deployment-nginx-test
deployment.apps/deployment-nginx-test 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

4.2.也可以查看具体镜像详情,跟上序号;

root@kubernetes-master01:~# kubectl rollout history deploy  deployment-nginx-test --revision=2
deployment.apps/deployment-nginx-test with revision #2
Pod Template:
  Labels:	app=nginx-deployment
	pod-template-hash=76f7b87b7c
  Containers:
   nginx:
    Image:	nginx:1.16
    Port:	80/TCP
    Host Port:	0/TCP
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

4.3.现在想要回滚到1.16这个版本;

# kubectl rollout undo 命令来执行回滚;
root@kubernetes-master01:~# kubectl rollout undo deploy  deployment-nginx-test  --to-revision=2 && kubectl rollout status deploy deployment-nginx-test
deployment.apps/deployment-nginx-test rolled back
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 old replicas are pending termination...
deployment "deployment-nginx-test" successfully rolled out

4.4.观察访问状态;亦会出现新老版本交替情况

<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
^C
折叠 

4.5.查看Pod和Replicaset

root@kubernetes-master01:~# kubectl get pods -l app=nginx-deployment
NAME                                     READY   STATUS    RESTARTS   AGE
deployment-nginx-test-76f7b87b7c-5tx7z   1/1     Running   0          2m34s
deployment-nginx-test-76f7b87b7c-t9ld2   1/1     Running   0          2m57s

# 也是没有问题。
root@kubernetes-master01:~# kubectl get replicaset -l app=nginx-deployment
NAME                               DESIRED   CURRENT   READY   AGE
deployment-nginx-test-577977f4b6   0         0         0       20m
deployment-nginx-test-65b579f8d5   0         0         0       31m
deployment-nginx-test-76f7b87b7c   2         2         2       24m

文章来自https://www.cnblogs.com/xunweidezui/p/16528064.html

相关推荐

3分钟让你的项目支持AI问答模块,完全开源!

hello,大家好,我是徐小夕。之前和大家分享了很多可视化,零代码和前端工程化的最佳实践,今天继续分享一下最近开源的Next-Admin的最新更新。最近对这个项目做了一些优化,并集成了大家比较关注...

干货|程序员的副业挂,12个平台分享

1、D2adminD2Admin是一个完全开源免费的企业中后台产品前端集成方案,使用最新的前端技术栈,小于60kb的本地首屏js加载,已经做好大部分项目前期准备工作,并且带有大量示例代码,助...

Github标星超200K,这10个可视化面板你知道几个

在Github上有很多开源免费的后台控制面板可以选择,但是哪些才是最好、最受欢迎的可视化控制面板呢?今天就和大家推荐Github上10个好看又流行的可视化面板:1.AdminLTEAdminLTE是...

开箱即用的炫酷中后台前端开源框架第二篇

#头条创作挑战赛#1、SoybeanAdmin(1)介绍:SoybeanAdmin是一个基于Vue3、Vite3、TypeScript、NaiveUI、Pinia和UnoCSS的清新优...

搭建React+AntDeign的开发环境和框架

搭建React+AntDeign的开发环境和框架随着前端技术的不断发展,React和AntDesign已经成为越来越多Web应用程序的首选开发框架。React是一个用于构建用户界面的JavaScrip...

基于.NET 5实现的开源通用权限管理平台

??大家好,我是为广大程序员兄弟操碎了心的小编,每天推荐一个小工具/源码,装满你的收藏夹,每天分享一个小技巧,让你轻松节省开发效率,实现不加班不熬夜不掉头发,是我的目标!??今天小编推荐一款基于.NE...

StreamPark - 大数据流计算引擎

使用Docker完成StreamPark的部署??1.基于h2和docker-compose进行StreamPark部署wgethttps://raw.githubusercontent.com/a...

教你使用UmiJS框架开发React

1、什么是Umi.js?umi,中文可发音为乌米,是一个可插拔的企业级react应用框架。你可以将它简单地理解为一个专注性能的类next.js前端框架,并通过约定、自动生成和解析代码等方式来辅助...

简单在线流程图工具在用例设计中的运用

敏捷模式下,测试团队的用例逐渐简化以适应快速的发版节奏,大家很早就开始运用思维导图工具比如xmind来编写测试方法、测试点。如今不少已经不少利用开源的思维导图组件(如百度脑图...)来构建测试测试...

【开源分享】神奇的大数据实时平台框架,让Flink&amp;Spark开发更简单

这是一个神奇的框架,让Flink|Spark开发更简单,一站式大数据实时平台!他就是StreamX!什么是StreamX大数据技术如今发展的如火如荼,已经呈现百花齐放欣欣向荣的景象,实时处理流域...

聊聊规则引擎的调研及实现全过程

摘要本期主要以规则引擎业务实现为例,陈述在陌生业务前如何进行业务深入、调研、技术选型、设计及实现全过程分析,如果你对规则引擎不感冒、也可以从中了解一些抽象实现过程。诉求从硬件采集到的数据提供的形式多种...

【开源推荐】Diboot 2.0.5 发布,自动化开发助理

一、前言Diboot2.0.5版本已于近日发布,在此次发布中,我们新增了file-starter组件,完善了iam-starter组件,对core核心进行了相关优化,让devtools也支持对IAM...

微软推出Copilot Actions,使用人工智能自动执行重复性任务

IT之家11月19日消息,微软在今天举办的Ignite大会上宣布了一系列新功能,旨在进一步提升Microsoft365Copilot的智能化水平。其中最引人注目的是Copilot...

Electron 使用Selenium和WebDriver

本节我们来学习如何在Electron下使用Selenium和WebDriver。SeleniumSelenium是ThoughtWorks提供的一个强大的基于浏览器的开源自动化测试工具...

Quick &#39;n Easy Web Builder 11.1.0设计和构建功能齐全的网页的工具

一个实用而有效的应用程序,能够让您轻松构建、创建和设计个人的HTML网站。Quick'nEasyWebBuilder是一款全面且轻巧的软件,为用户提供了一种简单的方式来创建、编辑...