当前位置:   article > 正文

【Kubernetes实战】(十二)Pod 控制器 Deployment相关命令_根据deployment查指定标签的pod

根据deployment查指定标签的pod

目录

一、环境安装

二、Deployment介绍

三、Deployment使用

1 创建 Deployment

2 查看Deployment状态

3 查看副本信息

4 扩缩容

5 镜像更新

5.1 重建更新Recreate

5.2 滚动更新RollingUpdate

6 版本回退

7 金丝雀发布(灰度发布)

8 删除 Deployment 

四、Deployment和ReplicaSet的区别

1 ReplicaSet

2 Deployment


一、环境安装

参考

【Kubernetes实战】(四)MiniKube方式部署

【Kubernetes实战】(五)KubeAdm方式部署

【Kubernetes实战】(六)Kind方式部署

二、Deployment介绍

        对于kubernetes来说Pod是资源调度最小单元,kubernetes主要的功能就是管理多个Pod,Pod中可以包含一个或多个容器,而kubernetes是如可管理多个Pod的呢?对,没错,就是通过控制器,比如Deployment和ReplicaSet(rs)。

         kubernetes下有多个Deployment,Deployment下管理RepliceSet,通过RepliceSet管理多个Pod,通过Pod管理容器。

        它们之间的关系图如下:

 查看Deployment官方帮助

  1. kubectl explain deployment
  2. 或者
  3. kubectl explain deploy
  1. KIND: Deployment
  2. VERSION: apps/v1
  3. DESCRIPTION:
  4. Deployment enables declarative updates for Pods and ReplicaSets.
  5. FIELDS:
  6. apiVersion <string>
  7. APIVersion defines the versioned schema of this representation of an
  8. object. Servers should convert recognized schemas to the latest internal
  9. value, and may reject unrecognized values. More info:
  10. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
  11. kind <string>
  12. Kind is a string value representing the REST resource this object
  13. represents. Servers may infer this from the endpoint the client submits
  14. requests to. Cannot be updated. In CamelCase. More info:
  15. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
  16. metadata <Object>
  17. Standard object's metadata. More info:
  18. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
  19. spec <Object>
  20. Specification of the desired behavior of the Deployment.
  21. status <Object>
  22. Most recently observed status of the Deployment.

可以一级一级的查看,想查看什么只需要在后面加上  .字段名  即可。例如查看Deployment下的spec字段

kubectl explain deploy.spec
  1. KIND: Deployment
  2. VERSION: apps/v1
  3. RESOURCE: spec <Object>
  4. DESCRIPTION:
  5. Specification of the desired behavior of the Deployment.
  6. DeploymentSpec is the specification of the desired behavior of the
  7. Deployment.
  8. FIELDS:
  9. minReadySeconds <integer>
  10. Minimum number of seconds for which a newly created pod should be ready
  11. without any of its container crashing, for it to be considered available.
  12. Defaults to 0 (pod will be considered available as soon as it is ready)
  13. paused <boolean>
  14. Indicates that the deployment is paused.
  15. progressDeadlineSeconds <integer>
  16. The maximum time in seconds for a deployment to make progress before it is
  17. considered to be failed. The deployment controller will continue to process
  18. failed deployments and a condition with a ProgressDeadlineExceeded reason
  19. will be surfaced in the deployment status. Note that progress will not be
  20. estimated during the time a deployment is paused. Defaults to 600s.
  21. replicas <integer>
  22. Number of desired pods. This is a pointer to distinguish between explicit
  23. zero and not specified. Defaults to 1.
  24. revisionHistoryLimit <integer>
  25. The number of old ReplicaSets to retain to allow rollback. This is a
  26. pointer to distinguish between explicit zero and not specified. Defaults to
  27. 10.
  28. selector <Object> -required-
  29. Label selector for pods. Existing ReplicaSets whose pods are selected by
  30. this will be the ones affected by this deployment. It must match the pod
  31. template's labels.
  32. strategy <Object>
  33. The deployment strategy to use to replace existing pods with new ones.
  34. template <Object> -required-
  35. Template describes the pods that will be created.

三、Deployment使用

完整配置

  1. apiVersion: apps/v1 # 版本号
  2. kind: Deployment # 类型
  3. metadata: # 元数据
  4. name: # deploy 名称
  5. namespace: # 所属命名空间
  6. labels: #标签
  7. controller: deploy
  8. spec: # 详情描述
  9. replicas: 3 # 副本数量
  10. revisionHistoryLimit: 3 # 保留历史版本
  11. paused: false # 暂停部署,默认是 false
  12. progressDeadlineSeconds: 600 # 部署超时时间(s),默认是 600
  13. strategy: # 策略
  14. type: RollingUpdate # 滚动更新策略
  15. rollingUpdate: # 滚动更新
  16. maxSurge: 30% # 最大额外可以存在的副本数,可以为百分比,也可以为整数。默认为25%。
  17. maxUnavailable: 30% # 最大不可用状态的 Pod 的最大值,可以为百分比,也可以为整数。默认为25%。
  18. selector: # 选择器,通过它指定该控制器管理哪些 Pod
  19. matchLabels: # Labels 匹配规则
  20. app: nginx-pod
  21. matchExpressions: # Expressions匹配规则
  22. - {key: app, operator: In, values: [nginx-pod]}
  23. template: # 模板,当副本数量不足时,会根据下面的模板创建 Pod
  24. metadata:
  25. labels:
  26. app: nginx-pod
  27. spec:
  28. containers:
  29. - name: nginx
  30. image: nginx:1.17.1
  31. ports:
  32. - containerPort: 80

使用示例yml:

pc-deployment.yml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: pc-deployment
  5. namespace: dev
  6. spec:
  7. replicas: 3
  8. selector:
  9. matchLabels:
  10. app: nginx-pod
  11. template:
  12. metadata:
  13. labels:
  14. app: nginx-pod
  15. spec:
  16. containers:
  17. - name: nginx
  18. image: nginx:1.17.1

1 创建 Deployment

kubectl create -f pc-deployment.yml

2 查看Deployment状态

kubectl get deploy -n dev -o wide

3 查看副本信息

kubectl get rs -n dev -o wide

4 扩缩容

使用方式同rs

参考:

【Kubernetes实战】(十一)Pod 控制器 ReplicaSet相关命令

1)edit 方式

kubectl edit deploy pc-deployment -n dev

扩容ReplicaSet到5后

2)scale 方式

kubectl scale deploy pc-deployment --replicas=4 -n dev

5 镜像更新


Deployment支持两种更新策略:重建更新和滚动更新,可以通过strategy指定策略类型。

5.1 重建更新Recreate

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: pc-deployment
  5. namespace: dev
  6. spec:
  7. strategy: # 策略
  8. type: Recreate # 重建更新
  9. replicas: 3
  10. selector:
  11. matchLabels:
  12. app: nginx-pod
  13. template:
  14. metadata:
  15. labels:
  16. app: nginx-pod
  17. spec:
  18. containers:
  19. - name: nginx
  20. image: nginx:1.17.1

5.2 滚动更新RollingUpdate

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: pc-deployment
  5. namespace: dev
  6. spec:
  7. strategy: # 策略
  8. type: RollingUpdate # 滚动更新策略
  9. rollingUpdate:
  10. maxSurge: 25%
  11. maxUnavailable: 25%
  12. replicas: 3
  13. selector:
  14. matchLabels:
  15. app: nginx-pod
  16. template:
  17. metadata:
  18. labels:
  19. app: nginx-pod
  20. spec:
  21. containers:
  22. - name: nginx
  23. image: nginx:1.17.1

6 版本回退


kubectl rollout:版本升级相关功能,支持下面的选项:

status:显示当前升级状态
history:显示升级历史记录
pause:暂停版本升级过程
resume:继续已经暂停的版本升级过程
restart:重启版本升级过程
undo:回滚到上一级版本(可以使用--to-revision回滚到指定版本)

查看当前升级版本的状态

kubectl rollout status deploy pc-deployment -n dev

查看升级历史记录

kubectl rollout history deploy pc-deployment -n dev


版本回滚

kubectl rollout undo deployment pc-deployment --to-revision=1 -n dev

Deployment之所以可是实现版本的回滚,就是通过记录下历史rs来实现的,一旦想回滚到哪个版本,只需要将当前版本Pod数量降为0,然后将回滚版本的Pod提升为目标数量就可以了。

7 金丝雀发布(灰度发布)

Deployment控制器支持控制更新过程中的控制,如暂停(pause)或继续(resume)更新操作。

更新并暂停

kubectl set image deploy pc-deployment nginx=nginx:1.17.4 -n dev && kubectl rollout pause deployment pc-deployment -n dev

 

观察更新状态

kubectl rollout status deploy pc-deployment -n dev

监控更新的过程

kubectl get rs -n dev -o wide

继续更新
 

kubectl rollout resume deploy pc-deployment -n dev

8 删除 Deployment 

kubectl delete -f pc-deployment.yml

四、Deployment和ReplicaSet的区别

1 ReplicaSet


核心作用在于帮助用户创建指定数量的pod副本,并确保pod副本一直处于满足用户期望的数量,起到多退少补的作用,并且还具有自动扩容缩容等机制

主要由三个部分组成
用户期望的pod副本数:用来定义由这个控制器管控的pod副本有几个
标签选择器:当pod挂掉的时候,replicaset就通过标签选择器,选择指定标签的pod模板,再通过pod模板创建pod。
pod资源模板:定义一个Pod模板,且需要给pod模板设置标签。给标签选择器使用

2 Deployment


        ReplicaSet并不是我们直接使用的控制器,kubernetes建议我们使用Deployment
Deployment控制器是工作在ReplicaSet之上的,Deployment通过控制ReplicaSet来控制pod,并不是直接控制pod。

        Deployment有ReplicaSet的所有功能
        Deployment支持自动扩容缩容,滚动更新和回滚等机制,并提供了一个声明式的定义功能,这种声明式定义使得我们将来创建资源时可以基于声明的逻辑来定义,我们那些所有定义的资源可以随时重新进行声明,随时改变我们在apiserver上的定义的目标期望状态,只要那些资源支持动态运行时修改,都能修改是帮我们管理无状态的应用的最好的控制器。

       

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/162033
推荐阅读
相关标签
  

闽ICP备14008679号