Blog:博客园 个人

除了创建,Deployment 提供的另一个重要的功能就是更新应用,这是一个比创建复杂很多的过程。想象一下在日常交付中,在线升级是一个很常见的需求,同时应该尽量保证不能因为升级中断服务。这就要求我们必须使用一定的策略来决定何时创建新的 Pod,何时删除旧版本的 Pod。kubectl 支持滚动升级的方式,每次更新一个pod,而不是同时删除整个服务。

前置知识

回顾知识:

kubectl set image

命令格式:

kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N

例如:

Examples:
# Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'.
kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1 # Update all deployments' and rc's nginx container's image to 'nginx:1.9.1'
kubectl set image deployments,rc nginx=nginx:1.9.1 --all # Update image of all containers of daemonset abc to 'nginx:1.9.1'
kubectl set image daemonset abc *=nginx:1.9.1 # Print result (in yaml format) of updating nginx container image from local file, without hitting the server
kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml

Tips:支持缩写,如pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), replicaset (rs)。

选项

--all=false: Select all resources, including uninitialized ones, in the namespace of the specified resource types
--allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
--dry-run='none': Must be "none", "server", or "client". If client strategy, only print the object that would be
sent, without sending it. If server strategy, submit server-side request without persisting the resource.
--field-manager='kubectl-set': Name of the manager used to track field ownership.
-f, --filename=[]: Filename, directory, or URL to files identifying the resource to get from a server.
-k, --kustomize='': Process the kustomization directory. This flag can't be used together with -f or -R.
--local=false: If true, set image will NOT contact api-server but run locally.
-o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
--record=false: Record current kubectl command in the resource annotation. If set to false, do not record the
command. If set to true, record the command. If not set, default to updating the existing annotation value only if one
already exists.
-R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage
related manifests organized within the same directory.
-l, --selector='': Selector (label query) to filter on, not including uninitialized ones, supports '=', '==', and
'!='.(e.g. -l key1=value1,key2=value2)
--show-managed-fields=false: If true, keep the managedFields when printing objects in JSON or YAML format.
--template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

示例

创建deployment

先创建一个deployment,ngx-deploy.yaml配置文件如下:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.0
ports:
- containerPort: 80

执行命令:

kubectl create -f ngx-deploy.yaml

使用 watch 方式来观测 deployment 的状态变化:

[root@master test]# kubectl get deploy nginx-deployment -w
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 0/2 2 0 14s
nginx-deployment 1/2 2 1 25s
nginx-deployment 2/2 2 2 38s

说明:

  • NAME:deployment 的名字。
  • READY:就是当前有多少个 Pod 处于运行中/期望有多少个 Pod。
  • UP-TO-DATE:达到最新状态的 Pod 的数量。当 Deployment 在进行更新时,会有新老版本的 Pod 同时存在,这时候这个字段会比较有用。
  • AVAILABLE:可用的 Pod。上个实验我们讲了健康检查的相关配置,Pod 运行中和可以提供服务是不同的概念。
  • AGE::deployment 运行的时间。

更新镜像

接下来先更新一个nginx镜像,版本为1.18:

kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.18.0 --record

说明:

  • --record :将这条命令记录到了 deployment 的 yaml 的 annotations 里,可用于回滚镜像。

使用watch方式观察deployment实例变更情况:

[root@master test]# kubectl get deploy nginx-deployment -w
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 1 2 3m41s
nginx-deployment 3/2 1 3 3m58s
nginx-deployment 2/2 1 2 3m58s
nginx-deployment 2/2 2 2 3m58s

可以通过 get yaml 看到:

[root@master test]# kubectl get deploy nginx-deployment -o yaml
...
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.18.0
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
...

回滚镜像

有更新就有回滚。比如新的镜像版本有问题,或者配置不对等等,这是部署到生产环境里经常发生的事情。相对于更新,回滚镜像一般都是出现了问题,需要更快地进行处理。Deployment 的回滚机制正是为此而生。

可以通过命令查看历史版本:

[root@master test]# kubectl rollout history deployment.v1.apps/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.18.0 --record=true

回滚到上一个版本:

kubectl rollout undo deployment.v1.apps/nginx-deployment

可以观察到已回到上个版本:

[root@master test]# kubectl get deploy nginx-deployment -o yaml
...
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.16.0
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
...

也可回滚到指定版本:

kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=2

Kubernetes:更新与回滚的更多相关文章

  1. Docker Kubernetes 容器更新与回滚

    Docker Kubernetes 容器更新与回滚 环境: 系统:Centos 7.4 x64 Docker版本:18.09.0 Kubernetes版本:v1.8 管理节点:192.168.1.79 ...

  2. Docker Swarm(八)滚动更新、回滚服务

    滚动更新.回滚服务 默认情况下, swarm一次只更新一个副本,并且两个副本之间没有等待时间,我们可以通过: # 定义并行更新的副本数量--update-parallelism# 定义滚动更新的时间间 ...

  3. deployment控制pod进行滚动更新以及回滚

    更新pod镜像两种方式: 方式一:kubectl set image deployment/${deployment name} ${container name}=${image} 例: kubec ...

  4. 入门Kubernetes - 滚动升级/回滚

    一.前言 上一篇文章中对yaml文件格式进行了解,并对k8s中各种主要资源通过yaml创建时的定义模板.接来下就进一步学习k8s的各种特点.并应用在示例中. 接下来先实现.Net Core Api程序 ...

  5. linux运维、架构之路-K8s滚动更新及回滚

    一.滚动更新        应用程序一次只更新一小部分副本,更新成功后,再更新更多的副本,最终完成所有副本的更新. 滚动更新的优点:零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性. 1. ...

  6. 浅入Kubernetes(12):Deployment 的升级、回滚

    目录 更新 上线 会滚 缩放 Deployment 直接设置 Pod 水平自动缩放 比例缩放 暂停 Deployment 上线 本篇内容讨论 Pod 的更新和回滚,内容不多. 更新 打开 https: ...

  7. Kubernetes:Pod 升级、回滚

    本篇主要讨论如何实现滚动更新和回滚,任意更换版本并且回滚以前的版本(版本更新),而下一章会讨论到 Pod 缩放,根据机器资源自动拓展和收缩应用(自动扩容实例). 本文为作者的 Kubernetes 系 ...

  8. docker swarm实现java项目的发布/滚动更新/回滚/镜像管理

    使用docker swarm滚动更新java项目,部署集群,这一切的前提是使用Jenkins+maven进行项目打包,分发等功能 具体可以参考我的另外三篇文章 https://www.cnblogs. ...

  9. Docker & Kubenetes 系列四:集群,扩容,升级,回滚

    本篇将会讲解应用部署到Kubenetes集群,集群副本集查看,集群自愈能力演示,集群扩容,滚动升级,以及回滚. 本篇是Docker&Kubenetes系列的第四篇,在前面的篇幅中,我们向Kub ...

随机推荐

  1. leetcode 28. 实现 strStr()

    问题描述 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不 ...

  2. 【reverse】逆向7 堆栈图

    [reverse]逆向7 堆栈图 前言 本章就是开始画堆栈图来打基础拉,堆栈熟悉了之后就可以开始C语言的逆向了. 这一章使用的exe文件,我已经上传到了我的个人网盘中,点击下载 1.准备工作 先看这张 ...

  3. manjora20不小心卸载,重新安装terminal,软件商店/软件中心linux类似

    问题 重新安装老版本gnome-shell 如果突然死机可能卸载完了terminal和软件商店,但是没有安装新的. 此时没有terminal也没有软件商店 无法安装软件 解决方案 terminal c ...

  4. Cesium入门9 - Loading and Styling Entities - 加载和样式化实体

    Cesium入门9 - Loading and Styling Entities - 加载和样式化实体 Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://c ...

  5. Sentry 开发者贡献指南 - 浏览器 SDK 集成测试

    Sentry 的浏览器 SDK 的集成测试在内部使用 Playwright.这些测试在 Chromium.Firefox 和 Webkit 的最新稳定版本上运行. https://playwright ...

  6. LaTex用法笔记(一)——LaTex源文件的基本结构

    首先打开TeXstudio,创建一个新文件并保存 用\documentclass{article}引入一个文档类,也可以引用book/report/letter 然后用\begin{}和\end{}输 ...

  7. java继承子父类构造函数-子类的实例化过程

    1 /* 2 * 子父类中的构造函数的特点. 3 * 在子类构造对象时,发现,访问子类构造函数时,父类也运行了. 4 * 为什么呢? 5 * 原因是:在子类的构造函数中第一行有一个默认的隐式语句.su ...

  8. SDCC 的 MCS-51 汇编基础概念和传参方式

    寄存器 Register 寄存器用于数据的临时存储, 其数据可以表示为 用于处理的数据字节 指向数据的地址 寄存器的结构 8051的寄存器几乎都是8位寄存器, 因为8位MCU处理的主要是8位数据, 如 ...

  9. 不难懂------git开发过程中流程

    001.创建仓库 002.新建项目 003.初始化仓库  这一步不需要做 git init : 文件夹中会多出一个隐藏的.git文件 004.克隆项目 git clone <项目地址> 0 ...

  10. python内置re模块全面实战

    目录 一:取消转义 二:python内置模块之re模块 三:常用方法 findall search match 简便 四:常用方法 finditer 匹配文件多情况 五:切割 替换 内置模块 六:分组 ...