Deployment为Pod与ReplicaSet提供了一个声明试定义方法,用来替代以前的ReplicationController来方便管理应用。典型的应用场景包括:
- 定义Deployment来创建Pod和ReplicaSet
- 滚动升级和回滚应用
- 扩容和缩容
- 暂停和继续Deployment
部署一个简单的Nginx应用
apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: web name: web spec: replicas: 2 selector: matchLabels: app: web strategy: {} template: metadata: creationTimestamp: null labels: app: web spec: containers: - image: nginx:1.14 name: nginx resources: {} status: {}
创建
kubectl apply -f web.yaml
扩容
kubectl scale deployment nginx-deployment --replicas 10
如果集群支持horizontal pod autoscaling的话,还可以为deployment设置自动扩展
kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=10
更新镜像
kubectl set image depleyment nginx-deployment nginx=nginx:1.15.1
回滚
kubectl rollout undo deployment nginx-deployment
编辑deployment
kubectl edit deployment nginx-deployment
查看rollout状态
kubectl rollout status deployment nginx-deployment
查看历史rollout状态
kubectl rollout history deployment nginx-deployment
Deployment更新策略
Deployment可以保证升级时只有一定数量的pod是down的。默认的,他会确保至少有比期望的Pod数量少一个是up状态(最多一个不可用)
Deployment同时可以保证只创建出超过期望数量的一定数量的Pod。默认的,他会确保最多比期望的Pod数量多一个Pod是up的(最多1个surge)
Rollover(多个rollout并行)
假如创建了一个有5个nginx:1.7.9replica的Deployment,但是当还有3个nginx:1.7.9的replica创建出来的时候你就开始更新包含5个nginx:1.9.1replica的Deployment。在这种情况下,Deployment会立即杀掉已经创建的3个nginx:1.7.9的Pod,并开始创建nginx:1.9.1的Pod。他不会等到所有的5个nginx:1.7.9的Pod都创建完成后才开始改变航道
回退Deployment
kubectl set image depleyment nginx-deployment nginx=nginx:1.15.1 kubectl rollout status deployment nginx-deployment kubectl get pods kubectl rollout history deployment nginx-deployment kubectl rollout undo deployment nginx-deployment kubectl rollout undo deployment nginx-deployment --to-reversion=2 #可以使用--reversion参数指定某个历史版本 kubectl rollout pause deployment nginx-deployment #暂停Deployment的更新
可以使用kubectl rollout status命令查看Deployment是否完成。如果rollout成功完成,kubectl rollout status将会返回一个0值的Exit Code
[root@mvpemen-game--test yaml]# kubectl rollout status deployment nignx-deployment deployment "nginx-deployment" successfully rolled out [root@mvpemen-game--test yaml]# echo $? 0
清理Policy
可以通过设置.spec.revisionHistoryLimit项来指定deployment最多保留多少reversion历史记录。默认的会保留所有的reversion;如果该项设置为0,Deployment就不允许回退