kubernetes: pod升级与回滚扩容与缩容暂停恢复
运行一个容器:
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1 # 副本数
revisionHistoryLimit: 10 # 历史记录保留的个数
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.4
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
# 保存为deployment.yaml 并创建nginx pod:
[root@k8s-master01 yaml]# kubectl create -f deployment-nginx.yaml
deployment.apps/nginx created
#检查:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6cdd5dd489-d7kll 1/1 Running 0 17s
nginx-6cdd5dd489-dblw2 1/1 Running 0 17s
nginx-6cdd5dd489-zc8fl 1/1 Running 0 17s
#检查nginx版本:
[root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-d7kll -- sh
# nginx -v
nginx version: nginx/1.15.4
#删除pod
kubectl delete -n default deployment nginx
#检查
[root@k8s-master01 pod]# kubectl get pod
No resources found in default namespace.
镜像升级与回滚
#升级:
root@k8s-master01[11:10:24]:~/yaml$ kubectl set image deploy nginx nginx=nginx:1.15.5 --record=true
deployment.apps/nginx image updated
#检查升级过程:
kubectl describe pod nginx-7c4b6d99fd-csh5s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 76s default-scheduler Successfully assigned default/nginx-df9789dc8-6h7p7 to k8s-node02
Normal Pulling 76s kubelet Pulling image "nginx:1.15.5"
Normal Pulled 51s kubelet Successfully pulled image "nginx:1.15.5" in 24.828362289s
Normal Created 51s kubelet Created container nginx
Normal Started 51s kubelet Started container nginx
#检查现在的版本:
[root@k8s-master01 yaml]# kubectl exec -it nginx-df9789dc8-trtwq -- sh
# nginx -v
nginx version: nginx/1.15.5
#检查更新过程
kubectl rollout status deploy nginx
[root@k8s-master01 yaml]# kubectl rollout status deploy nginx
Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
deployment "nginx" successfully rolled out
[root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-bsb7f -- sh
# nginx -v
nginx version: nginx/1.15.5
#为了实验效果 在升级一次
[root@k8s-master01 yaml]# kubectl set image deploy nginx nginx=nginx:1.15.6 --record=true
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-5455fbc855-64brv 1/1 Running 0 2m48s
nginx-5455fbc855-nblks 1/1 Running 0 3m16s
nginx-5455fbc855-ns4kz 1/1 Running 0 2m47s
[root@k8s-master01 yaml]# kubectl exec -it nginx-5455fbc855-64brv -- sh
# nginx -v
nginx version: nginx/1.15.6
#回滚:
1. 查看历史更新
kubectl rollout history deploy nginx
[root@k8s-master01 yaml]# kubectl rollout history deploy nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deploy nginx nginx=nginx:1.15.5 --record=true
3 kubectl set image deploy nginx nginx=nginx:1.15.6 --record=true
2. 回滚到上一个版本[kubectl rollout undo deploy {name} ]
[root@k8s-master01 yaml]# kubectl rollout undo deploy nginx
deployment.apps/nginx rolled back
3. 检查回滚状态:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6cdd5dd489-bzdgd 1/1 Running 0 21s
nginx-6cdd5dd489-cwxv5 1/1 Running 0 22s
nginx-6cdd5dd489-xb6xj 1/1 Running 0 20s
3.1 进入容器检查:
[root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-bzdgd -- sh
# nginx -v
nginx version: nginx/1.15.4
4. 回滚到指定版本
[root@k8s-master01 yaml]# kubectl rollout history deploy nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 kubectl set image deploy nginx nginx=nginx:1.15.5 --record=true
2 kubectl set image deploy nginx nginx=nginx:1.15.6 --record=true
# 查看指定版本信息
kubectl rollout history deploy nginx --revision=1
#回滚到指定版本
kubectl rollout undo deploy nginx --to-revision=1
[root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-q44cr -- sh
# nginx -v
nginx version: nginx/1.15.5
使用pod扩容,缩容
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-df9789dc8-gqk5j 1/1 Running 0 11m
nginx-df9789dc8-vhqkd 1/1 Running 0 11m
#修改资源:
[root@k8s-master01 yaml]# kubectl edit deploy nginx
deployment.apps/nginx edited
找到: replicas: 2
改为: replicas: 3
#检查状态:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-df9789dc8-c47mc 1/1 Running 0 3s
nginx-df9789dc8-gqk5j 1/1 Running 0 11m
nginx-df9789dc8-vhqkd 1/1 Running 0 11m
#缩容就是继续把 replicas: 3 改为: replicas: 2即可:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-df9789dc8-gqk5j 1/1 Running 0 13m
nginx-df9789dc8-vhqkd 1/1 Running 0 13m
#通过命令行进行扩容缩容
#缩容:
[root@k8s-master01 yaml]# kubectl scale --replicas=2 deploy nginx
deployment.apps/nginx scaled
#扩容:
[root@k8s-master01 yaml]# kubectl scale --replicas=10 deploy nginx
deployment.apps/nginx scaled
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-565979ccb4-5j896 1/1 Running 0 2s
nginx-565979ccb4-6hkjw 1/1 Running 0 2s
nginx-565979ccb4-6wkkx 1/1 Running 0 2s
nginx-565979ccb4-7nnlh 1/1 Running 0 8m54s
nginx-565979ccb4-cs2km 1/1 Running 0 2s
nginx-565979ccb4-dztkx 1/1 Running 0 2s
nginx-565979ccb4-jbmx7 1/1 Running 0 2s
nginx-565979ccb4-jqpzq 1/1 Running 0 2s
nginx-565979ccb4-vfnrd 1/1 Running 0 9m22s
nginx-565979ccb4-zsgwv 1/1 Running 0 2s
#注意,每个节点默认最大扩容数量为110个
# 如果需要修改则需要配置
使用yaml文件进行升级
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 3 # 副本数
revisionHistoryLimit: 10 # 历史记录保留的个数
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.4
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
#修改:
spec:
containers:
- image: nginx:1.15.5 #改为想要的镜像版本
#改为 1.15.5
[root@k8s-master01 yaml]# kubectl apply -f nginx-1.yaml
deployment.apps/nginx configured
[root@k8s-master01 yaml]# kubectl get pod
#注意升级的时候 他会起新的rs(replicas),通过命令可以看到,他会新起来一个pod 并把老副本数设置为2,新副本起来2个的时候
#老副本数会设置为1,直到变成0 最后才会真正的完成滚动更新。
[root@k8s-master01 yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5455fbc855 3 3 3 4m46s
nginx-565979ccb4 1 1 0 5s
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-5455fbc855-j552c 1/1 Running 0 17s
nginx-5455fbc855-sl8m8 1/1 Running 0 15s
nginx-5455fbc855-sq4mr 1/1 Running 0 14s
[root@k8s-master01 yaml]# kubectl exec -it nginx-5455fbc855-j552c -- sh
# nginx -v
nginx version: nginx/1.15.5
#查看滚动更新过程:
NewReplicaSet: nginx-565979ccb4 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set nginx-6cdd5dd489 to 3
Normal ScalingReplicaSet 9m53s deployment-controller Scaled up replica set nginx-df9789dc8 to 1
Normal ScalingReplicaSet 9m51s deployment-controller Scaled down replica set nginx-6cdd5dd489 to 2
Normal ScalingReplicaSet 9m51s deployment-controller Scaled up replica set nginx-df9789dc8 to 2
Normal ScalingReplicaSet 9m50s deployment-controller Scaled down replica set nginx-6cdd5dd489 to 1
Normal ScalingReplicaSet 9m50s deployment-controller Scaled up replica set nginx-df9789dc8 to 3
Normal ScalingReplicaSet 9m48s deployment-controller Scaled down replica set nginx-6cdd5dd489 to 0
Normal ScalingReplicaSet 7m47s deployment-controller Scaled up replica set nginx-5455fbc855 to 1
Normal ScalingReplicaSet 7m45s deployment-controller Scaled down replica set nginx-df9789dc8 to 2
Normal ScalingReplicaSet 2m18s (x10 over 7m45s) deployment-controller (combined from similar events): Scaled down replica set nginx-5455fbc855 to 0
Deployment 暂停[优化触发更新]
暂停功能使用在多次更新场景下的多次更新有多个记录的问题,如果每条更新都被记录,在查找有用更新的时候会比较麻烦,通过暂停更新功能,可以退回到指定版本,并且rs回滚只会产生一条记录。
[root@k8s-master01 ~]# # 开启 Deployment 暂停功能
[root@k8s-master01 ~]# kubectl rollout pause deployment nginx
deployment.apps/nginx paused
# 执行一次更新
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:1.15.3 --record
deployment.apps/nginx image updated
#此时按照常理来说 执行完了 kubectl set image deploy nginx nginx=nginx:1.15.3 --record 命令后应该会出现多次滚动更新来更新应用,但因为使用了 Deployment 暂停功能,在没有确认变更指令下达之前这些变更都不会生效
[root@k8s-master01 ~]# # 进行第二次配置变更
[root@k8s-master01 ~]# # 添加内存CPU配置
[root@k8s-master01 ~]# kubectl set resources deploy nginx -c nginx --limits=cpu=200m,memory=128Mi --requests=cpu=10m,memory=16Mi
deployment.apps/nginx resource requirements updated
#检查配置
[root@k8s-master01 ~]# kubectl get deploy nginx -oyaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "11"
kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.15.3
--record=true
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 18
labels:
app: nginx
name: nginx
namespace: default
resourceVersion: "2660534"
selfLink: /apis/apps/v1/namespaces/default/deployments/nginx
uid: 1d9253a5-a36c-48cc-aefe-56f95967db66
spec:
paused: true
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.3
imagePullPolicy: IfNotPresent
name: nginx
resources: #-------------新增但未生效的配置
limits:
cpu: 200m
memory: 128Mi
requests:
cpu: 10m
memory: 16Mi #-------------新增但未生效的配置
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 2
conditions:
- lastTransitionTime: "2020-09-19T03:26:50Z"
lastUpdateTime: "2020-09-19T03:26:50Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2020-09-19T03:30:15Z"
lastUpdateTime: "2020-09-19T03:30:15Z"
message: Deployment is paused
reason: DeploymentPaused
status: Unknown
type: Progressing
observedGeneration: 18
readyReplicas: 2
replicas: 2
[root@k8s-master01 ~]# # 查看pod是否被更新 [未更新]
[root@k8s-master01 ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-6cdd5dd489-lv28z 1/1 Running 0 30m
nginx-6cdd5dd489-nqqz7 1/1 Running 0 30m
#检查rs更新状态: [未更新]
[root@k8s-master01 pod]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5dfc8689c6 0 0 0 29m
nginx-64f878dfbf 0 0 0 32m
nginx-66bbc9fdc5 0 0 0 24m
nginx-7d5b7bcf78 2 2 2 30m
#关闭暂停更新功能
[root@k8s-master01 ~]# kubectl rollout resume deploy nginx
deployment.apps/nginx resumed
#检查状态
[root@k8s-master01 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5475c49ffb 0 0 0 21m
nginx-5dfc8689c6 0 0 0 33m
nginx-66bbc9fdc5 0 0 0 52m
nginx-68db656dd8 1 1 0 15s [状态更新]
nginx-6cdd5dd489 2 2 2 31m
nginx-799b8478d4 0 0 0 21m
nginx-7d79b96f68 0 0 0 24m
nginx-f974656f7 0 0 0 21m
#pod状态
[root@k8s-master01 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-68db656dd8-cc4jv 1/1 Running 0 22s [状态更新]
nginx-68db656dd8-hvj8x 1/1 Running 0 20s [状态更新]
kubernetes: pod升级与回滚扩容与缩容暂停恢复的更多相关文章
- k8s用kubectl管理应用升级,服务发布与回滚,扩缩容
应用升级 Kubectl set image --help 有案例指定新版本 [root@k8s-master ~]# kubectl set image deployment/nginx nginx ...
- Kubernetes:Pod 升级、回滚
本篇主要讨论如何实现滚动更新和回滚,任意更换版本并且回滚以前的版本(版本更新),而下一章会讨论到 Pod 缩放,根据机器资源自动拓展和收缩应用(自动扩容实例). 本文为作者的 Kubernetes 系 ...
- kubernetes deployment升级和回滚
a.创建deployment pod kubectl run mynginx --image=docker.io/nginx: --record 准备svc文件 apiVersion: v1 kind ...
- Docker Kubernetes 容器更新与回滚
Docker Kubernetes 容器更新与回滚 环境: 系统:Centos 7.4 x64 Docker版本:18.09.0 Kubernetes版本:v1.8 管理节点:192.168.1.79 ...
- 022.掌握Pod-Pod升级和回滚
一 deploymentPod升级和回滚 1.1 deployment升级 若Pod是通过Deployment创建的,可以在运行时修改Deployment的Pod定义(spec.template)或镜 ...
- Kubernetes 笔记 012 Pod 的自动扩容与缩容
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. Hi,大家好, ...
- Kubernetes 笔记 11 Pod 扩容与缩容 双十一前后的忙碌
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. Hi,大家好, ...
- Hadoop HDFS概念学习系列之HDFS升级和回滚机制(十二)
不多说,直接上干货! HDFS升级和回滚机制 作为一个大型的分布式系统,Hadoop内部实现了一套升级机制,当在一个集群上升级Hadoop时,像其他的软件升级一样,可能会有新的bug或一些会影响现有应 ...
- Docker Kubernetes 容器扩容与缩容
Docker Kubernetes 容器扩容与缩容 环境: 系统:Centos 7.4 x64 Docker版本:18.09.0 Kubernetes版本:v1.8 管理节点:192.168.1.79 ...
- k8s Pod 扩容和缩容
在生产环境下,在面临服务需要扩容的场景时,可以使用Deployment/RC的Scale机制来实现.Kubernetes支持对Pod的手动扩容和自动扩容. 手动扩容缩容 通过执行扩容命令,对某个dep ...
随机推荐
- 抓包整理————wireshark 抓包[二]
前言 简单整理一些wireshark抓包. 正文 打开wireshark 的capture的option 选项: 然后可以看到可以捕获的选项: 可以看到这里有我的以太网和虚拟机网卡流量. 这个就是将l ...
- js 闭包(新)
前言 旧的没有搬过来,先写一下新的感悟. 正文 ECMAScript中,闭包指的是: 从理论角度:所有的函数.因为它们都在创建的时候就将上层上下文的数据保存起来了.哪怕是简单的全局变量也是如此,因为函 ...
- ef 查询生成语句的几种方式
前言 整理一下ef 如何查看生成sql 语句的,现在有ef core 了,统一整理一下. 正文 方式如下: 数据库监听 这是一种推荐方式,因为调试和代码分开,不会有影响. 然后连接: 然后可以进行一些 ...
- 探索Kimi智能助手:如何用超长文本解锁高效信息处理新境界
目前,Kimi备受瞩目,不仅在社交平台上引起了广泛关注,而且在解决我们的实际问题方面也显示出了巨大潜力.其支持超长文本的特性使得我们能够更加灵活地配置信息,避免了频繁与向量数据库进行交互以及编写提示词 ...
- 力扣661(java)-图片平滑器(简单)
题目: 图像平滑器 是大小为 3 x 3 的过滤器,用于对图像的每个单元格平滑处理,平滑处理后单元格的值为该单元格的平均灰度. 每个单元格的 平均灰度 定义为:该单元格自身及其周围的 8 个单元格的 ...
- 《最新出炉》系列入门篇-Python+Playwright自动化测试-40-录制生成脚本
1.简介 各种自动化框架都会有脚本录制功能, playwright这么牛叉当然也不例外.很早之前的selenium.Jmeter工具,发展到每种浏览器都有对应的录制插件.今天我们就来看下微软自动化框架 ...
- Alibaba Cloud Linux 2 LTS 正式发布,提供更高性能和更多保障
Alibaba Cloud Linux 2 LTS版本发布后,阿里云将会为该版本提供长达5年的软件维护.问题修复服务.从2019-03-27开始到2024-03-31结束.包括: 免费的服务和支持:A ...
- 从 2018 年 Nacos 开源说起
2018 年夏天 国内微服务开源 领域,迎来了一位新成员.此后,在构建微服务注册中心和配置中心的过程中,国内开发者多了一个可信赖的选项. Nacos 是阿里巴巴开源的一个更易于构建云原生应用的动态服务 ...
- 一个好的网站logo设计长这样
简介:一个好的网站logo,不仅让用户一眼知道网站品牌传递的信息,还能提高网站专业度和丰富度,增加SEO搜索排名.今天分享下如何设计一款实用的网站logo.阿里云智能logo设计,在线免费体验log ...
- [MySQL] 原生全文检索 fulltext 的简单应用
在目标字段上添加全文检索:alter table 表名 add fulltext(字段) with parser ngram 查询语句:select * from xxx where match(字段 ...