(K8s学习笔记八)Pod的扩缩容
1.手动扩容机制
示例:对busybox-deployment手动扩缩容
apiVersion:apps/v1
kind: Deployment
metadata:
name: busybox-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox:latest
# 此Pod已运行副本数量3个,使用kubectl scale命令扩容至5个
kubectl scale deploy busybox-deployment --replicas 5
注:如将replicas值设置小于当前副本数则系统会杀掉一些运行中的Pod已实现缩容
2.自动扩容机制(HPA)
Horizontal Pod Autoscaler(HPA)的控制器,用于实现基于CPU使用率进行自动Pod扩容的功能,HPA控制器基于Master的kube-controllere-manager服务启动参数--horizontal-pod-autoscaler-sync-period定义的探测周期(默认值为15s),周期性监测目标Pod的资源性能指标,并与HPA资源对象中的扩容条件进行对比,在满足条件时对Pod副本数量进行调整。

使用HPA功能需要在controller-manager启动文件中加入的参数:
- --horizontal-pod-autoscaler-tolerance=0.1,设置扩缩容忍度,默认值0.1(10%),表示基于算法得到的结果在0.9-1.1(-10%-10%),控制器都不会进行扩缩容
- --horizontal-pod-autoscaler-initial-readiness-delay=30s,设置首次探测Pod是否Ready的延时时间,默认值30min
- --horizontal-pod-autoscaler-cpuinitialization-period=10s,设置首次采集Pod的CPU使用率的延迟时间
- --horizontal-pod-autoscaler-downscale-stabilization=1m0s,这个配置可让系统更平滑的进行缩容操作,默认值5min
使用HorizontalPodAutoscaler配置自定义扩缩容的规则:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: nginx
spec:
scaleTargetRef: # 定义目标对象,可以是deploy/RC/RS
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 1 # pod副本数量的最下值
maxReplicas: 10 # pod副本数量的最大值
metrics: # 目标指标值,系统在指标数据达到目标值时出发扩缩容操作
- type: Resource # 定义目标值
resources:
name: cpu
target:
type: Utilization
averageUtilization: 50
注:目标值类型可包括三项
- Resource:基于资源的指标,可设置CPU和内存,对于CPU使用率可在target参数中设置averageUtilization定义目标平均CPU使用率;对于内存使用率可在target参数中设置AverageValue定义目标平均内存使用率
- Pods:基于pod的指标,系统对全部Pod副本的指标进行计算平均值,数据来源于Pod对象本身,其target类型只能使用AverageValue
- Object:基于某种资源对象(如Ingress)的指标或应用系统的任意自定义指标,数据来源于其它资源对象或任意自定义指标,其target类型可以使用Value和AverageValue(根据Pod副本数计算平均值)进行设置
示例一:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: nginx
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 1
maxReplicas: 10
metrics:
- type: Object
object:
metrics:
name: requests-per-second # 指标名称
describedObject:
apiVersion: extensions/v1beta1
kind: Ingress # 来源于ingress main-route
name: main-route
target:
type: Value
value: 2k # 目标值为2000,即在ingress的每秒请求数达到2000时触发扩缩容操作
示例二:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: nginx
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 1
maxReplicas: 10
metrics:
- type: Object
object:
metrics:
name: 'http_requests' # 指标名称
selector: 'verb=GET' # 资源对象具有的标签
target:
type: AverageValue
averageValue: 500 # 平均值到500触发扩缩容操作
示例三:系统针对每种类型的指标都计算Pod副本的目标数量,以最大值为准进行扩缩容操作
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: nginx
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resources:
name: cpu
targetAverageUtilization: 50
- type: Pods
pods:
metrics:
name: packets-per-second
targetAverageUtilization: 1k
- type: Object
object:
metrics:
name: requests-per-second
describedObject:
apiVersion: extensions/v1beta1
kind: Ingress
name: main-route
target:
kind: Value
value: 1k
示例四:使用外部服务的性能指标对自己部署的K8s中的服务进行HPA
.......
metrics:
- type: External
external:
metrics:
name: queue-message-ready
selector: 'queue=worker_tasks'
targetAverageUtilization: 30
基于外部服务的性能指标实现HPA需要预先部署自定义Metries Server,目前可以基于Prometheus、MS Azure、Datadog Cluster和Google Stackdriver等系统的Adapter实现

使用外部性能指标,在K8s master的API Server启动Aggregation层,需要在apierver启动文件中加入的参数:
- --requestheader-client-ca-file,指定客户端的CA证书
- --requestheader-allowed-names,允许访问的客户端common name列表,将其设置为空置时,表示任意客户端都可以访问
- --requestheader-extra-headers-prefix=X-Remote-Extra,请求头中需要检查的前缀名
- --requestheader-group-headers=X-Remote-Group,请求头中需要检查的组名
- --requestheader-username-headers=X-Remote-User,请求头中需要检查的用户名
- --proxy-client-cert-file,在请求期间验证Aggregator的客户端CA证书
- --proxy-client-key-file,在请求期间验证Aggreagator的客户端私钥
使用外部性能指标,在K8s master的API Server启动Aggregation层,需要在controller-manager启动文件中加入的参数:
--horizontal-pod-autoscaler-sync-period=10s,HPA控制器同步Pod副本数量的时间间隔,默认值15s
示例五、使用Prometheus作为外部性能指标收集器
# 部署Prometheus Operator
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: prometheus-operator
name: prometheus-operator
spec:
replicas: 1
selector:
matchLabels:
k8s-app: prometheus-operator
template:
metadata:
labels:
k8s-app: prometheus-operator
spec:
containers:
- image: quay.io/coreos/prometheus-operator:v0.40.0
imagePullPolicy: IfNotPresent
name: prometheus-operator
ports:
- containerPort: 8080
name: http
resources:
limits:
cpu: 200M
memory: 100Mi
requests:
cpu: 100m
memory: 50Mi
这个prometheus-operatord会自动创建名为monitoring.coreos.com的CRD资源 # 通过Operator的配置部署Prometheus
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
labels:
app: promethus
prometheus: prometheus
spec:
replicas: 2
baseImage: quay.io/prometheus/prometheus
version: v2.10.0
serviceMonitorSelector:
matchLabels:
service-monitor: function
resources:
requests:
memory: 300Mi ---
apiVersion: v1
kind: Service
metadata:
name: prometheus
labels:
app: prometheus
prometheus: prometheus
spec:
selector:
prometheus: prometheus
ports:
- name: http
port: 9090 # 确认prometheus operator和prometheus服务正常
kubectl get pods
d
(K8s学习笔记八)Pod的扩缩容的更多相关文章
- Kubernetes 笔记 11 Pod 扩容与缩容 双十一前后的忙碌
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. Hi,大家好, ...
- k8s学习笔记之六:Pod控制器(kube-controller-manager)
第一章.什么是kube-controller-manager? Controller Manager 由 kube-controller-manager 和 cloud-controller-mana ...
- k8s学习笔记之五:Pod资源清单spec字段常用字段及含义
第一章.前言 在上一篇博客中,我们大致简述了一般情况下资源清单的格式,以及如何获得清单配置的命令帮助,下面我们再讲解下清单中spec字段中比较常见的字段及其含义 第二章.常用字段讲解 spec.con ...
- K8S学习笔记之Pod的Volume emptyDir和hostPath
0x00 Volume的类型 Volume是Kubernetes Pod中多个容器访问的共享目录. Volume被定义在Pod上,被这个Pod里的多个容器挂在到相同或不同的路径下. Volume的生命 ...
- 三十三、HPA实现自动扩缩容
通过HPA实现业务应用的动态扩缩容 HPA控制器介绍 当系统资源过高的时候,我们可以使用如下命令来实现 Pod 的扩缩容功能 $ kubectl -n luffy scale deployment m ...
- k8s 学习笔记
常用的kubectl命令 kubectl run kubia --image=luksa/kubia --port=8080 --generator=run/v1 --image 指定镜像 - ...
- 【K8s学习笔记】K8s是如何部署应用的?
本文内容 本文致力于介绍K8s一些基础概念与串联部署应用的主体流程,使用Minikube实操 基础架构概念回顾 温故而知新,上一节[K8S学习笔记]初识K8S 及架构组件 我们学习了K8s的发展历史. ...
- Redis学习笔记八:集群模式
作者:Grey 原文地址:Redis学习笔记八:集群模式 前面提到的Redis学习笔记七:主从复制和哨兵只能解决Redis的单点压力大和单点故障问题,接下来要讲的Redis Cluster模式,主要是 ...
- Learning ROS forRobotics Programming Second Edition学习笔记(八)indigo rviz gazebo
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
- python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑
python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件?当我们点开下载页时, 一般 ...
随机推荐
- 记录一个cpu彪高的BUG处理--jvm调优
业务场景:游戏行业,N个服务器,要进行大批量的合服处理,玩家数据会上升,从新整理和服务器的分配情况和逻辑处理,正常开发后,当天白天正常,然后晚上高峰期开始玩家频繁反馈无法登录~~~ 处理逻辑: 优先确 ...
- LeetCode-1606 找到处理请求最多的服务器
来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests ...
- C++ cannot bind non-const lvalue reference of type ‘Dog&’ to an rvalue of type ‘Dog’
void function(Dog & d){ /************** } 调用这个函数,如果传参一个右值对象,临时对象,则会出现这个问题 一个临时对象的引用,这怎么想都不合理 从该函 ...
- rn用Modal实现Drawer
PS:本文仅说明Modal可以用来做Drawer,并不介绍Modal的用法. 今天在开发的时候,想要使用Drawer. RN原生不自带Drawer,react-native-drawer又有bug(没 ...
- vue学习笔记:vue.js基础语法
一.VUE 概述 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅 ...
- elasticsearch 索引数据手动复制注意事项
一.背景 有一个已经在A机器建立的100+G的es索引数据文件,需要将这份数据文件直接复制到B机器的elasticsearch中 B机器的节点是在一个集群中,有多个数据节点. 没有原始数据,不重新构建 ...
- ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061) 报错原因:电脑之前有个5.0.2版本的mys ...
- Windows 下安装 Bun:像 Node 或 Deno 一样的现代 JavaScript 运行时
背景 最近前端工具链又火了一个项目 Bun,可以说内卷非常严重.Bun 是一个新的 JavaScript 运行时,内置了打包器.转译器.任务运行器和 npm 客户端. Bun 是像 Node 或 De ...
- 四大组件之广播接收者BroadcastReceiver
参考:Android开发基础之广播接收者BroadcastReceiver 什么是广播接收者? 我们小时候都知道,听广播,收听广播!什么是收听广播呢?打开收音机,调频就可以收到对应的广播节目了.其实我 ...
- 清理Linux系统无效的或者损坏的包
参考:解决Linux的 [有1 个软件包没有被完全安装或卸载] 问题 ubuntu中卸载没有安装完全的软件包 Ubuntu安装.基本命令和常见故障处理 1. 1 apt-get insta ...