作者介绍:王天夫 腾讯云后台开发工程师
腾讯云容器服务是基于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。
文章评论