K8S如何扩展副本集

Scaling ReplicaSets 扩展副本集

ReplicaSets are scaled up or down by updating the spec.replicas key on the ReplicaSet object stored in Kubernetes. When a ReplicaSet is scaled up, new Pods are submitted to the Kubernetes API using the Pod template defined on the ReplicaSet.

通过更新存储在 Kubernetes 中的 ReplicaSet 对象上的 spec.replicas 关键字,可以扩大或缩小 ReplicaSet 的规模。当扩大 ReplicaSet 时,新的 Pod 会使用 ReplicaSet 上定义的 Pod 模板提交给 Kubernetes API。

Imperative Scaling with kubectl scale 使用 kubectl scale 进行强制扩展

The easiest way to achieve this is using the scale command in kubectl. For example, to scale up to four replicas you could run:

最简单的方法是使用 kubectl 中的 scale 命令。例如,要扩展到四个副本,可以运行

kubectl scale replicasets kuard --replicas=4

While such imperative commands are useful for demonstrations and quick reactions to emergency situations (e.g., in response to a sudden increase in load), it is important to also update any text-file configurations to match the number of replicas that you set via the imperative scale command. The reason for this becomes obvious when you consider the following scenario:

虽然此类命令式命令对于演示和快速应对紧急情况(如负载突然增加)非常有用,但重要的是也要更新任何文本文件配置,使其与通过命令式 scale 命令设置的副本数量相匹配。考虑到以下情况,这样做的原因就显而易见了:

Alice is on call, when suddenly there is a large increase in load on the service she is managing. Alice uses the scale command to increase the number of servers responding to requests to 10, and the situation is resolved. However, Alice forgets to update the ReplicaSet configurations checked into source control. Several days later, Bob is preparing the weekly rollouts. Bob edits the ReplicaSet configurations stored in version control to use the new container image, but he doesn’t notice that the number of replicas in the file is currently 5, not the 10 that Alice set in response to the increased load. Bob proceeds with the rollout, which both updates the container image and reduces the number of replicas by half, causing an immediate overload or outage.

爱丽丝正在值班,突然她管理的服务负载大幅增加。Alice 使用 scale 命令将响应请求的服务器数量增加到 10 台,情况得到了解决。但是,Alice 忘记更新已检查到源控制中的 ReplicaSet 配置。几天后,Bob 正在准备每周发布。Bob 编辑了存储在版本控制中的 ReplicaSet 配置,以使用新的容器映像,但他没有注意到文件中的副本数量目前是 5 个,而不是 Alice 为应对负载增加而设置的 10 个。鲍勃继续推出,既更新了容器映像,又将副本数量减少了一半,导致立即过载或中断。

Hopefully, this illustrates the need to ensure that any imperative changes are immediately followed by a declarative change in source control. Indeed, if the need is not acute, we generally recommend only making declarative changes as described in the following section.

希望这能说明有必要确保在源代码控制中进行任何命令式修改后,立即进行声明式修改。事实上,如果需求并不迫切,我们一般建议只进行声明式修改,如下节所述。

Declaratively Scaling with kubectl apply 使用 kubectl apply 进行声明式扩展

In a declarative world, we make changes by editing the configuration file in version control and then applying those changes to our cluster. To scale the kuard ReplicaSet, edit the kuard-rs.yaml configuration file and set the replicas count to 3:

在声明式世界中,我们通过编辑版本控制中的配置文件进行更改,然后将这些更改应用到集群中。要扩展 kuard ReplicaSet,请编辑 kuard-rs.yaml 配置文件并将副本数设置为 3:

...
spec:replicas: 3
...

In a multiuser setting, you would like to have a documented code review of this change and eventually check the changes into version control. Either way, you can then use the kubectl apply command to submit the updated kuard ReplicaSet to the API server:

在多用户环境中,您希望对这一变更进行有记录的代码审查,并最终将变更检查到版本控制中。无论采用哪种方式,您都可以使用 kubectl apply 命令将更新后的 kuard ReplicaSet 提交到 API 服务器:

kubectl apply -f kuard-rs.yaml

Now that the updated kuard ReplicaSet is in place, the ReplicaSet controller will detect that the number of desired Pods has changed and that it needs to take action to realize that desired state. If you used the imperative scale command in the previous section, the ReplicaSet controller will destroy one Pod to get the number to three.

现在,更新后的 kuard ReplicaSet 已经就位,ReplicaSet 控制器将检测到所需 Pod 的数量已发生变化,它需要采取行动来实现所需的状态。如果您在上一节中使用了强制缩放命令,ReplicaSet 控制器将摧毁一个 Pod,使数量变为三个。

Otherwise, it will submit two new Pods to the Kubernetes API using the Pod template defined on the kuard ReplicaSet. Regardless, use the kubectl get pods command to list the running kuard Pods. You should see output like the following:

否则,它会使用 kuard ReplicaSet 上定义的 Pod 模板向 Kubernetes API 提交两个新 Pod。无论如何,使用 kubectl get pods 命令列出正在运行的 kuard Pod。你应该会看到类似下面的输出:

kubectl get pods

Autoscaling a ReplicaSet 自动扩展副本集

While there will be times when you want to have explicit control over the number of replicas in a ReplicaSet, often you simply want to have “enough” replicas. The definition varies depending on the needs of the containers in the ReplicaSet. For example, with a web server like NGINX, you may want to scale due to CPU usage. For an in-memory cache, you may want to scale with memory consumption. In some cases you may want to scale in response to custom application metrics. Kubernetes can handle all of these scenarios via Horizontal Pod Autoscaling (HPA).

虽然有时您希望对 ReplicaSet 中的副本数量进行明确控制,但通常您只希望拥有 "足够多 "的副本。这个定义根据 ReplicaSet 中容器的需求而有所不同。例如,对于像 NGINX 这样的网络服务器,您可能希望根据 CPU 使用情况进行扩展。对于内存缓存,可能希望根据内存消耗量进行扩展。在某些情况下,您可能希望根据自定义应用指标进行扩展。Kubernetes 可以通过水平 Pod 自动扩展(HPA)处理所有这些情况。

HPA requires the presence of the heapster Pod on your cluster. heapster keeps track of metrics and provides an API for consuming metrics that HPA uses when making scaling decisions. Most installations of Kubernetes include heapster by default. You can validate its presence by listing the Pods in the kube-system namespace:

HPA 需要在集群上安装 heapster Pod。heapster 会跟踪度量指标,并提供用于消费度量指标的 API,HPA 在做出扩展决策时会用到这些指标。大多数 Kubernetes 安装默认都包含 heapster。你可以通过列出 kube-system 命名空间中的 Pod 来验证它的存在:

kubectl get pods --namespace=kube-system

You should see a Pod named heapster somewhere in that list. If you do not see it, autoscaling will not work correctly.

你应该能在列表中看到一个名为 heapster 的 Pod。如果看不到,自动缩放将无法正常工作。

“Horizontal Pod Autoscaling” is kind of a mouthful, and you might wonder why it is not simply called “autoscaling.” Kubernetes makes a distinction between horizontal scaling, which involves creating additional replicas of a Pod, and vertical scaling, which involves increasing the resources required for a particular Pod (e.g., increasing the CPU required for the Pod). Vertical scaling is not currently implemented in Kubernetes, but it is planned. Additionally, many solutions also enable cluster autoscaling, where the number of machines in the cluster is scaled in response to resource needs, but this solution is not covered here.

"水平 Pod 自动扩展 "有点拗口,你可能会问,为什么不干脆叫 "自动扩展 "呢?Kubernetes 区分了水平扩展和垂直扩展,前者涉及为 Pod 创建额外的副本,后者涉及增加特定 Pod 所需的资源(例如,增加 Pod 所需的 CPU)。Kubernetes 目前尚未实现垂直扩展,但已在计划中。此外,许多解决方案还支持集群自动扩展,即根据资源需求扩展集群中的机器数量,但这里不涉及这一解决方案。

AUTOSCALING BASED ON CPU 基于 CPU 的自动扩展

Scaling based on CPU usage is the most common use case for Pod autoscaling. Generally it is most useful for request-based systems that consume CPU proportionally to the number of requests they are receiving, while using a relatively static amount of memory.

基于 CPU 使用情况进行扩展是 Pod 自动扩展最常见的使用案例。一般来说,它对基于请求的系统最有用,因为这些系统消耗的 CPU 与接收到的请求数量成比例,同时使用相对固定的内存量。

To scale a ReplicaSet, you can run a command like the following:

要缩放 ReplicaSet,可以运行类似下面的命令:

kubectl autoscale rs kuard --min=2 --max=5 --cpupercent=80

This command creates an autoscaler that scales between two and five replicas with a CPU threshold of 80%. To view, modify, or delete this resource you can use the standard kubectl commands and the horizontalpodautoscalers resource. horizontalpodautoscalers is quite a bit to type, but it can be shortened to hpa:

该命令创建了一个自动扩展器,可在 2 到 5 个副本之间扩展,CPU 阈值为 80%。要查看、修改或删除该资源,可以使用标准的 kubectl 命令和 horizontalpodautoscalers 资源。 horizontalpodautoscalers 的键入量很大,但可以简称为 hpa:

kubectl get hpa

Because of the decoupled nature of Kubernetes, there is no direct link between the HPA and the ReplicaSet. While this is great for modularity and composition, it also enables some anti-patterns. In particular, it’s a bad idea to combine both autoscaling and imperative or declarative management of the number of replicas. If both you and an autoscaler are attempting to modify the number of replicas, it’s highly likely that you will clash, resulting in unexpected behavior.

由于 Kubernetes 的解耦性质,HPA 和 ReplicaSet 之间没有直接联系。虽然这对模块化和组合非常有利,但也会带来一些反模式。尤其是,将自动伸缩和对副本数量的命令式或声明式管理结合起来不是一个好主意。如果你和自动伸缩器都试图修改副本数量,很可能会发生冲突,导致意想不到的行为。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/611980.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Web应用防火墙是什么?聊聊领先WAF解决方案

数字化进程的加速发展,Web站点及各类应用的数量呈现爆发式增长态势。与此同时,利用Web漏洞进行攻击的事件也与日俱增,黑客攻击手段不断升级,包括各种拟人化自动化攻击、API攻击以及0day攻击等,给Web应用安全防护带来了…

通过两台linux主机配置ssh实现互相免密登入

一 1.使用Xshell远程连接工工具生成公钥文件 2.生产密钥参数 3.生成公钥对 4.用户密钥信息 5.公钥注册 二 1.关闭服务端防火墙 ---systemctl stop firewalld 2.检查是否有/root/.ssh目录,没有则创建有则打开/root/.ssh/authorized_keys文件将密钥粘贴创建/ro…

数据结构之Radix和Trie

数据结构可视化演示链接,也就是视频中的网址 Radix树:压缩后的Trie树 Radix叫做基数树(压缩树),就是有相同前缀的字符串,其前缀可以作为一个公共的父节点。同时在具体存储上,Radix树的处理是以…

13.Kubernetes应用部署完整流程:从Dockerfile到Ingress发布完整流程

本文以一个简单的Go应用Demo来演示Kubernetes应用部署的完整流程 1、Dockerfile多阶段构建 Dockerfile多阶段构建 [root@docker github]# git clone https://gitee.com/yxydde/http-dump.git [root@docker github]# cd http-dump/ [root@docker http-dump]# cat Dockerfile …

C#高级语法 Attribute特性详解和类型,方法,变量附加特性讲解

文章目录 前言相关资料Attribute特性个人原理理解特性的声明与使用类型特性运行结果: 找到类的Attribute属性方法特性和变量特性代码封装测试类TestService1TestService2TestService3 测试代码运行结果 对封装的代码进行优化封装代码测试代码运行结果(和…

66、python - 代码仓库介绍

上一节,我们可以用自己手写的算法以及手动搭建的神经网络完成预测了,不知各位同学有没有自己尝试来预测一只猫或者一只狗,看看准确度如何? 本节应一位同学的建议,来介绍下 python 代码仓库的目录结构,以及每一部分是做什么? 我们这个小课的代码实战仓库链接为:cv_lea…

mysql中使用IN的注意事项

目录 前言注意事项 前言 在写sql语句过程中,难免会使用IN条件查询,那你知道使用IN要注意那些事项呢?下面我们就来一列举 注意事项 使用IN查询是否会使用索引 答:有时会使用,有时就不会使用。当IN 的范围小时会使用索引查询&…

MongDB集群部署和升级方案

******集群部署部分****** 主机信息mongodb集群 192.168.1.46 192.168.1.47 192.168.1.48 192.168.1.49 192.168.1.50 192.168.1.51 规划 sharding 1 192.168.1.46 192.168.1.47 192.168.1.48 端口 28111 sharding 2 192.168.1.49 192.168.1.50 192…

JavaScript与Swift的异同,python像vb6

其实很多主流的编程语言都是大同小异,魔改了一下罢了。 JavaScript与Swift一样,是动态语言类型,即不用指定变量类型,会根据赋值的内容动态的判断出它的类型。与Swift不同的是,JavaScript定义变量的时候,也不需要指定变…

【已解决】c语言为什么函数运算顺序从右往左

本博文主要源于笔者正在调试的c程序,c语言的函数运算顺序一直是从右往左int a 5;add(a,a);如果按照正常思维就是(5,5),运行结果确是(6,5),这是为什么呢 问题起源 对c语言的函数运算顺序产生怀疑 问题解释原因 函数从右到左依次入栈,出栈时可以很方便…

12.1SPI驱动框架

SPI硬件基础 总线拓扑结构 引脚含义 DO(MOSI):Master Output, Slave Input, SPI主控用来发出数据,SPI从设备用来接收数据 DI(MISO) :Master Input, Slave Output, SPI主控用来发出数据,SPI从设备用来接收…

SpringMVC配置文件上传解析器实现文件上传项目实例

SpringMVC配置文件上传解析器实现文件上传项目实例 1、在pom.xml文件中添加相关依赖 <!--文件上传--> <dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</versio…

threejs 光带扩散动画

目录 一、创建光带 (1) 设置光带顶点 (2) 设置光带顶点透明度属性 二、光带动画 完整代码 html文件代码 js文件代码 最后展示一下项目里的效果&#xff1a; 最近项目中要求做一段光带效果动画&#xff0c;尝试着写了一下&#xff0c;下面是本次分享光带扩散动画的效果预…

C++11之智能指针

C11之智能指针 前言1、智能指针概念2. 智能指针的定义和使用2.1 auto_ptr&#xff08;C11已经抛弃&#xff09;2.2 share_ptr2.3 unique_ptr2.4 weak_ptr 前言 C程序设计中&#xff0c;动态内存的管理式通过一对运算符来完成的&#xff1a;new和delete。 使用堆内存是非常频繁…

代码随想录算法训练营第一天|数组理论基础、704二分查找、27移除元素

数组理论基础 一维数组 数组中的元素在内存空间中是连续的数组名与数组中第一个元素的地址相同&#xff08;一维数组&#xff09;数组的下标从0开始删除数组的元素其实是用后面的元素覆盖掉要删除的元素数组的长度不能改变 二维数组 二维数组是按照行存储的&#xff0c;也是…

关于json.dumps()写入文件时是utf8

json.dumps()默认情况下&#xff0c;该函数会自动处理Unicode编码。 不要直接在json.dumps()设置encodingutf-8&#xff0c;会报错 json.dumps got an unexpected keyword argument encoding 需要将json.dumps()中设置ensure_asciiFalse&#xff0c;结合open函数中的encodin…

Java解析第三方接口返回的json

在实际开发过程中&#xff0c;免不了和其他公司进行联调&#xff0c;调用第三方接口&#xff0c;这个时候我们就需要根据对方返回的数据进行解析&#xff0c;获得我们想要的字段 第一种 //这种是data里面有个list的格式 {"data": {"username": "s…

linux 网络设备驱动之报文接收

从网络上接收报文比发送它要难一些, 因为必须分配一个 sk_buff 并从一个原子性上下 文中递交给上层. 网络驱动可以实现 2 种报文接收的模式: 中断驱动和查询. 大部分驱 动采用中断驱动技术, 这是我们首先要涉及的. 有些高带宽适配卡的驱动也可能采用查询 技术; 我们在"接收…

Kali Linux——aircrack-ng无线教程

目录 一、准备 二、案例 1、连接usb无线网卡 2、查看网卡信息 3、开启网卡监听 4、扫描wifi信号 5、抓取握手包 6、强制断开连接 7、破解握手包 三、预防 一、准备 1、usb无线网卡&#xff08;笔记本也是需要用到&#xff09; 2、密码字典&#xff08;Kali 系统自带…

java句柄数过多解决办法

java句柄数过多解决办法 使用file-leak-detector指定为应用程序的javaagent&#xff0c;然后重启程序&#xff0c;通过file-leak-detector提供能力&#xff0c;可以查看句柄和线程名称的信息&#xff0c;可直接排查出是哪行代码问题 包下载地址&#xff1a;http://search.mav…