根据Nginx Ingress指标对指定后端进行HPA
本文分享自华为云社区《根据Nginx Ingress指标对指定后端进行HPA》,作者: 可以交个朋友。
背景
生产场景下,Nginx Ingress的流量会通过域名和path路径最终转发至不同的应用,而有时候cpu和内存并不是nginx的性能瓶颈,此时可以基于nginx_ingress_controller_requests指标,为其对应的应用配置HPA,以实现基于不同域名和path的请求量弹性指定后端工作负载
简介
环境准备
- nginx-ingress已部署
- 云原生监控插件kube-prometheus-stack已安装(server模式),插件默认监控nginx-ingress,开源环境请自行配置监控。
- 已配置kubectl命令或使用cloudshell
注意:由于HPA规则中scaleTargetRef和describedObject两个字段都无法指定命名空间,所以指标来源、HPA和弹性目标需在同一命名空间,而nginx-ingress和业务工作负载一般处在不同命名空间;本次方案采用external类型的HPA,可以忽略指标来源的命名空间
操作步骤
创建演示需要的弹性目标工作负载,service以及ingress
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-hpa
labels:
app: test-app
spec:
replicas: 1
selector:
matchLabels:
app: test-app
template:
metadata:
labels:
app: test-app
spec:
containers:
- image: skto/sample-app:v2
name: metrics-provider
ports:
- name: http
containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: test-app
namespace: default
labels:
app: test-app
spec:
ports:
- port: 8080
name: http
protocol: TCP
targetPort: 8080
selector:
app: test-app
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-app
labels:
app: sample-app
spec:
replicas: 1
selector:
matchLabels:
app: sample-app
template:
metadata:
labels:
app: sample-app
spec:
containers:
- image: skto/sample-app:v2
name: metrics-provider
ports:
- name: http
containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: sample-app
namespace: default
labels:
app: sample-app
spec:
ports:
- port: 80
name: http
protocol: TCP
targetPort: 8080
selector:
app: sample-app
type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
namespace: default
spec:
ingressClassName: nginx
rules:
- host: test.example.com
http:
paths:
- backend:
service:
name: sample-app
port:
number: 80
path: /
pathType: ImplementationSpecific
- backend:
service:
name: test-app
port:
number: 8080
path: /home
pathType: ImplementationSpecific
分别查询test.example.com/和test.example.com/home的nginx_ingress_controller_requests指标,指标正常
创建external类型的apiservices资源;创建后apiservices的状态为false是正常现象,添加externalRules后状态变为true
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
name: v1beta1.external.metrics.k8s.io
spec:
group: external.metrics.k8s.io
groupPriorityMinimum: 100
insecureSkipTLSVerify: true
service: #指定prometheus-adapter对应的service,华为CCE插件中adapter名称为custom-metrics-apiserver
name: custom-metrics-apiserver
namespace: monitoring
port: 443
version: v1beta1
versionPriority: 100
将externalRules规则添加到adapter的configmap中,修改后需要重启prometheus-adapter服务
kubectl -n monitoring edit configmap user-adapter-config
externalRules:
- metricsQuery: sum(rate(<<.Series>>{<<.LabelMatchers>>}[2m])) by (<<.GroupBy>>)
name:
as: ${1}_per_second
matches: ^(.*)
resources:
namespaced: false #忽略指标来源的命名空间,该配置不适用rules规则
seriesQuery: nginx_ingress_controller_requests
seriesQuery:原始指标;可以直接写指标名称,也可以使用{labelKey=labelValue}的方式筛选出原始指标
metricsQuery:用PromQL对指标进行筛选汇聚;.Series表示原始指标,.LabelMatchers表示对指标进行标签筛选,hpa中可以配置具体的筛选规则
name:将指标重命名
resources:hpa查询指标时通过api的方式调用,调用路径为:
而resources的作用就是将指标中命名空间标签的值替换路径中的${namespace},而我们本次方案需要忽略指标来源命名空间。
custom-metrics-apiserver服务重启后需要等待1分钟左右,执行命令查看指标是否正常
kubectl get --raw /apis/external.metrics.k8s.io/v1beta1/namespaces/*/nginx_ingress_controller_requests_per_second
创建HPA规则
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: sample-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: sample-app
minReplicas: 1
maxReplicas: 10
metrics:
- type: External
external:
metric:
name: nginx_ingress_controller_requests_per_second
selector:
matchLabels: #可以通过该字段对指标进行过滤,这里标签筛选条件会加入到externalRules的<<.LabelMatchers>>中。
exported_service: sample-app #筛选后端服务为sample-app的请求
host: test.example.com #筛选访问域名为test.example.com的请求
target:
type: AverageValue #External指标类型下只支持Value和AverageValue类型的目标值。
averageValue: 30
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: test-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: test-app
minReplicas: 1
maxReplicas: 10
metrics:
- type: External
external:
metric:
name: nginx_ingress_controller_requests_per_second
selector:
matchLabels:
exported_service: test-app
host: test.example.com
target:
type: AverageValue
averageValue: 30
弹性演示
使用命令压测sample-app对应的访问域名和路径,正常触发弹性;请自行配置域名与ELB地址的映射
ab -c 50 -n 5000 http://test.example.com/
用同样的方式压测test-app,正常触发弹性
ab -c 50 -n 5000 http://test.example.com/home
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
实践展示openEuler部署Kubernetes 1.29.4版本集群
本文分享自华为云社区《openEuler部署Kubernetes 1.29.4版本集群》,作者:江晚正愁余。 一、Kubernetes集群节点准备 1.1 主机操作系统说明 序号 操作系统及版本 备注 1 CentOS7u9或 OpenEuler2203 1.2 主机硬件配置说明 需求 CPU 内存 硬盘 角色 主机名 值 8C 8G 1024GB master k8s-master01 值 8C 16G 1024GB worker(node) k8s-worker01 值 8C 16G 1024GB worker(node) k8s-worker02 1.3 主机配置 1.3.1 主机名配置 由于本次使用3台主机完成kubernetes集群部署,其中1台为master节点,名称为k8s-master01;其中2台为worker节点,名称分别为:k8s-worker01及k8s-worker02 # master节点 hostnamectl set-hostname k8s-master01 #worker01节点 hostnamectl set-hostname k8s...
-
下一篇
教你如何进行Prometheus 分片自动缩放
本文分享自华为云社区《使用 Prometheus-Operator 进行 Prometheus + Keda 分片自动缩放》,作者: Kubeservice@董江。 垂直缩放与水平缩放 Prometheus已经成为云原生时代事实上的监控工具。从监控小型花园的实例到企业中大规模的监控,Prometheus 都可以处理工作负载!但并非没有挑战… 在拥有数百个团队的大型组织中,每秒获取数百万个指标是很常见的。人们可以维护一个 Prometheus 实例,并通过投入资金来解决扩展问题:只需获得一个更大的节点即可。好吧,如果你愿意付钱,那就去吧!但是节点价格的增长速度通常高于其大小,并且管理大型和小型 Prometheus 实例之间还有另一个很大的区别:WAL 重播! Prometheus 保留一个包含最新抓取数据的内存数据库。为了避免在可能的重新启动期间丢失数据,Prometheus 在磁盘上保留了预写日志 (WAL)。当 Prometheus 重启时,它会将 WAL 重新加载到内存中,这样最新抓取的数据就又可用了,这个操作就是我们所说的 WAL Replay。 在 WAL 重放期间,Prom...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL数据库在高并发下的优化方案
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- 设置Eclipse缩进为4个空格,增强代码规范
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- SpringBoot2整合Redis,开启缓存,提高访问速度








微信收款码
支付宝收款码