控制Pod,使Pod拥有多副本,自愈,扩缩容等能力

个人理解:
Deployment:部署,一次部署会涉及多个Pod(应用)。

1.命令方式

  1. 创建一个部署

    1. kubectl create deployment mytomcat --image=tomcat:8.5.68

    image.png

  2. 自愈能力测试

image.png

  1. 删除一个部署

    1. # kubectl delete deploy mytomcat

    image.png

  2. 查看部署信息

    1. # kubectl get deploy

    image.png

  3. 多副本部署

    1. # kubectl create deployment my-dep --image=nginx --replicas=3

    image.png

    2.Yaml文件方式

  4. 多副本部署

    1. #vi my-dep.yaml
    2. apiVersion: apps/v1
    3. kind: Deployment
    4. metadata:
    5. labels:
    6. app: my-dep
    7. name: my-dep
    8. spec:
    9. replicas: 3
    10. selector:
    11. matchLabels:
    12. app: my-dep
    13. template:
    14. metadata:
    15. labels:
    16. app: my-dep
    17. spec:
    18. containers:
    19. - image: nginx
    20. name: nginx

    image.png

  1. xxx

3.自愈以及故障转移

自愈:当一个pod挂了,会自动拉起
故障转移:当一个Node挂了,转移所有的Pod
K8s对象-Deployment - 图7

3.1自愈能力测试

进入node02,手动docker stop 镜像

  1. 手动docker stop

image.png

  1. 查看pod

image.png

3.2故障转移能力测试

将Node2关机

  1. 关机前Pod

image.png

  1. 关机后(5分钟左右)

image.png


  1. 4.扩缩容

    当一个应用需要再多/少部署几个副本时
    K8s对象-Deployment - 图12

    4.1命令行

    1. kubectl scale --replicas=5 deployment/my-dep
    image.png4.2编辑yaml ```yaml kubectl edit deployment my-dep

修改 replicas

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1609516/1634721862242-5e567f23-d6a5-4fe1-99d2-18acf0537825.png#clientId=ub13c4d7d-7995-4&from=paste&height=401&id=u1d60853d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=802&originWidth=1052&originalType=binary&ratio=1&size=237880&status=done&style=none&taskId=u6de90f8f-327c-47fa-88e8-c4b9055c22a&width=526)
  2. <a name="jkO6P"></a>
  3. # 5.滚动更新
  4. 新版本的上线是替换老版本的。不是一次性全杀死。<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1609516/1634719589156-c11cd6b6-c7db-45ce-9c1d-81d6d396820c.png#clientId=u0cb12fcb-5baf-4&from=paste&height=429&id=ucf057c11&margin=%5Bobject%20Object%5D&name=image.png&originHeight=858&originWidth=1781&originalType=binary&ratio=1&size=223648&status=done&style=none&taskId=ud2518e6b-3bd7-4304-8a4f-1cade5c77a8&width=890.5)
  5. <a name="LKuG7"></a>
  6. ## 5.1命令行
  7. ```yaml
  8. kubectl set image deployment/my-dep nginx=nginx:1.16.1 --record
  9. #查询更新状态
  10. kubectl rollout status deployment/my-dep

image.png
image.png

5.2yaml文件

  1. # 修改 kubectl edit deployment/my-dep

image.png

6.版本回退

  1. #查看历史版本
  2. kubectl rollout history deployment/my-dep

image.png

  1. #查看某个历史详情
  2. kubectl rollout history deployment/my-dep --revision=2

image.png

  1. #回滚(回到上次)
  2. kubectl rollout undo deployment/my-dep

image.png

  1. #回滚(回到指定版本)
  2. kubectl rollout undo deployment/my-dep --to-revision=2

image.png