1、基础知识

一个 Deployment 控制器为 PodsReplicaSets 提供声明式的更新能力。
你负责描述 Deployment 中的 目标状态,而 Deployment 控制器以受控速率更改实际状态, 使其变为期望状态。你可以定义 Deployment 以创建新的 ReplicaSet,或删除现有 Deployment, 并通过新的 Deployment 收养其资源。
注意:不要管理 Deployment 所拥有的 ReplicaSet 。

以下是 Deployments 的典型用例:

2、命令行运行一个Deployment

  1. [root@clientvm ~]# kubectl create deployment -h
  2. Usage:
  3. kubectl create deployment NAME --image=image -- [COMMAND] [args...] [options]
  4. Examples:
  5. # Create a deployment named my-dep that runs the busybox image.
  6. kubectl create deployment my-dep --image=busybox
  7. # Create a deployment with command
  8. kubectl create deployment my-dep --image=busybox -- date
  9. # Create a deployment named my-dep that runs the nginx image with 3 replicas.
  10. kubectl create deployment my-dep --image=nginx --replicas=3
  11. # Create a deployment named my-dep that runs the busybox image and expose port 5701.
  12. kubectl create deployment my-dep --image=busybox --port=5701

Example:

  1. ## 创建
  2. [root@clientvm ~]# kubectl create deployment testdep --image=nginx -n mytest
  3. deployment.apps/testdep created
  4. ## 查看
  5. [root@clientvm k8s]# kubectl get deployments.apps -n mytest
  6. NAME READY UP-TO-DATE AVAILABLE AGE
  7. testdep 1/1 1 1 44s
  8. [root@clientvm k8s]# kubectl get pod -n mytest
  9. NAME READY STATUS RESTARTS AGE
  10. testdep-64c98ff7d7-9ph5j 1/1 Running 0 50s
  11. ## 扩展
  12. [root@clientvm ~]# kubectl get deployments.apps -n mytest
  13. NAME READY UP-TO-DATE AVAILABLE AGE
  14. testdep 2/2 2 2 4m6s
  15. [root@clientvm ~]# kubectl get pod -n mytest
  16. NAME READY STATUS RESTARTS AGE
  17. testdep-64c98ff7d7-6drmb 1/1 Running 0 64s
  18. testdep-64c98ff7d7-9ph5j 1/1 Running 0 4m9s

3、创建Deployment

3.1 生成配置文件

  1. [root@clientvm ~]# kubectl -n mytest create deployment dep-from-file --image=nginx --dry-run=client -o yaml >dep-from-file.yaml

3.2 根据需要修改配置

  1. [root@clientvm ~]# vim dep-from-file.yaml
  2. [root@clientvm ~]#
  3. [root@clientvm ~]# cat dep-from-file.yaml
  4. apiVersion: apps/v1
  5. kind: Deployment
  6. metadata:
  7. labels:
  8. app: dep-from-file
  9. name: dep-from-file
  10. namespace: mytest
  11. spec:
  12. replicas: 2
  13. selector:
  14. matchLabels:
  15. app: dep-from-file
  16. template:
  17. metadata:
  18. creationTimestamp: null
  19. labels:
  20. app: dep-from-file
  21. spec:
  22. containers:
  23. - image: nginx
  24. name: nginx
  25. imagePullPolicy: IfNotPresent

3.3 创建Deployment

[root@clientvm ~]# kubectl apply -f dep-from-file.yaml
deployment.apps/dep-from-file created
[root@clientvm ~]# kubectl get deployments.apps -n mytest
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
dep-from-file   2/2     2            2           15s
testdep         2/2     2            2           12m

3.4 查看RS

[root@clientvm ~]# kubectl get rs -n mytest
NAME                       DESIRED   CURRENT   READY   AGE
dep-from-file-7b78bc4877   2         2         2       72s
testdep-64c98ff7d7         2         2         2       13m

3.5 弹性伸缩

## 修改配置文件,修改replicas为4
[root@clientvm ~]# vim dep-from-file.yaml
[root@clientvm ~]# cat dep-from-file.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: dep-from-file
  name: dep-from-file
  namespace: mytest
spec:
  replicas: 4
  selector:
  ......

## 应用修改
[root@clientvm ~]# kubectl apply -f dep-from-file.yaml
deployment.apps/dep-from-file configured

## 查看
[root@clientvm ~]# kubectl get deployments.apps -n mytest
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
dep-from-file   4/4     4            4           3m41s

3.6 滚动更新

[root@clientvm ~]# kubectl set image -h
Update existing container image(s) of resources.
Usage:
  kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N
[options]

Examples:
  # Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'.
  kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1

  # Update all deployments' and rc's nginx container's image to 'nginx:1.9.1'
  kubectl set image deployments,rc nginx=nginx:1.9.1 --all

  # Update image of all containers of daemonset abc to 'nginx:1.9.1'
  kubectl set image daemonset abc *=nginx:1.9.1

  # Print result (in yaml format) of updating nginx container image from local file, without hitting the server
  kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml

Example 1:

[root@clientvm ~]# kubectl get deployments.apps -n mytest
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
dep-from-file   4/4     4            4           2m32s
testdep         2/2     2            2           24m
[root@clientvm ~]#
[root@clientvm ~]# kubectl set image deployment dep-from-file nginx=nginx:1.9.1 -n mytest
deployment.apps/dep-from-file image updated

## 查看
[root@clientvm ~]# kubectl rollout status deployment dep-from-file -n mytest
deployment "dep-from-file" successfully rolled out

[root@clientvm ~]# kubectl rollout status deployment dep-from-file -n mytest
deployment "dep-from-file" successfully rolled out
[root@clientvm ~]# kubectl describe deployments.apps -n mytest  dep-from-file
Name:                   dep-from-file
Namespace:              mytest
......
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  39m   deployment-controller  Scaled up replica set dep-from-file-7b78bc4877 to 4
  Normal  ScalingReplicaSet  34m   deployment-controller  Scaled up replica set dep-from-file-7dfb96d9dd to 1
  Normal  ScalingReplicaSet  34m   deployment-controller  Scaled down replica set dep-from-file-7b78bc4877 to 3
  Normal  ScalingReplicaSet  34m   deployment-controller  Scaled up replica set dep-from-file-7dfb96d9dd to 2
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled down replica set dep-from-file-7b78bc4877 to 2
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled up replica set dep-from-file-7dfb96d9dd to 3
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled down replica set dep-from-file-7b78bc4877 to 1
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled up replica set dep-from-file-7dfb96d9dd to 4
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled down replica set dep-from-file-7b78bc4877 to 0

Example 2:

[root@clientvm ~]# kubectl set image deployment dep-from-file nginx=nginx:1.8.1 -n mytest --record
deployment.apps/dep-from-file image updated

[root@master ~]# kubectl get pod -n mytest
NAME                             READY   STATUS              RESTARTS   AGE
dep-from-file-7478c844bc-6x8d4   0/1     ContainerCreating   0          2m
dep-from-file-7478c844bc-bf2rw   1/1     Running             0          66s
dep-from-file-7478c844bc-krjj4   1/1     Running             0          2m
dep-from-file-7478c844bc-tvsv4   0/1     ContainerCreating   0          64s
dep-from-file-7dfb96d9dd-9hzls   1/1     Running             0          39m

[root@clientvm ~]# kubectl rollout status deployment dep-from-file -n mytest
Waiting for deployment "dep-from-file" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "dep-from-file" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "dep-from-file" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "dep-from-file" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "dep-from-file" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "dep-from-file" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "dep-from-file" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "dep-from-file" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "dep-from-file" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "dep-from-file" rollout to finish: 3 of 4 updated replicas are available...
deployment "dep-from-file" successfully rolled out

## 查看历史记录
[root@clientvm ~]# kubectl rollout history deployment -n mytest dep-from-file
deployment.apps/dep-from-file
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         kubectl set image deployment dep-from-file nginx=nginx:1.8.1 --namespace=mytest --record=true

## 查看历史记录详细信息
[root@clientvm ~]# kubectl rollout history deployment -n mytest  dep-from-file --revision=3


## 回滚版本
[root@clientvm ~]# kubectl rollout undo deployment dep-from-file -n mytest --to-revision=2
deployment.apps/dep-from-file rolled back

[root@clientvm ~]# kubectl rollout status deployment dep-from-file -n mytest
deployment "dep-from-file" successfully rolled out


##验证Pod nginx版本
[root@clientvm ~]# kubectl edit pod dep-from-file-7dfb96d9dd-5hghh -n mytest
......
spec:
  containers:
  - image: nginx:1.9.1
    imagePullPolicy: IfNotPresent
......

3.7 升级策略

[root@clientvm ~]# kubectl explain deployment.spec.strategy.rollingUpdate
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: rollingUpdate <Object>

DESCRIPTION:
     Rolling update config params. Present only if DeploymentStrategyType =
     RollingUpdate.

     Spec to control the desired behavior of rolling update.

FIELDS:
   maxSurge     <string>
     The maximum number of pods that can be scheduled above the desired number
     of pods. Value can be an absolute number (ex: 5) or a percentage of desired
     pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number
     is calculated from percentage by rounding up. Defaults to 25%. Example:
     when this is set to 30%, the new ReplicaSet can be scaled up immediately
     when the rolling update starts, such that the total number of old and new
     pods do not exceed 130% of desired pods. Once old pods have been killed,
     new ReplicaSet can be scaled up further, ensuring that total number of pods
     running at any time during the update is at most 130% of desired pods.

   maxUnavailable       <string>
     The maximum number of pods that can be unavailable during the update. Value
     can be an absolute number (ex: 5) or a percentage of desired pods (ex:
     10%). Absolute number is calculated from percentage by rounding down. This
     can not be 0 if MaxSurge is 0. Defaults to 25%. Example: when this is set
     to 30%, the old ReplicaSet can be scaled down to 70% of desired pods
     immediately when the rolling update starts. Once new pods are ready, old
     ReplicaSet can be scaled down further, followed by scaling up the new
     ReplicaSet, ensuring that the total number of pods available at all times
     during the update is at least 70% of desired pods.
  • MaxUnavailable:滚动过程中最多有多少个 Pod 不可用;
  • MaxSurge:滚动过程中最多存在多少个 Pod 超过预期 replicas 数量。

3.8 其他操作

[root@clientvm ~]# kubectl rollout -h
Manage the rollout of a resource.
Available Commands:
  history     View rollout history
  pause       Mark the provided resource as paused
  restart     Restart a resource
  resume      Resume a paused resource
  status      Show the status of the rollout
  undo        Undo a previous rollout

3.9 删除Deployment

[root@clientvm ~]# kubectl delete -f dep-from-file.yaml
deployment.apps "dep-from-file" deleted
[root@clientvm ~]# kubectl get deployments.apps -n mytest
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
testdep   2/2     2            2           129m