作用
- 定义一组Pod的期望数量,controller会维持pod数量与期望数量一致
- 配置Pod发布方式,controller会按照给定策略更新Pod,保证更新过程中不可用的Pod数量在限定范围内
- 如果发布有问题,支持“一键”回滚
Deployment认为:管理的所有同版本的Pod都是一模一样的副本
语法
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
元信息
metadata:
name: nginx-deployment
labels:
app: nginx
期望的Pod数量
replicas: 3
Pod的选择器
selector:
matchLabels:
app: nginx
Pod模板
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
查看Deployment状态
$ kubectl create -f nginx-deployment.yaml
$ deployment.apps/nginx-deployment created
$
$ NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
$ nginx-deployment 3 3 3 3 35s
- DESIRED:期望Pod数量(replicas)
- CURRENT:当前实际的pod数量
- UP-TO-DATE:到达期望版本的pod数量
- AVAILABLE:运行中并可用的pod数量
- AGE:deployment创建的时长
更新镜像
$ kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1
- set image:设置镜像
- deployment.v1.apps:资源类型,也可以为deployment或者deployment.apps
- nginx-deployment:要更新的Deployment名字
- nginx=:需要新的容器名字
- nginx:1.9.1:新的镜像
回滚到Deployment上一个版本
$ kubectl rollout undo deployment/nginx-deployment
回滚Deployment到某一个版本,需要先查询版本列表:
需要先查询一下版本列表:
kubectl rollout history deployment.v1.apps/nginx-deployment
$ kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=2