Cloud Native应用交付

  • 首页
  • 关于本站
  • 个人介绍
  • Downloads
  • Repo
    • Github
    • Container
  • F5
    • F5 Python SDK
    • F5-container
    • F5-LBaaS
  • 社交
    • 联系我
    • 微信/微博
    • 公众号
    • 打赏赞助
行至水穷处 坐看云起时
Cloud Native Application Services: cnadn.net
  1. 首页
  2. 容器/k8s
  3. 正文

[转]Deployment vs ReplicationController in Kubernetes

2017年07月12日 6575点热度 0人点赞 0条评论

作者介绍:王天夫 腾讯云后台开发工程师

腾讯云容器服务是基于Kubernetes打造的。在Kubernetes中,创建和管理容器都是以controller来实现,例如:Replication Controller,Deployment,PetSet等。使用controller来管理容器,可以使用户方便的对容器数量做扩缩容,升级容器等操作。此文主要选择了两个最常用的controller,从各自功能,优缺点方面进行对比,方便大家在后续自己直接使用原生的Kubernetes功能时做参考。

Replication Controller

Replication Controller为Kubernetes的一个核心内容,应用托管到Kubernetes之后,需要保证应用能够持续的运行,Replication Controller就是这个保证的key,主要的功能如下:

  • 确保pod数量:它会确保Kubernetes中有指定数量的Pod在运行。如果少于指定数量的pod,Replication Controller会创建新的,反之则会删除掉多余的以保证Pod数量不变。
  • 确保pod健康:当pod不健康,运行出错或者无法提供服务时,Replication Controller也会杀死不健康的pod,重新创建新的。
  • 弹性伸缩 :在业务高峰或者低峰期的时候,可以通过Replication Controller动态的调整pod的数量来提高资源的利用率。同时,配置相应的监控功能(Hroizontal Pod Autoscaler),会定时自动从监控平台获取Replication Controller关联pod的整体资源使用情况,做到自动伸缩。
  • 滚动升级:滚动升级为一种平滑的升级方式,通过逐步替换的策略,保证整体系统的稳定,在初始化升级的时候就可以及时发现和解决问题,避免问题不断扩大。

最后,来看一下官方的解释:

A replication controller ensures that a specified number of pod “replicas” are running at any one time. In other words, a replication controller makes sure that a pod or homogeneous set of pods are always up and available. If there are too many pods, it will kill some. If there are too few, the replication controller will start more. Unlike manually created pods, the pods maintained by a replication controller are automatically replaced if they fail, get deleted, or are terminated. For example, your pods get re-created on a node after disruptive maintenance such as a kernel upgrade. For this reason, we recommend that you use a replication controller even if your application requires only a single pod. You can think of a replication controller as something similar to a process supervisor, but rather than individual processes on a single node, the replication controller supervises multiple pods across multiple nodes.

Deployment

Deployment同样为Kubernetes的一个核心内容,主要职责同样是为了保证pod的数量和健康,90%的功能与Replication Controller完全一样,可以看做新一代的Replication Controller。但是,它又具备了Replication Control,ler之外的新特性:

Replication Controller全部功能:Deployment继承了上面描述的Replication Controller全部功能。

事件和状态查看:可以查看Deployment的升级详细进度和状态。

回滚:当升级pod镜像或者相关参数的时候发现问题,可以使用回滚操作回滚到上一个稳定的版本或者指定的版本。

版本记录: 每一次对Deployment的操作,都能保存下来,给予后续可能的回滚使用。

暂停和启动:对于每一次升级,都能够随时暂停和启动。

多种升级方案:Recreate----删除所有已存在的pod,重新创建新的; RollingUpdate----滚动升级,逐步替换的策略,同时滚动升级时,支持更多的附加参数,例如设置最大不可用pod数量,最小升级间隔时间等等。

下面同样是官方的解释:

A Deployment provides declarative updates for Pods and Replica Sets (the next-generation Replication Controller). You only need to describe the desired state in a Deployment object, and the Deployment controller will change the actual state to the desired state at a controlled rate for you. You can define Deployments to create new resources, or replace existing ones by new ones.

Replication Controller vs Deployment

Deployment做为新一代的Replication Controller,好处不言而喻,不仅在功能上更为丰富,同时官方的文档中,也都推荐使用Deployment来管理pod,在google容器服务中,集群的系统pod,例如kube-dns,kube-proxy也都使用deployment来管理的,所以当大家需要选择的时候,也推荐使用Deployment在来管理pod。

Rolling updates Between Replication Controller and Deployment

Replication Controller

Replication Controller是使用kubectl rolling-update 来进行升级

1
2
3
$ kubectl rolling-<span class="hljs-keyword">update</span> <span class="hljs-keyword">NAME</span> \
    ([NEW_NAME] <span class="hljs-comment">--image=IMAGE | -f FILE)</span>
 

首先 我们定义一个nginx-v1.yaml的文件,副本数量为2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: ReplicationController
<span class="hljs-section">metadata:</span>
  name: nginx-v1
<span class="hljs-section">spec:</span>
  replicas: 2
  selector:
  app: nginx
  template:
  metadata:
  name: nginx
  labels:
  app: nginx
  spec:
  containers:
  - name: nginx
  image: nginx:1.8
  ports:
  - containerPort: 80
 

创建rc

1
2
$ kubectl create <span class="hljs-_">-f</span> nginx-v1.yaml
 

然后是用kubeclt查看创建的rc

1
2
$ kubectl <span class="hljs-keyword">get</span> rc/nginx-v1
 

现在需要将nginx的版本进行升级,从1.8升级到1.9,我们在定义一个1.9版本的Replication Controller的yaml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: ReplicationController
<span class="hljs-section">metadata:</span>
  name: nginx-v2
<span class="hljs-section">spec:</span>
replicas: 2
<span class="hljs-section">selector:</span>
app: nginx
<span class="hljs-section">template:</span>
<span class="hljs-section">metadata:</span>
name: nginx
<span class="hljs-section">labels:</span>
app: nginx
<span class="hljs-section">spec:</span>
<span class="hljs-section">containers:</span>
- name: nginx
image: nginx:1.9
<span class="hljs-section">ports:</span>
- containerPort: 80
 

开始滚动升级,update-period为更新间隔,这里设置的是10s:

1
2
3
4
5
6
7
8
9
10
$ kubectl rolling-<span class="hljs-keyword">update</span> nginx-v1 -f nginx-v2.yaml <span class="hljs-comment">--update-period=10s</span>
Scaling up nginx-v2 <span class="hljs-keyword">from</span> <span class="hljs-number">0</span> <span class="hljs-keyword">to</span> <span class="hljs-number">2.</span>scaling down nginx-v1 <span class="hljs-keyword">from</span> <span class="hljs-number">2</span> <span class="hljs-keyword">to</span> <span class="hljs-number">0</span>
Scaling nginx-v2 up <span class="hljs-keyword">to</span> <span class="hljs-number">1</span>
Scaling nginx-v1 down <span class="hljs-keyword">to</span> <span class="hljs-number">1</span>
Scaling nginx-v2 up <span class="hljs-keyword">to</span> <span class="hljs-number">2</span>
Scaling nginx-v1 down <span class="hljs-keyword">to</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">Update</span> successed.Delete nginx-v1
replicationcontroller <span class="hljs-string">"nginx-v1"</span> <span class="hljs-keyword">rolling</span> <span class="hljs-keyword">updated</span> <span class="hljs-keyword">to</span> <span class="hljs-string">"nginx-v2"</span>
Deployment
 

Deployment直接使用kubectl edit deployment/deploymentName 或者kubectl set方法就可以直接升级:

首先 我们同样定义一个nginx-deploy-v1.yaml的文件,副本数量为2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: extensions/v1beta1
kind: Deployment
<span class="hljs-section">metadata:</span>
name: nginx-deployment2
<span class="hljs-section">spec:</span>
replicas: 3
<span class="hljs-section">template:</span>
<span class="hljs-section">metadata:</span>
<span class="hljs-section">labels:</span>
test: nginx
<span class="hljs-section">spec:</span>
<span class="hljs-section">containers:</span>
- name: nginx
image: nginx:1.8
<span class="hljs-section">ports:</span>
- containerPort: 80
 

创建deployment

1
2
$ kubectl create <span class="hljs-_">-f</span> nginx-deploy-v1.yaml
 

然后是用kubeclt查看创建的rc

1
2
$ kubectl <span class="hljs-keyword">get</span> deployment/nginx-deployment2
 

现在需要将nginx的版本进行升级,从1.8升级到1.9,有两种方法可以使用: 直接set镜像:

1
2
3
$ kubectl <span class="hljs-keyword">set</span> image deployment/nginx-deployment2 nginx=nginx:<span class="hljs-number">1.9</span>
deployment <span class="hljs-string">"nginx-deployment2"</span> image <span class="hljs-keyword">updated</span>
 

或者直接edit:

1
2
3
$ kubectl edit deployment/nginx-deployment
deployment <span class="hljs-string">"nginx-deployment2"</span> edited
 

最后,查看详细信息来确定升级进度:

1
2
$ kubectl describe deployments
 

同时介绍一下附加的一些操作,暂停和继续,回滚升级:

1
2
3
4
$ kubectl rollout pause deployment/nginx-deployment2
$ kubectl rollout resume deployment/nginx-deployment2
$ kubectl rollout undo deployment/nginx-deployment2
 

总结:在腾讯云容器服务中,我们创建的无状态服务都是以Deployment来管理的容器,因为Deployment功能更多,并且也是官方推荐的,下一代的Replication Controller。

相关文章

  • 密码保护:F5OS tenant部署后的容器情况、网络接口情况
  • 密码保护:F5OS tenant镜像实例化后信息
  • 密码保护:F5OS docker-compose.yml
  • 密码保护:F5OS 底层容器、网络及k8s状态
  • 优雅下线业务Pod的方法
本作品采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2017年07月14日

纳米

linjing.io

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

页面AI聊天助手
文章目录
  • Replication Controller
  • Deployment
  • Replication Controller vs Deployment

纳米

linjing.io

☁️迈向Cloud Native ADC ☁️

认证获得:
TOGAF: ID 152743
Kubernetes: CKA #664
Microsoft: MCSE MCDBA
Cisco: CCNP
Juniper: JNCIS
F5:
F5 Certified Solution Expert, Security
F5 Certified Technology Specialist, LTM/GTM/APM/ASM
F5 Certified BIG-IP Administrator
  • 点击查看本博技术要素列表
  • 归档
    分类
    • AI
    • Automation
    • Avi Networks
    • Cisco ACI
    • CISCO资源
    • F5 with ELK
    • F5-Tech tips
    • F5技术
    • Juniper
    • Linux
    • NGINX
    • SDN
    • ServiceMesh
    • WEB编程
    • WINDOWS相关
    • 业界文章
    • 交换机技术
    • 化云为雨/Openstack
    • 协议原理
    • 容器/k8s
    • 我的工作
    • 我的生活
    • 网站技术
    • 路由器技术
    • 项目案例
    标签聚合
    network bigip api neutron nginx istio envoy irule flannel openstack docker DNS F5 k8s gtm
    最近评论
    汤姆 发布于 9 个月前(09月10日) 嗨,楼主,里面的json怎么下载啊,怎么收费啊?
    汤姆 发布于 9 个月前(09月09日) 大佬,kib的页面可以分享下吗?谢谢
    zhangsha 发布于 1 年前(05月12日) 资料发给我下,谢谢纳米同志!!!!lyx895@qq.com
    李成才 发布于 1 年前(01月02日) 麻烦了,谢谢大佬
    纳米 发布于 1 年前(01月02日) 你好。是的,因为以前下载系统插件在一次升级后将所有的下载生成信息全弄丢了。所以不少文件无法下载。DN...
    浏览次数
    • Downloads - 184,603 views
    • 联系我 - 118,966 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 117,961 views
    • Github - 104,600 views
    • F5常见log日志解释 - 80,103 views
    • 从传统ADC迈向CLOUD NATIVE ADC - 下载 - 76,057 views
    • Sniffer Pro 4 70 530抓包软件 中文版+视频教程 - 74,320 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 67,770 views
    • 关于本站 - 61,446 views
    • 这篇文档您是否感兴趣 - 55,760 views
    链接表
    • F5SE创新
    • Jimmy Song‘s Blog
    • SDNlab
    • Service Mesh社区
    • 三斗室
    • 个人profile
    • 云原生社区

    COPYRIGHT © 2023 Cloud Native 应用交付. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang

    京ICP备14048088号-1

    京公网安备 11010502041506号