文章插图
推荐阅读:膜拜!阿里内部都在强推的K8S(kubernetes)学习指南,不能再详细了Deployment可以做到很便捷的管理Pod,只需要在Deployment中描述一下希望的Pod状态是什么,包括定义Pod副本数、滚动升级和回滚应用、扩容和缩容、暂停和继续Deployment等,然后Deployment Controller就可以帮我们实现我们想要达到的状态 。
我们从一个例子入手:
apiVersion: Apps/v1kind: Deploymentmetadata:name: Nginx-deploymentspec:selector:matchLabels:app: nginxreplicas: 2template:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.7.9ports:- containerPort: 80
这个例子中:- 我们定义了一个Deployment,名字叫nginx-deployment;
- 通过spec.replicas字段定义了Pod的副本数是2;
- 通过spec.selector字段定义了被打上app: nginx的标签的Pod才会被管理;
- tmplate字段定义了这个Deployment管理的Pod应该是怎样的,具有怎样的属性;
文章插图
ReplicaSetReplicaSet是一个副本控制器,ReplicaSet可以用selector来控制Pod的数量,而Deployments是一个更高层次的概念,它管理ReplicaSets,并提供对pod的声明性更新以及许多其他的功能 。
Deployment通过控制ReplicaSet的个数来和属性,进而实现“水平扩展 / 收缩”和“滚动更新”这两个编排动作 。
水平扩展与滚动更新我们可以通过下面这个命令来创建Deployment:
kubectl create -f nginx-deployment.yaml --record
这里–record 参数作用是记录下你每次操作所执行的命令,以方便后面查看 。检查一下Deployment状态:
kubectl get deploymentsNAMEREADYUP-TO-DATEAVAILABLEAGEnginx-deployment2/22233m
- NAME:列举出Deployments 的名字;
- READY:显示了多少个应用的副本是可用的,目前副本数/期望副本数;
- UP-TO-DATE:显示了副本更新的次数,这里显示为2,说明已经做过2次更新;
- AVAILABLE:显示了多少个应用的副本是可用的;
- AGE:显示了应用运行的时间 。
kubectl scale deployment nginx-deployment --replicas=4deployment.apps/nginx-deployment scaled
然后我们可以使用rollout status来查看滚动更新的状态:kubectl rollout status deployment/nginx-deploymentWaiting for deployment "nginx-deployment" rollout to finish: 2 of 4 updated replicas are available...Waiting for deployment "nginx-deployment" rollout to finish: 3 of 4 updated replicas are available...deployment "nginx-deployment" successfully rolled out
上面的3 of 4 updated replicas are available表示已经有3个Pod进入了UP-TO-DATE 状态 。然后我们还可以查看一下ReplicaSet状态:
kubectl get rsNAMEDESIREDCURRENTREADYAGEnginx-deployment-9754ccbdf44444m
在运行了一个Deployment修改后,Deployment Controller会创建一个副本为ReplicaSet,并会生成一串随机字符,ReplicaSet 会把这个随机字符串加在它所控制的所有 Pod 的标签里,从而保证这些 Pod 不会与集群里的其他 Pod 混淆 。我们可以通过 kubectl get pods –show-labels来查看:
NAMEREADYSTATUSRESTARTSAGELABELSnginx-deployment-9754ccbdf-5pl2j1/1Running05m4sapp=nginx,pod-template-hash=9754ccbdfnginx-deployment-9754ccbdf-67d4g1/1Running048mapp=nginx,pod-template-hash=9754ccbdfnginx-deployment-9754ccbdf-9drgb1/1Running048mapp=nginx,pod-template-hash=9754ccbdfnginx-deployment-9754ccbdf-fdmrx1/1Running05m4sapp=nginx,pod-template-hash=9754ccbdf
下面我们看看Deployment的滚动更新是如何做的:我们使用set image 来修改deployment中的镜像
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1 --recorddeployment.apps/nginx-deployment image updated
然后我们可以通过kubectl describe查看滚动更新的过程:$ kubectl describe deployment nginx-deployment...Events:TypeReasonAgeFromMessage-------------------------... NormalScalingReplicaSet29sdeployment-controllerScaled down replica set nginx-deployment-9754ccbdf to 3NormalScalingReplicaSet29sdeployment-controllerScaled up replica set nginx-deployment-dc46b5ffc to 2NormalScalingReplicaSet12s (x2 over 11m)deployment-controllerScaled down replica set nginx-deployment-9754ccbdf to 2NormalScalingReplicaSet12sdeployment-controllerScaled up replica set nginx-deployment-dc46b5ffc to 3NormalScalingReplicaSet11sdeployment-controllerScaled down replica set nginx-deployment-9754ccbdf to 1NormalScalingReplicaSet11sdeployment-controllerScaled up replica set nginx-deployment-dc46b5ffc to 4NormalScalingReplicaSet11sdeployment-controllerScaled down replica set nginx-deployment-9754ccbdf to 0
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 深入理解DNS解析过程
- K8S 搭建一套带Dashboard的kubernetes集群
- Antrea 0.9.0 发布,Kubernetes 网络解决方案
- CentOS7 安装K8S的两种方案
- ansible部署k8s
- K8S常用命令
- 蚂蚁集团针对 K8s 中 Secret 安全防护的实践与探索
- K8S部署指导书
- 工程师进阶:深入TCP协议
- Istio+K8s,微服务的双剑合璧