Deployments是kubernetes中的一种控制器,是比ReplicaSet更高级的概念,它最重的特性是支持对pod与ReplicaSet的声明式升级,声明式升级比其它方式的升级更安全可靠。需要注意的是用户不应该手动管理被Deployments创建的ReplicaSet。
使用案例
以下是几种典型的Deployments使用案例:
- Create a Deployment to rollout a ReplicaSet. The ReplicaSet creates Pods in the background. Check the status of the rollout to see if it succeeds or not.
- Declare the new state of the Pods by updating the PodTemplateSpec of the Deployment. A new ReplicaSet is created and the Deployment manages moving the Pods from the old ReplicaSet to the new one at a controlled rate. Each new ReplicaSet updates the revision of the Deployment.
- Rollback to an earlier Deployment revision if the current state of the Deployment is not stable. Each rollback updates the revision of the Deployment.
- Scale up the Deployment to facilitate more load.
- Pause the Deployment to apply multiple fixes to its PodTemplateSpec and then resume it to start a new rollout.
- Use the status of the Deployment as an indicator that a rollout has stuck.
- Clean up older ReplicaSets that you don’t need anymore
Creating a Deployment
以下Deployment示例创建一个ReplicaSet,ReplicaSet控制三个nginx 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
以上文件中除了kind变成Deployment外,与ReplicaSet的定义并无不同,Deployment据此创建ReplicaSet。
创建Deployment:
kubectl create -f https://k8s.io/examples/controllers/nginx-deployment.yaml
提示:可以在上述命令中追加--record选项,其作用是在Deployment的注解中记录下当前执行的命令,这个特性对于Deployment的变更审计、追踪很有用。
查看Deployments:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deployment 3 0 0 0 1s
输出的自字段含义:
- NAME: Deployments名称
- DESIRED:创建Deployments时指定的期望副本数量。
- CURRENT:当前正在运行的副本数量。
- UP-TO-DATE:成功升级的副本数量。
- AVAILABLE:可用副本的数量,注意副本在运行不等于可用。
- AGE:应用运行时间。
创建Deployments后,kubernetes在系统中保存代表Deployments规格与状态的数据,以上字段与系统中保存数据的对应关系:
- DESIRED:spec.replicas field.
- CURRENT:status.replicas field.
- UP-TO-DATE:status.updatedReplicas field.
- AVAILABLE:status.availableReplicas field.
查看Deployments的推进状态(rollout status):
kubectl rollout status deployment/nginx-deployment
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
deployment "nginx-deployment" successfully rolled out
结果显示正在等待部署完成,3个副本中的2两已经更新完成,稍后查看Deployments的状态:
kubectl get deployments
NAME DESIRED C