(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第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件?当我们点开下载页时, 一般 ...
随机推荐
- Java Agent场景性能测试分析优化经验分享
摘要:本文将以Sermant的SpringBoot 注册插件的性能测试及优化过程为例,分享在Java Agent场景如何进行更好的性能测试优化及在Java Agent下需要着重注意的性能陷阱. 作者: ...
- 安装ElasticSearch7.6.2使用自带JDK
平时用jdk8,但运行es7无法启动.在elasticsearch7以上的版本中会自带jdk.需要修改elasticsearch-env配置文件,就可以使用自带jdk版本,不影响其他java项目. w ...
- 微信小程序(开发某些方式)
1.开发工具:微信小程序开发工具(需要appid登录)2.调试:可使用微信开发者工具预览(用真机测试)3.真机调试:微信开发者工具真机调试(可打印以及查看网络等)4.扫一扫功能: 1.小程序里面可 ...
- NSIS KillProcDLL插件 扩展使用
客户端插件KillProcDLL ,用于结束进程. 官网文档:https://nsis.sourceforge.io/KillProcDLL_plug-in 使用场景 卸载程序时,结束正在运行的应用程 ...
- DRF过滤Filtering
过滤Filtering 对于列表数据可能需要根据字段进行过滤,我们可以通过添加django-filter扩展来增强支持. pip install django-filter 在配置文件中增加过滤后端的 ...
- python服务返回text与json
json.dumps(),loads()和jsonify()的区别 使用方法不同: dumps和loads方法,来自json模块,而json模块是python中的,可以直接导入: import jso ...
- ubuntu安装nvidia-docker2
1.配置源: distribution=$(. /etc/os-release;echo $ID$VERSION_ID) && curl -s -L https://nvidia.gi ...
- SAP HANA : CDS
6.SAP HANA CDS 使用SAP HANA Core Data Services(CDS)在SAP HANA Extended Application Services中构建设计时数据持久性模 ...
- DEV 导出多行头
//在winfrom 中添加个Gridcontrol 和按钮 public partial class MultTitle : XtraForm { BandedGridView bandedGrid ...
- CF1268B题解
CF1268B 题解 题目翻译 给你一个杨表,用一个有 \(n\) 个元素的数组 \(a\) 表示杨表每一列的高度.你需要用 \(1 \times 2\) 或 \(2 \times 1\) 的骨牌填充 ...