当前位置: 首页 > news >正文

k8s之deployments相关操作

k8s之deployments相关操作

介绍

官网是这样说明如下:

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

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

deployment创建

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

[root@k8s-master deploy]# kubectl explain deploy
KIND:     Deployment
VERSION:  apps/v1DESCRIPTION:Deployment enables declarative updates for Pods and ReplicaSets.FIELDS:apiVersion   <string>APIVersion defines the versioned schema of this representation of anobject. Servers should convert recognized schemas to the latest internalvalue, and may reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourceskind <string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kindsmetadata     <Object>Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataspec <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: nginxspec:containers:- name: nginximage: nginx:1.14.2ports:- 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          76sNAME                                         DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-9456bbbf9   3         3         3       76sNAME                               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/v1RESOURCE: spec <Object>DESCRIPTION:Specification of the desired behavior of the Deployment.DeploymentSpec is the specification of the desired behavior of theDeployment.FIELDS:minReadySeconds	<integer>   //认定read状态以后,多久杀死旧的podMinimum number of seconds for which a newly created pod should be readywithout 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 isconsidered to be failed. The deployment controller will continue to processfailed deployments and a condition with a ProgressDeadlineExceeded reasonwill be surfaced in the deployment status. Note that progress will not beestimated during the time a deployment is paused. Defaults to 600s.replicas	<integer> //pod副本数量Number of desired pods. This is a pointer to distinguish between explicitzero and not specified. Defaults to 1.revisionHistoryLimit	<integer> //旧副本集保留的数量The number of old ReplicaSets to retain to allow rollback. This is apointer to distinguish between explicit zero and not specified. Defaults to10.selector	<Object> -required-Label selector for pods. Existing ReplicaSets whose pods are selected bythis will be the ones affected by this deployment. It must match the podtemplate'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/v1RESOURCE: 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 isRollingUpdate.

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

[root@k8s-master deploy]# kubectl explain deploy.spec.strategy.rollingUpdate
KIND:     Deployment
VERSION:  apps/v1RESOURCE: 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 numberof pods. Value can be an absolute number (ex: 5) or a percentage of desiredpods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute numberis calculated from percentage by rounding up. Defaults to 25%. Example:when this is set to 30%, the new ReplicaSet can be scaled up immediatelywhen the rolling update starts, such that the total number of old and newpods 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 podsrunning 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. Valuecan be an absolute number (ex: 5) or a percentage of desired pods (ex:10%). Absolute number is calculated from percentage by rounding down. Thiscan not be 0 if MaxSurge is 0. Defaults to 25%. Example: when this is setto 30%, the old ReplicaSet can be scaled down to 70% of desired podsimmediately when the rolling update starts. Once new pods are ready, oldReplicaSet can be scaled down further, followed by scaling up the newReplicaSet, ensuring that the total number of pods available at all timesduring 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: 300paused: false  ##暂停更新replicas: 7   ##副本数strategy:# type: Recreate  #不推荐rollingUpdate:maxSurge: 20%maxUnavailable: 2selector:     ## 选择器,会被 rs控制matchLabels: ##匹配标签app: nginx ##和模板template的pod标签一样template:metadata: ##pod的相关信息labels:app: nginxspec:containers:- name: nginximage: nginx:stable-alpine3.19-perlports:- containerPort: 80

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

相关文章:

  • 【图 - 遍历(BFS DFS)】深度优先搜索算法(Depth First Search), 广度优先搜索算法(Breadth First Search)
  • (佳作)两轮平衡小车(原理图、PCB、程序源码、BOM等)
  • 安装ps提示缺少dll文件是怎么回事,哪种解决方法更推荐
  • 【计算机视觉】数字图像处理基础:以像素为单位的图像基本运算(点运算、代数运算、逻辑运算、几何运算、插值)
  • Spring Boot整合WebSocket和Redis实现直播间在线人数统计功能
  • 检测五个数是否一样的算法
  • JavaScript html css 字符串对象
  • ChatGPT4写2024高考作文:在AI时代,人们对于问题的数量是否会减少?
  • 机器学习周记(第四十二周:AT-LSTM)2024.6.3~2024.6.9
  • vue3中的ref与reactive的区别
  • unity3d:GameFramework+xLua+Protobuf+lua-protobuf,生成.cs,.pb工具流
  • 自然语言处理:第三十三章FILCO:过滤内容的RAG
  • 【OpenHarmony】ArkTS 语法基础 ⑤ ( ArkTS 状态管理 | @State 装饰器定义状态数据 | 使用状态数据渲染组件 )
  • 程序员搞副业一些会用到的工具
  • SpringBoot+Vue图书管理系统(前后端分离)
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • 2019年如何成为全栈工程师?
  • 5、React组件事件详解
  • Cookie 在前端中的实践
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • Java面向对象及其三大特征
  • leetcode386. Lexicographical Numbers
  • Linux中的硬链接与软链接
  • Octave 入门
  • Python学习之路13-记分
  • Ruby 2.x 源代码分析:扩展 概述
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • vue-cli3搭建项目
  • 深度解析利用ES6进行Promise封装总结
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 问题之ssh中Host key verification failed的解决
  • 一份游戏开发学习路线
  • ​ArcGIS Pro 如何批量删除字段
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • # windows 安装 mysql 显示 no packages found 解决方法
  • # 详解 JS 中的事件循环、宏/微任务、Primise对象、定时器函数,以及其在工作中的应用和注意事项
  • #stm32驱动外设模块总结w5500模块
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (1)Android开发优化---------UI优化
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (七)理解angular中的module和injector,即依赖注入
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • (转)Windows2003安全设置/维护
  • ... 是什么 ?... 有什么用处?
  • .axf 转化 .bin文件 的方法
  • .Net core 6.0 升8.0
  • .Net IOC框架入门之一 Unity
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
  • .net 提取注释生成API文档 帮助文档
  • .NET版Word处理控件Aspose.words功能演示:在ASP.NET MVC中创建MS Word编辑器
  • .NET企业级应用架构设计系列之技术选型
  • ?php echo $logosrc[0];?,如何在一行中显示logo和标题?
  • @NestedConfigurationProperty 注解用法
  • [2016.7.test1] T2 偷天换日 [codevs 1163 访问艺术馆(类似)]
  • [20180129]bash显示path环境变量.txt