Deployment

一个 Deployment 为Pod和ReplicaSet提供声明式的更新能力。

负责描述 Deployment 中的目标状态,而 Deployment控制器(Controller)以受控速率更改实际状态, 使其变为期望状态。可以定义 Deployment 以创建新的 ReplicaSet,或删除现有 Deployment, 并通过新的 Deployment 适配其资源。一个Deployment可以有多个Replicaset。

一、Deployment原理

Deployment使用声明式定义,直接在命令行通过纯命令的方式完成对应资源版本的内容的修改,也就是通过打补丁的方式进行修改;Deployment能提供滚动式自定义自控制的更新;对Deployment来讲,实现更新是还可以通实现控制更新节奏和更新逻辑。

二、Deployment资源清单文件编写

[root@k8s-master01 ~]# kubectl explain Deployment
GROUP:      apps
KIND:       Deployment
VERSION:    v1

DESCRIPTION:
    Deployment enables declarative updates for Pods and ReplicaSets.
    
FIELDS:
  apiVersion	<string>
    APIVersion defines the versioned schema of this representation of an object.
    Servers should convert recognized schemas to the latest internal value, and
    may reject unrecognized values. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

  kind	<string>
    Kind is a string value representing the REST resource this object
    represents. Servers may infer this from the endpoint the client submits
    requests to. Cannot be updated. In CamelCase. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

  metadata	<ObjectMeta>
    Standard object's metadata. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

  spec	<DeploymentSpec>
    Specification of the desired behavior of the Deployment.

  status	<DeploymentStatus>
    Most recently observed status of the Deployment.
[root@k8s-master01 ~]# kubectl explain Deployment.spec
GROUP:      apps
KIND:       Deployment
VERSION:    v1

FIELD: spec <DeploymentSpec>

DESCRIPTION:
    Specification of the desired behavior of the Deployment.
    DeploymentSpec is the specification of the desired behavior of the
    Deployment.
    
FIELDS:
  minReadySeconds	<integer>
    Minimum number of seconds for which a newly created pod should be ready
    without any of its container crashing, for it to be considered available.
    Defaults to 0 (pod will be considered available as soon as it is ready)

  paused	<boolean>
    Indicates that the deployment is paused.

  progressDeadlineSeconds	<integer>
    The maximum time in seconds for a deployment to make progress before it is
    considered to be failed. The deployment controller will continue to process
    failed deployments and a condition with a ProgressDeadlineExceeded reason
    will be surfaced in the deployment status. Note that progress will not be
    estimated during the time a deployment is paused. Defaults to 600s.

  replicas	<integer>
    Number of desired pods. This is a pointer to distinguish between explicit
    zero and not specified. Defaults to 1.

  revisionHistoryLimit	<integer>
    The number of old ReplicaSets to retain to allow rollback. This is a pointer
    to distinguish between explicit zero and not specified. Defaults to 10.

  selector	<LabelSelector> -required-
    Label selector for pods. Existing ReplicaSets whose pods are selected by
    this will be the ones affected by this deployment. It must match the pod
    template's labels.

  strategy	<DeploymentStrategy>
    The deployment strategy to use to replace existing pods with new ones.

  template	<PodTemplateSpec> -required-
    Template describes the pods that will be created. The only allowed
    template.spec.restartPolicy value is "Always".

Deployment.spec字段解析

minReadySeconds 是一个 Deployment 或 StatefulSet 的配置选项,用于定义新 Pod 在被认为是可以替换老的pod(在滚动更新的情况下)或计入服务的健康副本数量之前,必须保持 Ready 状态的最短时间(以秒为单位)。在执行滚动更新时,Kubernetes 会等待每个新启动的 Pod 达到 minReadySeconds 指定的时间并且处于 Ready 状态后,才会继续更新下一个 Pod,从而保证服务的连续性和稳定性。

可以通过设置 Deployment 的 spec.paused 字段为 true 来暂停部署的滚动更新或其他更改操作。这样做可以让您在不影响现有副本集的情况下,安全地检查或修改 Deployment 配置。暂停期间,Deployment 不会创建新版本的 Pod,也不会删除或更新现有的 Pod。要恢复更新,只需将 spec.paused 设置回 false

  maxSurge	<IntOrString>
    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	<IntOrString>
    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.

Deployment.spec.strategy.rollingUpdate.maxSurge,默认值为25%,更新过程中允许超出指定副本数的最多个数,取值方式可采用绝对值或百分比(百分比向上取整)。这个配置以期望的副本数为基准用于对新副本集新增pod。

Deployment.spec.strategy.rollingUpdate.maxUnavailable,默认值为25%,取值方式可采用绝对值或百分比(百分比向下取整)最大允许pods不可用数。这个配置以期望的副本数为基准用于对老副本集减少pod。

注意:maxSurge和maxUnavailable不能同时为0。

[!important] Deployment滚动更新先删pod在更新和先更新再删pod问题
默认策略是,maxSurge: 25% maxUnavaileble: 25%,都是k8s默认值。有的白天上线不影响业务设置maxUnavailable为0。

课程Deployment案例

在这里插入图片描述

三、Deployment缩扩容

1、直接改yaml文件,kubectl apply -f xxx.xml,会立即生效。

2、kubectl edit deployment xxx,修改后保存退出,会立即生效。

3、缩扩容命令

kubectl scale deployment/nginx-deployment --replicas=10

四、Deployment滚动更新

1、直接改yaml文件,kubectl apply -f xxx.xml,会立即生效。

2、kubectl edit deployment xxx,修改后保存退出,会立即生效。

3、更新镜像命令

kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1

查看上线(rollout)状态

kubectl rollout status deployment/nginx-deployment

实时查看Pod状态

kubectl get pod -w

查看deployment更多信息

kubectl describe deployment/xxxx

禁用更新(用于修改配置时不触发自动更新)

kubectl rollout pause deployment/nginx-deployment

恢复更新

kubectl rollout resume deployment/nginx-deployment

五、Deployment回滚

查看Deployment的历史版本

kubectl rollout history deployment/nginx-deployment

查看某个版本详细信息

kubectl rollout history deployment/nginx-deployment --revision=2

回滚到上一个版本

kubectl rollout undo deployment/nginx-deployment

回滚到指定版本

kubectl rollout undo deployment/nginx-deployment --to-revision=2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值