http://www.dockerinfo.net/1128.html
什么是Deployment
Kubernetes Deployment提供了官方的用于更新Pod和Replica Set(下一代的Replication Controller)的方法,您可以在Deployment对象中只描述您所期望的理想状态(预期的运行状态),Deployment控制器为您将现在的实际状态转换成您期望的状态,例如,您想将所有的webapp:v1.0.9升级成webapp:v1.1.0,您只需创建一个Deployment,Kubernetes会按照Deployment自动进行升级。现在,您可以通过Deployment来创建新的资源(pod,rs,rc),替换已经存在的资源等。
Deployment集成了上线部署、滚动升级、创建副本、暂停上线任务,恢复上线任务,回滚到以前某一版本(成功/稳定)的Deployment等功能,在某种程度上,Deployment可以帮我们实现无人值守的上线,大大降低我们的上线过程的复杂沟通、操作风险。
Deployment的使用场景
下面是Deployment的典型用例:
- 使用Deployment来启动(上线/部署)一个Pod或者ReplicaSet
- 检查一个Deployment是否成功执行
- 更新Deployment来重新创建相应的Pods(例如,需要使用一个新的Image)
- 如果现有的Deployment不稳定,那么回滚到一个早期的稳定的Deployment版本
- 暂停或者恢复一个Deployment
创建Deployment
下面是一个简单的Deployment的例子,它创建了一个ReplicaSet来创建三个
nginx Pods:
cat > nginx-deployment.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
创建nginx(1.7.9)deployment示例:
kubectl create -f nginx-deployment.yaml --record
使用—record选项是为了记录当前执行的命令所创建/更新的资源,即使用—record选型执行的deployment都会被记录下来,用于以后查看每次deployment的细节,查看所有Deployment的历史,或者回滚到某一历史版本。
我们使用kubectl get deployment(s) [name]来查看Deployment的状态,下面的命令供参考:
kubectl get deployment nginx-deploymen NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment 3 3 3 3 22m
这里DESIRED为3,CURRENT为3,UP-TO-DATE,AVAILABLE均为3,说明Deployment按照您的预期(创建3个Nginx Pods)已经完成任务。
kubectl get deployement kubectl get rc kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-deployment-2035384211-nllka 1/1 Running 0 23m app=nginx,pod-template-hash=2035384211 nginx-deployment-2035384211-w1jqv 1/1 Running 0 23m app=nginx,pod-template-hash=2035384211 nginx-deployment-2035384211-x0c1a 1/1 Running 0 23m app=nginx,pod-template-hash=2035384211
查看Deployment的状态
kubectl get deployment nginx-deployment -o yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: "1" creationTimestamp: 2016-04-12T15:52:51Z generation: 2 labels: app: nginx name: nginx-deployment namespace: default resourceVersion: "539464" selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx-deployment uid: 9ca3c289-00c6-11e6-a09f-fa163e788d93 spec: replicas: 3 selector: matchLabels: app: nginx strategy: rollingUpdate: maxSurge: 1 maxUnavailable: 1 type: RollingUpdate template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx:1.7.9 imagePullPolicy: IfNotPresent name: nginx ports: - containerPort: 80 protocol: TCP resources: {} terminationMessagePath: /dev/termination-log dnsPolicy: ClusterFirst restartPolicy: Always securityContext: {} terminationGracePeriodSeconds: 30 status: availableReplicas: 3 observedGeneration: 2 replicas: 3 {.spec.replicas}被设置为3,那么Deploymentment会创建3个nginx(1.7.9)的副本 kubectl get deployment nginx-deployment -o yaml | grep [Gg]eneration generation: 2 observednGeneration: 2
更新Deployment
假设我们现在需要将刚刚创建的nginx从1.7.9版本升级到1.9.1,我们可以创建一个新的Deployment:
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.9.1 ports: - containerPort: 80
我们使用kubectl apply命令来发起Deployment更新:
kubectl apply -f new-nginx-deployment.yaml deployment “nginx-deployment” configured
也可以通过edit命令在线修改deployment:
kubectl edit deployment nginx-deployment deployment “nginx-deployment” configured
命令执行后,可以通过kubectl get pods [—show-label]来查看nginx是否已经从1.7.9更新至1.9.1
回滚Deployment
这里我们模拟一次失败的部署,我们利用一个不存在的镜像(例如:nginx:1.91),我们看接下来会发生什么。
先创建一个nginx(1.91)的Deployment:
cat > bad-nginx-deployment.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.91 ports: - containerPort: 80
更新这个deployment:
kubectl apply -f bad-nginx-deployment.yaml kubectl get rs NAME DESIRED CURRENT AGE nginx-deployment-1564180365 2 2 32m nginx-deployment-2035384211 0 0 37m nginx-deployment-3066724191 2 2 30m
查看deployment的创建情况:
kubectl get pods NAME READY STATUS RESTARTS AGE daemons-demo-29xri 1/1 Running 0 1h daemons-demo-j5z1k 1/1 Running 0 1h daemons-demo-oxzki 1/1 Running 0 1h daemons-demo-rsw8f 1/1 Running 0 1h nginx-deployment-1564180365-klelt 1/1 Running 0 33m nginx-deployment-1564180365-opmuu 1/1 Running 0 33m nginx-deployment-3066724191-qmqo2 0/1 ImagePullBackOff 0 2m nginx-deployment-3066724191-w6wd7 0/1 ImagePullBackOff 0 2m
到此时,我们发现最新一次Deployment出现了问题,没有找到相应的镜像,部署失败。对于这种情形,我们选择将Deployment恢复到上一版本,注意,这里不是通过恢复之前的RC或者Pods或者RS,而是回滚Deployment。
查看deployment的历史记录:
kubectl rollout history deployment nginx-deployment deployments "nginx-deployment": REVISION CHANGE-CAUSE 1 kubectl create -f nginx-deployment.yaml --record 4 kubectl apply -f new-nginx-deployment.yaml --record 5 kubectl apply -f bad-nginx-deployment.yaml --record
REVISION的三个序号【1,4,5】说明您可以回到这三个版本中的任一一个,现在我们选择回退到序号4即nginx:1.9.1版本:
kubectl get rs NAME DESIRED CURRENT AGE nginx-deployment-1564180365 2 2 39m nginx-deployment-2035384211 0 0 43m nginx-deployment-3066724191 2 2 36m kubectl rollout undo deployment nginx-deployment --to-revision=4 deployment "nginx-deployment" rolled back kubectl get rs NAME DESIRED CURRENT AGE nginx-deployment-1564180365 3 3 39m nginx-deployment-2035384211 0 0 44m nginx-deployment-3066724191 0 0 37m
现在Deployment已经完全恢复到nginx1.9.1时的正常情况,nginx又可以正常对外服务了。在以后的实际运维工作中,我们会采用这种方式来回滚应用版本,不再使用原来RC的方式。
暂停或恢复上线
在发布一个新的deployment过程中,可以暂停上线过程:
kubectl rollout pause deployment nginx-deployment kubectl get rs NAME DESIRED CURRENT AGE nginx-deployment-1564180365 3 3 45m nginx-deployment-2035384211 0 0 50m nginx-deployment-3066724191 0 0 42m
也可通过如下命令恢复/继续上线过程:
kubectl rollout resume deployment/nginx-deployment kubectl get rs NAME DESIRED CURRENT AGE nginx-deployment-1564180365 3 3 46m nginx-deployment-2035384211 0 0 51m nginx-deployment-3066724191 0 0 44m