详谈kubernetes滚动更新-1
这个系列分为两个小节,第一个小节介绍deployment滚动更新时,deployment、replicaset、pod的细节以及创建过程以及deployment版本管理的方式
第二个小节将介绍滚动更新过程中最大可用、liveness以及readiness等
我们在阿里云上有两个不同版本的镜像用于测试,使用docker pull把它拉取到本地
docker pull registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1
docker pull registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2
截至目前,我们并没有详细介绍过docker的操作,但是目前市面上已经有很多书籍和博客介绍docker的基本操作,没有docker操作经验的可以关注一些入门教程.
root@k8s-master:~# docker run -d -p 10080:80 nginx:v1
e88097841c5feef92e4285a2448b943934ade5d86412946bc8d86e262f80a050
root@k8s-master:~# curl http://127.0.0.1:10080
----------
version: v1
hostname: f5189a5d3ad3
注在linux里如果是以root用户登陆,则命令行前一个
#标识,这里并不是注释的意思,还请注意
deployment、replicaset、pod之间的关系
+------------+
| deployment |
+-----+------+
|
|
|
|
+--------------------------------------------------+
| | |
| | |
| | |
| | |
| | |
| | |
+------v------+ +------v------+ +------v------+
|replicaset:v1| |replicaset:v2| |replicaset:v3|
+-------------+ +------+------+ +-------------+
|
|
+--------+---------+
| |
| |
+---v---+ +---v---+
|pod:v2 | |pod:v2 |
+-------+ +-------+
- deployment调度replicaset,pod由replicaset调度
- deployment管理多个replicaset版本,可用于回滚
- replicaset控制pod的行为,包括新增pod、删除pod
我们首先准备一个yaml文件用于测试:
root@k8s-master:~# more roll_update.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: image-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: image-update
spec:
containers:
- name: nginx
image: registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1
imagePullPolicy: Always
简单验证一下:
root@k8s-master:~# kubectl apply -f roll_update.yaml
deployment.extensions "update-deployment" created
root@k8s-master:~# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
update-deployment 3 3 3 3 54s
root@k8s-master:~# kubectl get rs
NAME DESIRED CURRENT READY AGE
update-deployment-7db77f7cc6 3 3 3 56s
root@k8s-master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
update-deployment-7db77f7cc6-7j49g 1/1 Running 0 1m
update-deployment-7db77f7cc6-b75wn 1/1 Running 0 1m
update-deployment-7db77f7cc6-cfnt5 1/1 Running 0 1m
deployment、replicaset、pod都已经正常启动,下面分析一下他们的行为:
deployment
root@k8s-master:~# kubectl describe deploy update-deployment
Name: update-deployment
Namespace: default
...
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
...
NewReplicaSet: update-deployment-7db77f7cc6 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set update-deployment-7db77f7cc6 to 3
- deployment创建了一个replicaset,叫做update-deployment-7db77f7cc6(7db77f7cc6是replicaset的template hash值)
- 根据配置文件的要求,replicaset的副本数为3
replicaset
root@k8s-master:~# kubectl describe rs update-deployment-7db77f7cc6
Name: update-deployment-7db77f7cc6
Namespace: default
...
Controlled By: Deployment/update-deployment
Replicas: 3 current / 3 desired
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 3m replicaset-controller Created pod: update-deployment-7db77f7cc6-7j49g
Normal SuccessfulCreate 3m replicaset-controller Created pod: update-deployment-7db77f7cc6-b75wn
Normal SuccessfulCreate 3m replicaset-controller Created pod: update-deployment-7db77f7cc6-cfnt5
replicaset创建了3个pod
pod
root@k8s-master:~# kubectl describe pod update-deployment-7db77f7cc6-7j49g
Name: update-deployment-7db77f7cc6-7j49g
Namespace: default
...
Status: Running
IP: 10.10.169.140
Controlled By: ReplicaSet/update-deployment-7db77f7cc6
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 9m default-scheduler Successfully assigned update-deployment-7db77f7cc6-7j49g to k8s-node2
Normal SuccessfulMountVolume 9m kubelet, k8s-node2 MountVolume.SetUp succeeded for volume "default-token-v9nkm"
Normal Pulling 9m kubelet, k8s-node2 pulling image "registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1"
Normal Pulled 9m kubelet, k8s-node2 Successfully pulled image "registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1"
Normal Created 9m kubelet, k8s-node2 Created container
Normal Started 9m kubelet, k8s-node2 Started container
- pod被replicaset创建之后,开始分配到worker节点、拉取镜像、启动容器等一系列操作
- 所以pod的命名方式是:update-deployment-7db77f7cc6-7j49g(deployment名字-replicaset模板hash名字-pod模板hash名字)
大家可能不禁会有疑问,为什么搞这么复杂,启动一个pod需要动用这么多组件呢?下面用一个场景说明为啥需要这么多组件:
镜像版本更新
镜像版本更新
当镜像版本有更新时(三种方法都可以实现,参考前一篇文章:更新k8s镜像版本的三种方式),既要保证服务可用,又要保证在线更新,流程应该是:
- 先增加一个pod,镜像版本为新版本
- pod可用之后,删除一个老版本pod
- 循环第1、2步,直到老版本pod全部删除,新版本的pod全部可用
上述的这个过程就是replicaset的作用,它根据需求,自动的增加新版本pod,然后删除老版本pod,直到老版本pod全部删除,新版本的pod全部可用
如果此时版本需要回退,那replicaset需要把刚才的步骤逆向更新一遍,实现版本回退
deployment的作用就是管理replicaset。deployment会保存各个版本的replicaset,一旦需要进行版本回滚,deployment会立即回滚replicaset的版本,从而控制pod状态
下面测试一下:
使用patch命令更新镜像版本,并且使用pause命令来观察:
root@k8s-master:~# kubectl patch deployment update-deployment \
--patch '{"spec": {"template": {"spec": {"containers": [{"name": "nginx","image":"registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2"}]}}}}' \
&& kubectl rollout pause deployment update-deployment
deployment.extensions "update-deployment" patched
deployment.apps "update-deployment" paused
此时pod状态:
root@k8s-master:~# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE
update-deployment-7db77f7cc6-7j49g 1/1 Running 0 1h 10.10.169.140 k8s-node2
update-deployment-7db77f7cc6-b75wn 1/1 Running 0 1h 10.10.235.211 k8s-master
update-deployment-7db77f7cc6-cfnt5 1/1 Terminating 0 1h 10.10.36.126 k8s-node1
update-deployment-7fb7b4b557-6987x 1/1 Running 0 7s 10.10.36.127 k8s-node1
update-deployment-7fb7b4b557-dxdqb 1/1 Running 0 10s 10.10.169.139 k8s-node2
新增了2个pod,而删除了1个老版本的pod
此时replicaset状态:
root@k8s-master:~# kubectl get rs -owide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
update-deployment-7db77f7cc6 2 2 2 1h nginx registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1 app=roll-update,pod-template-hash=3863393772
update-deployment-7fb7b4b557 2 2 2 4m nginx registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2 app=roll-update,pod-template-hash=3963606113
有一个新版本的replicaset创建了出来,并且需求的pod数量为2,而原来的replicaset需求的pod数量从3降为2
查看replicaset版本:
root@k8s-master:~# kubectl rollout history deploy update-deployment
deployments "update-deployment"
REVISION CHANGE-CAUSE
1 <none>
2 update version to v2
新增了一个版本2
由于使用pause命令,更新过程到此会卡主,我们让更新的过程继续下去:
root@k8s-master:~# kubectl rollout resume deployment update-deployment
deployment.apps "update-deployment" resumed
查看状态:
root@k8s-master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
update-deployment-7fb7b4b557-6987x 1/1 Running 0 15m
update-deployment-7fb7b4b557-dxdqb 1/1 Running 0 15m
update-deployment-7fb7b4b557-wg5c8 1/1 Running 0 1m
root@k8s-master:~# kubectl get rs -owide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
update-deployment-7db77f7cc6 0 0 0 1h nginx registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1 app=roll-update,pod-template-hash=3863393772
update-deployment-7fb7b4b557 3 3 3 14m nginx registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2 app=roll-update,pod-template-hash=3963606113
v1版本的replicaset已经没有pod,但是历史记录还是保留的,可以通过deployment调度快速回滚
详谈kubernetes滚动更新-1的更多相关文章
- kubernetes滚动更新
系列目录 简介 当kubernetes集群中的某个服务需要升级时,传统的做法是,先将要更新的服务下线,业务停止后再更新版本和配置,然后重新启动并提供服务.如果业务集群规模较大时,这个工作就变成了一个挑 ...
- Kubernetes——滚动更新和数据管理
k8s——滚动更新滚动更新就是一次只更新一小部分副本,更新成功之后再更新更多的副本,最终完成所有副本的更新.滚动更新最大的好处是零停机,整个更新的过程中始终有副本运行,从而保证了业务的连续性.kube ...
- Kubernetes滚动更新介绍及使用-minReadySeconds
滚动升级Deployment 现在我们将刚刚保存的yaml文件中的nginx镜像修改为 nginx:1.13.3,然后在spec下面添加滚动升级策略: 1 2 3 4 5 6 7 minReady ...
- kubernetes 滚动更新发布及回滚
基本命令 记录历史 --record kubectl apply -f **** --record 查看当前状态 kubectl rollout status deployment/demo -w ...
- Kubernetes集群中Service的滚动更新
Kubernetes集群中Service的滚动更新 二月 9, 2017 0 条评论 在移动互联网时代,消费者的消费行为已经“全天候化”,为此,商家的业务系统也要保持7×24小时不间断地提供服务以满足 ...
- Kubernetes Deloyment实现滚动更新
目录 滚动更新简介 使用kubectl rolling-update更新RC Deployment的rolling-update 滚动更新简介 当kubernetes集群中的某个服务需要升级时,传统的 ...
- 详谈kubernetes更新-2
系列目录 本文详细探索deployment在滚动更新时候的行为 要详细探讨的参数描述: livenessProbe:存活性探测.判断pod是否已经停止 readinessProbe:就绪性探测.判断p ...
- Kubernetes Pod应用的滚动更新(八)
一.环境准备 我们紧接上一节的环境,进行下面的操作,如果不清楚的,可以先查看上一篇博文. 滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新.滚动更新的最大的好处是零停 ...
- kubernetes之DaemonSet以及滚动更新
1.什么是DaemonSet? 1.1DaemonSet是Pod控制器的又一种实现方式,用于在集群中的全部节点上同时运行一份指定的Pod资源副本,后续加入集群的节点也会自动创建一个相关的Pod对象,当 ...
随机推荐
- [cogs729]圆桌问题(最大流)
传送门 模型 二分图多重匹配问题,可以用最大流解决. 实现 建立二分图,每个单位为X集合中的顶点,每个餐桌为Y集合中的顶点,增设附加源S和汇T. 1.从S向每个Xi顶点连接一条容量为该单位人数的有向边 ...
- 刷题总结——Human Gene Functions(hdu1080)
题目: Problem Description It is well known that a human gene can be considered as a sequence, consisti ...
- bzoj 2788 [Poi2012]Festival 差分约束+tarjan+floyd
题目大意 有n个正整数X1,X2,...,Xn,再给出m1+m2个限制条件,限制分为两类: 1.给出a,b (1<=a,b<=n),要求满足Xa + 1 = Xb 2.给出c,d (1&l ...
- POJ3928 Ping pong
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description N(3<= ...
- 共享内存之——mmap内存映射
共享内存允许两个或多个进程共享一给定的存储区,因为数据不需要来回复制,所以是最快的一种进程间通信机制.共享内存可以通过mmap()映射普通文件 (特殊情况下还可以采用匿名映射)机制实现,也可以通过sy ...
- 基于现有图像数据创建自定义像素格式的 BufferedImage
在最近的一个项目中,需要实现 Mac OS X 环境下的摄像头图像实时捕获并转换为 Java 中的 BufferedImage 对象.首先通过开发一个本地库实现 Mac OS X 的摄像头图像捕获,采 ...
- python笔记3:注释命名风格
6.注释: 行注释采用 # 开头,多行注释使用三个单引号 (''') 或三个双引号 ("' '"),注释不需要对齐 三引号让程序员从引号和特殊字符串的泥潭里面解脱出来,自始至终保 ...
- 前端笔记之Vue(七)Vue-router&axios&Vue插件&Mock.js&cookie|session&加密
一.Vue-router(路由) 1.1路由创建 官网:https://router.vuejs.org/zh/ 用 Vue.js + Vue Router 创建单页应用,是非常简单的.使用 Vue. ...
- char 转string
c++: string.c_str() ---------> c: char c; string str;stringstream stream;stream << ...
- guake使用
1. 安装:sudo apt-get install guake 2. 在终端输入guake 3. f12:显示/隐藏 4. f11:全屏/正常屏切换 5. f2:重命名终端名 6. 还可以查看修改快 ...