一、基于Prometheus的HPA自动伸缩
背景:
- Kubernetes集群规模大、动态变化快,而且容器化应用部署和服务治理机制的普及,传统的基础设施监控方式已经无法满足Kubernetes集群的监控需求。
- 需要使用专门针对Kubernetes集群设计的监控工具来监控集群的状态和服务质量。
Prometheus则是目前Kubernetes集群中最常用的监控工具之一,它可以通过Kubernetes API中的
metrics-server
获取Kubernetes
集群的指标数据,从而实现对Kubernetes集群的应用层面监控,以及基于它们的水平自动伸缩对象HorizontalPodAutoscaler
。
Metrics-server部署配置
Metrics Server 的项目网址( https://github.com/kubernetes-sigs/metrics-server)
Metrics Server 默认使用
TLS 协议
,要验证证书才能与kubelet
实现安全通信,而我们的内网环境里没有这个必要。默认镜像源非国内,如有下载失败的小伙伴,更改镜像为如下阿里云提供的即可:
HorizontalPodAutoscaler
HorizontalPodAutoscaler (HPA)是Kubernetes中的一个控制器,用于动态地调整Pod副本的数量。HPA可以根据Metrics-server提供的指标(如CPU使用率、内存使用率等)或内部指标(如每秒的请求数)来自动调整Pod的副本数量,以确保应用程序具有足够的资源,并且不会浪费资源。
HPA是Kubernetes扩展程序中非常常用的部分,特别是在负载高峰期自动扩展应用程序时。
使用HorizontalPodAutoscaler
创建一个 Nginx 应用,定义 Deployment 和 Service,作为自动伸缩的目标对象:
注意在它的
spec
里一定要用resources
字段写清楚资源配额,否则HorizontalPodAutoscaler
会无法获取 Pod 的指标,也就无法实现自动化扩缩容。接下来我们要用命令
kubectl autoscale
创建一个HorizontalPodAutoscaler
的样板 YAML 文件,它有三个参数:
- min,Pod 数量的最小值,也就是缩容的下限。
- max,Pod 数量的最大值,也就是扩容的上限。
- cpu-percent,CPU 使用率指标,当大于这个值时扩容,小于这个值时缩容。
现在我们就来为刚才的 Nginx 应用创建 HorizontalPodAutoscaler,指定 Pod 数量最少 2 个,最多 8 个,CPU 使用率指标设置的小一点,5%,方便我们观察扩容现象:
测试验证
下面我们来给 Nginx 加上压力流量,运行一个测试 Pod,使用的镜像是
httpd:alpine
,它里面有 HTTP 性能测试工具 ab(Apache Bench):
然后我们向 Nginx 发送一百万个请求,持续 1 分钟,再用
kubectl get hpa
来观察HorizontalPodAutoscaler
的运行状况:
Metrics Server
大约每 15 秒采集一次数据,所以HorizontalPodAutoscaler
的自动化扩容和缩容也是按照这个时间点来逐步处理的。当它发现目标的 CPU 使用率超过了预定的 5% 后,就会以 2 的倍数开始扩容,一直到数量上限,然后持续监控一段时间;
如果 CPU 使用率回落,就会再缩容到最小值 (默认会等待五分钟如果负载没有上去,就会缩小到最低水平,防止抖动)。
验证如下图:
总结
- 1、
Metrics Server
是Kubernetes中的一个组件,它可以将集群中的散布的资源使用情况数据收集并聚合起来。收集的数据包括节点的CPU和内存使用情况等。- 2、通过API提供给
Kubernetes
中的其它组件(如HPA)使用。Metrics Server
可以帮助集群管理员和应用程序开发者更好的了解集群中资源的使用情况,并根据这些数据做出合理的决策,例如调整Pod副本数、扩展集群等。- 3、
Metrics Server
对于Kubernetes中的资源管理和应用程序扩展非常重要。
二、黑盒监控Blackbox
Prometheus
监控分为两种:
- 白盒监控
- 黑盒监控
白盒监控:是指我们日常监控主机的资源用量、容器的运行状态的运行数据。
黑盒监控:常见的黑盒监控包括
HTTP探针
、TCP探针
、Dns
、Icmp
等用于检测站点、服务的可访问性、服务的连通性,以及访问效率等。两者比较:
- 黑盒监控是以故障为导向当故障发生时,黑盒监控能快速发现故障。
- 白盒监控则侧重于主动发现或者预测潜在的问题。
一个完善的监控目标是要能够从白盒的角度发现潜在问题,能够在黑盒的角度快速发现已经发生的问题。
目前支持的应用场景:
- ICMP 测试
- 主机探活机制
- TCP 测试
- 业务组件端口状态监听
- 应用层协议定义与监听
- HTTP 测试
- 定义 Request Header 信息
- 判断 Http status / Http Respones Header / Http Body 内容
- POST 测试
- 接口联通性
- SSL 证书过期时间
Blackbox Exporter 部署
blackbox 的部署分为两块:一个是配置文件,一个是deployment
DNS 监控
参数解释:
ICMP监控
监控(K8S 内部发现方法)
自定义发现 Service
监控 端口
和 路径
TCP检测
HTTP 监控(监控外部域名)
HTTP Post 监控(监控外部域名)
总结
- Blackbox Exporter是一个用于监控网络服务的开源工具。
- 通过模拟HTTP、HTTPS、DNS、TCP等协议向服务端发送请求,并返回响应码、响应时间等信息。
- Blackbox Exporter具有高度的灵活性,可以通过配置文件进行定制,包括对请求的频率、超时时限、请求头、请求参数等进行配置。
- Blackbox Exporter还支持Prometheus监控系统,可以将采集到的数据自动发送给Prometheus,并进行聚合和展示。
三、自定义资源接入
Prometheus使用各种Exporter来监控资源。Exporter可以看成是监控的agent端,它负责收集对应资源的指标,并提供接口给到Prometheus读取。
虚机数据抓取
配置安装node-exporter
配置
数据收集到prometheus
process-exporter进程监控
少于某个数量进行告警
创建挂载目录
配置安装process-exporter
自定义中间件监控
创建mysql监听用户并授权,注意服务ip不要填错
启动容器
总结
- 1、安装部署自定义 Exporter 组件;
- 2、配置prometheus-config文件做数据抓取;
- 3、配置prometheus-rules做监控告警;
- 4、配置grafana;
四、基于Consul的自动发现
背景
Prometheus配置文件
prometheus-config.yaml
配置了大量的采集规则,基本上都是运维小伙伴手动处理,如果后面增加了节点或者组件信息,就得手动修改此配置,并热加载 promethues;那么能否动态的监听微服务呢?Prometheus 提供了多种动态服务发现的功能,这里以 consul 为例。
基于Consul的自动发现
Consul是分布式k/v数据库,是一个服务注册组件,其他服务都可以注册到consul上,Prometheus也不例外,通过consul的服务发现,我们可以避免在Prometheus中指定大量的target。
prometheus基于consul的服务发现流程如下:
- 在consul注册服务或注销服务(监控targets)
- Prometheus一直监视consul服务,当发现consul中符合要求的服务有新变化就会更新Prometheus监控对象
Prometheus 支持的多种服务发现机制
Prometheus数据源的配置主要分为 静态配置 和 动态发现 , 常用的为以下几类:
工作原理
1、Prometheus通过Consul API查询Consul的KV存储中保存的配置信息,然后从中获取关于服务的元数据;
2、Prometheus使用这些信息来构造目标服务的URL,并将其添加到服务发现的目标列表中。
3、当服务被注销或不可用时,Prometheus将自动从其目标列表中删除该服务。
容器化Consul集群
单节点测试验证,不可作为线上使用! 线上一定要基于集群的方式做整体的部署验证,并做服务进程的守护及监控。
创建一个只有一个节点的consul集群
测试验证:
可通过web端访问,例如:http://"节点IP":18500
注册主机到Consul
例如:将某台虚机上的
node-exporter
注册到 consule.
Prometheus配置Consul实现自动服务发现
总结
- 动态服务发现和监控:通过与Consul集成,Prometheus可以动态地维护其目标列表,确保在新服务上线时及时发现和监控它们。
- 可扩展性:自动服务发现使得扩展基础架构变得更加容易,无需担心监控数据的可用性和性能问题。
- 无缝集成:Consul作为服务注册中心,使得Prometheus可以与Consul生态系统中的其他工具进行无缝集成,提供完整的服务基础架构监控和管理解决方案。
- 自愈能力:自动服务发现意味着Prometheus可以自动检测服务基础架构的变化,并在实时调整监控目标列表,确保监控数据的连续性和高性能。