k8s之deployments相关操作

k8s之deployments相关操作

介绍

官网是这样说明如下:

一个 Deployment 为 PodReplicaSet 提供声明式的更新能力。

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

deployment创建

使用 kubectl explain deploy 解释一下 deployment,如下所示

[root@k8s-master deploy]# kubectl explain deploy
KIND:     Deployment
VERSION:  apps/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     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

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

   status       <Object>
     Most recently observed status of the Deployment.

官网给的示例文件如下:其中创建了一个 ReplicaSet,负责启动三个 nginx Pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment  ##deploy名称
  labels:
    app: nginx   ##标签
spec:     ##期望状态
  replicas: 3   ##副本数
  selector:     ## 选择器,会被 rs控制
    matchLabels: ##匹配标签
      app: nginx ##和模板template的pod标签一样
  template:
    metadata: ##pod的相关信息
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

将官网给的示例创建一个yaml文件运行,然后使用 kubectl get pod,rs,deployment 查看效果如下所示

[root@k8s-master deploy]# kubectl get pod,rs,deployment
NAME                                   READY   STATUS    RESTARTS   AGE
pod/nginx-deployment-9456bbbf9-k4r99   1/1     Running   0          76s
pod/nginx-deployment-9456bbbf9-s55cl   1/1     Running   0          76s
pod/nginx-deployment-9456bbbf9-tscr6   1/1     Running   0          76s

NAME                                         DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-9456bbbf9   3         3         3       76s

NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deployment   3/3     3            3           76s

可以看到一个deploy 最终产生三个资源。其中 rs控制者pod副本数量,deploy控制rs

deployment更新

上面的部署nginx的版本使用的是nginx:1.14.2,现在想要使用最新的版本,只需要编辑yaml中信息即可

在这里插入图片描述

然后 使用 kubectl get pod,rs,deployment 查看

在这里插入图片描述

可以查看到它并不是把所有的pod都杀掉,而是杀掉一个,然后在启动一个,这个就是滚动更新

可以看到一次升级会产生一个rs,最终也是通过rs 来进行回滚

在这里插入图片描述

最终都升级好以后可以看到最新的rs状态

在这里插入图片描述

使用 kubectl rollout history deployment.apps/nginx-deployment查看deploy历史

在这里插入图片描述

可以看到有两次变动,如果想要回到上一个版本,可以使用 kubectl rollout undo deployment.apps/nginx-deployment --to-revision=1 进行回滚

在这里插入图片描述

比例缩放

使用 kubectl explain deploy.spec 解释一下 spec中的字段

[root@k8s-master deploy]# kubectl explain deploy.spec
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: spec <Object>

DESCRIPTION:
     Specification of the desired behavior of the Deployment.

     DeploymentSpec is the specification of the desired behavior of the
     Deployment.

FIELDS:
   minReadySeconds	<integer>   //认定read状态以后,多久杀死旧的pod
     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> //pod副本数量
     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	<Object> -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	<Object>  //新pod 替换的策略
     The deployment strategy to use to replace existing pods with new ones.

   template	<Object> -required-
     Template describes the pods that will be created.

所以 比例缩放也就是围绕 strategy 字段来展开的

使用 kubectl explain deploy.spec.strategy 解释一下 strategy

[root@k8s-master deploy]# kubectl explain deploy.spec.strategy
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: strategy <Object>

DESCRIPTION:
     The deployment strategy to use to replace existing pods with new ones.

     DeploymentStrategy describes how to replace existing pods with new ones.

FIELDS:
   rollingUpdate	<Object>
     Rolling update config params. Present only if DeploymentStrategyType =
     RollingUpdate.

   type	<string>
     Type of deployment. Can be "Recreate" or "RollingUpdate". Default is
     RollingUpdate.

然后在使用 kubectl explain deploy.spec.strategy.rollingUpdate 解释一下rollingUpdate

[root@k8s-master deploy]# kubectl explain deploy.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>   //一次最多创建几个 pod, 可以是百分比也可以是数字
     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.

最终示例如下

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment  ##deploy名称
  labels:
    app: nginx   ##标签
spec:     ##期望状态
  revisionHistoryLimit: 10 ##保留最近的副本数量
  progressDeadlineSeconds: 300
  paused: false  ##暂停更新
  replicas: 7   ##副本数
  strategy:
    # type: Recreate  #不推荐
    rollingUpdate:
      maxSurge: 20%
      maxUnavailable: 2
  selector:     ## 选择器,会被 rs控制
    matchLabels: ##匹配标签
      app: nginx ##和模板template的pod标签一样
  template:
    metadata: ##pod的相关信息
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:stable-alpine3.19-perl
        ports:
        - containerPort: 80

然后运行 kubectl get pod,rs,deploy 进行观察

  • 18
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

让美好继续发生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值