一 背景
为工作负载配置HPA弹性伸缩策略,可以实现pod实例的动态扩缩容,提高了资源利用率的同时也解决了流量高峰场景下的应用性能瓶颈。
HPA指标状态、实例大小变化等这些信息对用户来说也是十分宝贵的,可以帮助我们了解业务程序的高峰期是什么时候,以及设置HPA的大小边界是否合理。但是通常kubernetes事件的记录保持时间仅有1h,应用程序如果在凌晨触发了HPA事件,对于用户来说是无法感知的,所以就需要一个监控界面或者告警通知的行为来知晓HPA活动情况。
二 HPA相关指标普罗监控解决方案
开源kube-state-metrics程序可以用来监控kubernetes集群中的各种资源信息,包括 horizontalpodautoscalers、deployments、pods、services等
其中HPA关键指标有:
kube_horizontalpodautoscaler_spec_max_replicas: HPA设置的最大实例数
kube_horizontalpodautoscaler_spec_min_replicas:HPA设置的最小实例数
kube_horizontalpodautoscaler_spec_target_metric: HPA设置的期望指标值
kube_horizontalpodautoscaler_status_target_metric:HPA对应负载的实际指标值
kube_horizontalpodautoscaler_status_condition: HPA状态包括AbleToScale,ScalingActive,ScalingLimited
kube_horizontalpodautoscaler_status_current_replicas: HPA对应负载当前实例数
kube_horizontalpodautoscaler_status_desired_replicas:HPA对应负载的期望实例数
可以通过使用这些指标进行dashboard的定制,实现HPA活动情况的监测。
安装开源kube-state-metrics组件,对接prometehus监控系统,最后使用grafana定制相关dashboard。
注意因为只需要采集hpa相关指标信息,本文档kube-state-metrics程序的安装只限制了对horizontalpodautoscalers资源的采集。其它资源指标的采集不作演示。
2.1 安装kube-state-metrics程序
创建serviceaccount,负载实例需要使用该sa身份进行集群资源获取
apiVersion: v1
automountServiceAccountToken: false
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: 2.10.0
name: kube-state-metrics
namespace: monitoring
创建cluster-role,定义rules规则
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: 2.10.0
name: kube-state-metrics
rules:
- apiGroups:
- autoscaling
resources:
- horizontalpodautoscalers
verbs:
- list
- watch
创建cluster-role-binding,将serviceaccount与上述role进行绑定
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: 2.10.0
name: kube-state-metrics
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kube-state-metrics
subjects:
- kind: ServiceAccount
name: kube-state-metrics
namespace: monitoring
创建kube-state-metrics工作负载
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: 2.10.0
name: kube-state-metrics
namespace: monitoring
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: kube-state-metrics
template:
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: 2.10.0
spec:
automountServiceAccountToken: true
containers:
- image: registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.10.0
args:
- '--resources=horizontalpodautoscalers'
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
timeoutSeconds: 5
name: kube-state-metrics
ports:
- containerPort: 8080
name: http-metrics
- containerPort: 8081
name: telemetry
readinessProbe:
httpGet:
path: /
port: 8081
initialDelaySeconds: 5
timeoutSeconds: 5
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 65534
seccompProfile:
type: RuntimeDefault
serviceAccountName: kube-state-metrics
创建service
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: 2.10.0
name: kube-state-metrics
namespace: monitoring
spec:
clusterIP: None
ports:
- name: http-metrics
port: 8080
targetPort: http-metrics
- name: telemetry
port: 8081
targetPort: telemetry
selector:
app.kubernetes.io/name: kube-state-metrics
上述各种资源部署完毕后,查看容器状态
2.2 编写servicemonitor对接prometheus
集群使用的prometheus-operator方式,所以需要配置servicemonitor crd进行指标抓取
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
name: kube-state-metrics
namespace: monitoring
labels:
service-monitor: kube-state-metrics
spec:
selector:
matchLabels:
app.kubernetes.io/name: kube-state-metrics
endpoints:
- port: http-metrics
将上述规则在集群中应用后,稍等片刻查看Prometheus 控制台可以发现kube-state-metrics指标已经成功抓取
2.3 使用Grafana dashboard 进行指标展示
以下看板可以查看对应HPA的最大最小实例数,以及期望实例数和当前运行的实例数。还可以查看历史时刻相关HPA实例的震荡图,并将期望实例数和当前运行实例数进行对比,更加直观的看到HPA快速拉起pod实例的过程
三 HPA事件告警通知解决方案
该方案借助于AOM在CCE上的log-agent插件和华为云AOM服务实现。其中log-agent负责采集kubernetes事件,并将事件持久化存储在华为云LTS服务中,AOM服务可以用于实现基于CCE集群事件的告警功能。
3.1 开通kubernetes 事件采集策略
需要前往CCE控制台 -->> 日志中心 -->> 日志采集策略进行配置
配置完成后,查看kubernetes事件记录
可先触发HPA弹性伸缩事件,然后再观察事件记录
3.2 前往AOM服务配置告警规则
前往告警列表查看事件信息
创建告警规则
前往告警规则页面创建告警,创建告警时需要使用事件名称,可参考告警列表中的事件名称
告警规则的设置,需要使用事件告警规则,事件类型为自定义事件
最后需要开启告警通知通能,才能将事件告警以邮箱或者短信、微信、钉钉等方式进行通知。若没有可用的行动规则,可参照3.3章节进行告警行动规则的配置
3.3 配置告警行动规则
首先进行消息通知服务的主题配置
添加订阅可以决定使用何种方式进行告警信息通知
添加完毕后,系统会发发送订阅确认邮件。点击订阅确认即可。
创建告警行动规则
告警行动规则中需要使用上述步骤创建的消息主题。消息模板可适用内置的aom.built-in.template.zh
3.4 确认告警信息发送
手动触发弹性伸缩事件
观察邮箱是否收到HPA弹性事件通知
邮箱已收到CCE集群HPA事件告警通知