Deployment简述
- Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义(declarative)方法,用来替代以前的ReplicationController 来方便的管理应用。典型的应用场景包括:
- 定义Deployment来创建Pod和ReplicaSet
- 滚动升级和回滚应用
- 扩容和缩容
- 暂停和继续Deployment
您只需要在 Deployment 中描述您想要的目标状态是什么,Deployment controller 就会帮您将 Pod 和ReplicaSet 的实际状态改变到您的目标状态。您可以定义一个全新的 Deployment 来创建 ReplicaSet 或者删除已有的 Deployment 并创建一个新的来替换。
- Deployment用例:
- 使用Deployment来创建ReplicaSet。ReplicaSet在后台创建pod。
- 检查启动状态,看它是成功还是失败。
- 通过更新Deployment的PodTemplateSpec字段来声明Pod的新状态
- 创建一个新的ReplicaSet,Deployment会按照控制的速率将pod从旧的ReplicaSet移动到新的ReplicaSet中。
- 如果当前状态不稳定,回滚到之前的Deployment revision。每次回滚都会更新Deployment的revision。
- 扩容Deployment以满足更高的负载。
- 暂停Deployment来应用PodTemplateSpec的多个修复,然后恢复上线。
- 根据Deployment 的状态判断上线是否hang住了。
-
清除旧的不必要的 ReplicaSet。
image.png
- 总结
- 用户通过 kubectl 创建 Deployment。
- Deployment 创建 ReplicaSet。
- ReplicaSet 创建 Pod。
对象的命名方式是:子对象的名字 = 父对象名字 + 随机字符串或数字。
image.jpg
Deployment资源清单
Deployment 也需要apiVersion,kind和metadata这些配置项
[root@k8s-master01 learning]# kubectl explain deployment
KIND: Deployment
VERSION: extensions/v1beta1
DESCRIPTION:
DEPRECATED - This group version of Deployment is deprecated by
apps/v1beta2/Deployment. See the release notes for more information.
Deployment enables declarative updates for Pods and ReplicaSets.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
metadata <Object>
Standard object metadata.
spec <Object>
Specification of the desired behavior of the Deployment.
status <Object>
Most recently observed status of the Deployment.
spec字段参数
- Replicas: 可选字段,指定期望的pod数量,默认是1
- Selector: 可选字段, 用来指定 label selector ,圈定Deployment管理的pod范围
- 如果被指定: 必须匹配 .spec.template.metadata.labels,否则它将被API拒绝
- 如果未指定: .spec.selector.matchLabels 默认是.spec.template.metadata.labels
- 在Pod的template跟.spec.template不同或者数量超过了.spec.replicas规定的数量的情况下,Deployment会杀掉label跟selector不同的Pod
- Template: 必须字段, 划分Pod的范围,Deployment中的pod template必须指定适当的label
- strategy: 可选字段, 指定新的Pod替换旧的Pod的策略,可以是"Recreate"或者是 "RollingUpdate"。默认值是"RollingUpdate"
- Recreate: 重建式更新,就是删一个建一个
- rollingUpdate:滚动更新,简单定义 更新期间pod最多有几个等
- maxSurge: 可选字段,用来指定可以超过期望的Pod数量的最大个数,默认是25%,之前是1
- maxUnavailable: 可选字段,用来指定不可用Pod数量的最大个数,默认是25%,之前是1
- revisionHistoryLimit: 可选字段,指定保留的旧的ReplicaSet数量,默认是10
- rollbackTo: 可选字段,设置该参数将触发回退操作,每次回退完成后,该值就会被清除
- revision: 可选字段,用来指定回退到的revision。默认是0,意味着回退到上一个revision。
- progressDeadlineSeconds: 可选字段,用来指定在系统报告Deployment的failed progressing——表现为resource的状态中type=Progressing、Status=False、 Reason=ProgressDeadlineExceeded前可以等待的Deployment进行的秒数
- minReadySeconds: 可选字段,指定没有任何容器crash的Pod并被认为是可用状态的最小秒数。默认是0(Pod在ready后就会被认为是可用状态)
- paused: 可选字段,用来指定暂停和恢复Deployment。Paused和没有paused的Deployment之间的唯一区别就是,所有对paused deployment中的PodTemplateSpec的修改都不会触发新的rollout。Deployment被创建之后默认是非paused。
定义资源清单
[root@k8s-master01 learning]# vim deployment-demo.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: deploy-demo
namespace: learning
spec:
replicas: 2
selector:
matchLabels:
app: deploy-demo
tag: learn-deploy
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: deploy-demo
tag: learn-deploy
spec:
containers:
- name: deploy-demo
image: nginx:1.10
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
failureThreshold: 3
httpGet:
path: /index.html
port: 80
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 2
successThreshold: 1
timeoutSeconds: 1
readinessProbe:
failureThreshold: 3
httpGet:
path: /index.html
port: 80
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 2
successThreshold: 2
timeoutSeconds: 1
常用命令
查看资源
[root@k8s-master01 learning]# kubectl apply -f deployment-demo.yaml
deployment.apps/deploy-demo created
# 查看创建的deployments
[root@k8s-master01 learning]# kubectl get deployments. -n learning
NAME READY UP-TO-DATE AVAILABLE AGE
deploy-demo 2/2 2 2 2m4s
# 查看deployment管理的replicaset
[root@k8s-master01 learning]# kubectl get replicasets. -n learning
NAME DESIRED CURRENT READY AGE
deploy-demo-5c64bd4959 2 2 2 2m11s
# 查看replicaset管理的pod
[root@k8s-master01 learning]# kubectl get pods -n learning
NAME READY STATUS RESTARTS AGE
deploy-demo-5c64bd4959-5ltxf 1/1 Running 0 2m18s
deploy-demo-5c64bd4959-b6pth 1/1 Running 0 2m18s
# 查看详细信息
[root@k8s-master01 learning]# kubectl describe deployments. -n learning deploy-demo
Name: deploy-demo
Namespace: learning
CreationTimestamp: Mon, 28 Jan 2019 20:01:31 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1beta2","kind":"Deployment","metadata":{"annotations":{},"name":"deploy-demo","namespace":"learning"},"spec":{"replic...
Selector: app=deploy-demo,tag=learn-deploy
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 0 max unavailable, 1 max surge
Pod Template:
Labels: app=deploy-demo
tag=learn-deploy
Containers:
deploy-demo:
Image: nginx:1.10
Port: 80/TCP
Host Port: 0/TCP
Liveness: http-get http://:80/index.html delay=20s timeout=1s period=2s #success=1 #failure=3
Readiness: http-get http://:80/index.html delay=20s timeout=1s period=2s #success=2 #failure=3
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: deploy-demo-5c64bd4959 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 6m46s deployment-controller Scaled up replica set deploy-demo-5c64bd4959 to 2
修改Pod数量
1. 修改配置文件,然后apply -f
2. edit在线编辑
3. 打补丁的方式
[root@k8s-master01 learning]# kubectl patch deployments. -n learning deploy-demo -p '{"spec":{"replicas":5}}'
deployment.extensions/deploy-demo patched
[root@k8s-master01 learning]# kubectl get pods -n learning
NAME READY STATUS RESTARTS AGE
deploy-demo-5c64bd4959-89spz 1/1 Running 0 3m33s
deploy-demo-5c64bd4959-hxg6b 1/1 Running 0 33s
deploy-demo-5c64bd4959-k7dnr 1/1 Running 0 3m9s
deploy-demo-5c64bd4959-pz2wb 1/1 Running 0 33s
deploy-demo-5c64bd4959-vl55c 1/1 Running 0 33s
升级版本
1. 修改配置文件,然后apply -f
2. edit在线编辑
3. sed image修改
[root@k8s-master01 ~]# kubectl set image deployment deploy-demo deploy-demo=nginx:1.13 -n learning
deployment.extensions/deploy-demo image updated
[root@k8s-master01 ~]# kubectl get deployments. -n learning -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deploy-demo 5/5 3 5 41m deploy-demo nginx:1.13 app=deploy-demo,tag=learn-deploy
# 升级版本 查看升级过程
# 修改配置文件升级,观察升级过程,可以看出是先增加一个,当新增加的状态为running后才进行删除一个,删除后在增加,直到升级完成
[root@k8s-master01 learning]# sed -i 's/ image: nginx:1.10/ image: nginx:1.11/g' deployment-demo.yaml
[root@k8s-master01 learning]# kubectl apply -f deployment-demo.yaml & kubectl get pods -n learning -w -o wide -l app=deploy-demo
NAME READY STATUS RESTARTS AGE
deploy-demo-5c64bd4959-48frt 1/1 Running 0 45s
deploy-demo-5c64bd4959-rprrl 1/1 Running 0 45s
deployment.apps/deploy-demo configured
deploy-demo-5d56cdb4cd-llf4m 0/1 Pending 0 0s
deploy-demo-5d56cdb4cd-llf4m 0/1 Pending 0 0s
deploy-demo-5d56cdb4cd-llf4m 0/1 ContainerCreating 0 0s
deploy-demo-5d56cdb4cd-llf4m 0/1 Running 0 2s
deploy-demo-5d56cdb4cd-llf4m 1/1 Running 0 23s
deploy-demo-5c64bd4959-48frt 1/1 Terminating 0
deploy-demo-5d56cdb4cd-trtfz 0/1 Pending 0 0s
deploy-demo-5d56cdb4cd-trtfz 0/1 Pending 0 0s
deploy-demo-5d56cdb4cd-trtfz 0/1 ContainerCreating 0 0s
deploy-demo-5c64bd4959-48frt 0/1 Terminating 0 68s
deploy-demo-5d56cdb4cd-trtfz 0/1 Running 0 1s
deploy-demo-5c64bd4959-48frt 0/1 Terminating 0 69s
deploy-demo-5c64bd4959-48frt 0/1 Terminating 0 69s
deploy-demo-5c64bd4959-48frt 0/1 Terminating 0 73s
deploy-demo-5c64bd4959-48frt 0/1 Terminating 0 73s
deploy-demo-5d56cdb4cd-trtfz 1/1 Running 0 24s
deploy-demo-5c64bd4959-rprrl 1/1 Terminating 0 92s
deploy-demo-5c64bd4959-rprrl 0/1 Terminating 0 93s
deploy-demo-5c64bd4959-rprrl 0/1 Terminating 0 101s
deploy-demo-5c64bd4959-rprrl 0/1 Terminating 0 101s
# 查看升级后的rs
[root@k8s-master01 learning]# kubectl get rs -n learning -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-demo-5c64bd4959 0 0 0 5m7s deploy-demo nginx:1.10 app=deploy-demo,pod-template-hash=5c64bd4959,tag=learn-deploy
deploy-demo-5d56cdb4cd 2 2 2 4m22s deploy-demo nginx:1.11 app=deploy-demo,pod-template-hash=5d56cdb4cd,tag=learn-deploy
回滚到上个版本
只要 Deployment 的 rollout 被触发就会创建一个 revision。也就是说当且仅当 Deployment 的 Pod template(如.spec.template)被更改,例如更新template 中的 label 和容器镜像时,就会创建出一个新的 revision
# 查看最近的历史版本
[root@k8s-master01 learning]# kubectl rollout history deployment -n learning deploy-demo
deployment.extensions/deploy-demo
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
# 查看历史版本信息
[root@k8s-master01 learning]# kubectl rollout history deployment -n learning deploy-demo --revision=2
deployment.extensions/deploy-demo with revision #2
Pod Template:
Labels: app=deploy-demo
pod-template-hash=5d56cdb4cd
tag=learn-deploy
Containers:
deploy-demo:
Image: nginx:1.11
Port: 80/TCP
Host Port: 0/TCP
Liveness: http-get http://:80/index.html delay=20s timeout=1s period=2s #success=1 #failure=3
Readiness: http-get http://:80/index.html delay=20s timeout=1s period=2s #success=2 #failure=3
Environment: <none>
Mounts: <none>
Volumes: <none>
# 回退历史版本,默认是回退到上一个版本
[root@k8s-master01 learning]# kubectl rollout undo deployment -n learning deploy-demo
deployment.extensions/deploy-demo rolled back
# 第二个版本已经不存在了,已经成了最新的第四个版本
[root@k8s-master01 learning]# kubectl rollout history deployment -n learning deploy-demo
deployment.extensions/deploy-demo
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
# 通过这里可以看到已经回退到了1.11版本
[root@k8s-master01 learning]# kubectl get rs -n learning -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-demo-57cdd6d965 0 0 0 2m19s deploy-demo nginx:1.12 app=deploy-demo,pod-template-hash=57cdd6d965,tag=learn-deploy
deploy-demo-5c64bd4959 0 0 0 10m deploy-demo nginx:1.10 app=deploy-demo,pod-template-hash=5c64bd4959,tag=learn-deploy
deploy-demo-5d56cdb4cd 2 2 2 9m44s deploy-demo nginx:1.11 app=deploy-demo,pod-template-hash=5d56cdb4cd,tag=learn-deploy
回滚到指定版本
# 查看现存的版本
[root@k8s-master01 learning]# kubectl rollout history deployment -n learning deploy-demo
deployment.extensions/deploy-demo
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
# 回滚到第一个版本
[root@k8s-master01 learning]# kubectl rollout undo deployment -n learning deploy-demo --to-revision=1
deployment.extensions/deploy-demo rolled back
# 可以看到第一个版本已经变成了现在的第五个版本
[root@k8s-master01 learning]# kubectl rollout history deployment -n learning deploy-demo
deployment.extensions/deploy-demo
REVISION CHANGE-CAUSE
3 <none>
4 <none>
5 <none>
[root@k8s-master01 learning]# kubectl get rs -n learning -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-demo-57cdd6d965 0 0 0 5m31s deploy-demo nginx:1.12 app=deploy-demo,pod-template-hash=57cdd6d965,tag=learn-deploy
deploy-demo-5c64bd4959 2 2 2 13m deploy-demo nginx:1.10 app=deploy-demo,pod-template-hash=5c64bd4959,tag=learn-deploy
deploy-demo-5d56cdb4cd 0 0 0 12m deploy-demo nginx:1.11 app=deploy-demo,pod-template-hash=5d56cdb4cd,tag=learn-deploy
暂停和恢复
你可以在出发一次或多次更新前暂停一个Deployment,然后再恢复它。这样你就能多次暂停和恢复Deployment,在此期间进行一些修复工作,而不会出发不必要的rollout
1. 更新nginx到1.13版本,并配置暂停deployment
[root@k8s-master01 learning]# kubectl set image deployment deploy-demo deploy-demo=nginx:1.13 -n learning && kubectl rollout pause deployment deploy-demo -n learning
deployment.extensions/deploy-demo image updated
deployment.extensions/deploy-demo paused
2. 监控更新的过程,因为pause命令,只是新增了一个资源,并没有去删除旧资源
[root@k8s-master01 learning]# kubectl get pods -n learning
NAME READY STATUS RESTARTS AGE
deploy-demo-5c64bd4959-rm99c 1/1 Running 0 2m46s
deploy-demo-5c64bd4959-x7ctm 1/1 Running 0 2m23s
deploy-demo-6b876f975b-6kq4d 1/1 Running 0 83s #新加的
# 在rs里面可以清楚的看到
[root@k8s-master01 learning]# kubectl get rs -l app=deploy-demo -n learning -o wide -n learning
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-demo-57cdd6d965 0 0 0 44m deploy-demo nginx:1.12 app=deploy-demo,pod-template-hash=57cdd6d965,tag=learn-deploy
deploy-demo-5c64bd4959 2 2 2 52m deploy-demo nginx:1.10 app=deploy-demo,pod-template-hash=5c64bd4959,tag=learn-deploy
deploy-demo-5d56cdb4cd 0 0 0 51m deploy-demo nginx:1.11 app=deploy-demo,pod-template-hash=5d56cdb4cd,tag=learn-deploy
deploy-demo-6b876f975b 1 1 1 11m deploy-demo nginx:1.13 app=deploy-demo,pod-template-hash=6b876f975b,tag=learn-deploy
3. 确保更新的pod没问题了,继续更新
[root@k8s-master01 learning]# kubectl rollout resume deployment deploy-demo -n learning
deployment.extensions/deploy-demo resumed
4. 更新后结果
[root@k8s-master01 learning]# kubectl get pods -n learning
NAME READY STATUS RESTARTS AGE
deploy-demo-6b876f975b-6kq4d 1/1 Running 0 4m17s
deploy-demo-6b876f975b-szw4s 1/1 Running 0 53s
# 可以看到 都已经到了1.13版本了
[root@k8s-master01 learning]# kubectl get rs -l app=deploy-demo -n learning -o wide -n learning
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-demo-57cdd6d965 0 0 0 46m deploy-demo nginx:1.12 app=deploy-demo,pod-template-hash=57cdd6d965,tag=learn-deploy
deploy-demo-5c64bd4959 0 0 0 54m deploy-demo nginx:1.10 app=deploy-demo,pod-template-hash=5c64bd4959,tag=learn-deploy
deploy-demo-5d56cdb4cd 0 0 0 53m deploy-demo nginx:1.11 app=deploy-demo,pod-template-hash=5d56cdb4cd,tag=learn-deploy
deploy-demo-6b876f975b 2 2 2 13m deploy-demo nginx:1.13 app=deploy-demo,pod-template-hash=6b876f975b,tag=learn-deploy
网友评论