背景
在Elastic Stack 7.5版本之前,系统默认采用内置服务进行监控数据采集(称为内部收集机制),这种设计存在显著局限性:
-
当ES集群崩溃时自带的节点监控也会随之崩溃,直到集群恢复前,崩溃期间的监控指标将丢失。
-
对于生产集群,需要为自带的节点监控提供额外的资源,会给集群带来一定压力。
为解决这些问题,Metricbeat作为独立轻量级数据采集器应运而生——它可直接部署于服务器节点,通过外部进程持续采集操作系统及Elasticsearch等服务的运行时指标(如健康状态、性能参数),并绕过生产集群直接将数据推送至专用监控集群。这种架构革新实现了监控体系与业务系统的物理解耦:既消除了监控功能对生产环境资源的挤占,又通过独立数据管道保障了监控数据的完整性与传输可靠性,即使主集群完全宕机,仍能持续记录关键故障日志,为事后诊断提供完整数据支撑。
本篇情景:已搭建好es集群和kibana面板,再部署metricbeat对每个es进行监控,设置参数进行钉钉告警。
准备工作
kibana左侧菜单栏倒数第3个为Dev Tools:
在部署前,需要开启es的xpack,自动收集监控数据,在kibana的Dev Tools中输入以下指令进行更改:
PUT _cluster/settings
{"persistent": {"xpack.monitoring.collection.enabled": true}
}
禁用生产集群默认的 Elasticsearch 指标监控:
PUT _cluster/settings
{"persistent": {"xpack.monitoring.elasticsearch.collection.enabled": false}
}
检查:
GET _cluster/settings
使用helm包部署metric
部署的配置文件
#values.yaml# 这里把 daemonset 类型注释掉,下面有 deployment 类型的
daemonset:enabled: false
# 以下的内容,都是在 deployment 层下面修改的
deployment:
# 这里要注意 es 用户名密码的 secret 名字是不是集群有的extraEnvs:- name: "ELASTICSEARCH_USERNAME"valueFrom:secretKeyRef:name: elastic-credentialskey: username- name: "ELASTICSEARCH_PASSWORD"valueFrom:secretKeyRef:name: elastic-credentialskey: password# 挂载存放 HTTPS 证书的 Secret(这里使用 elastic-certificates,若证书在 elastic-certificate-pem 中请相应修改 secretName)extraVolumeMounts: []extraVolumes: []
# metricbeat 配置文件的处理metricbeatConfig:metricbeat.yml: |metricbeat.modules:# es 采集配置- module: elasticsearch # 采集指标,一定要注意格式,缩进!!! metricsets:- ccr- cluster_stats- enrich- index- index_recovery- index_summary- ml_job- node_stats- shard# 启用xpackxpack.enabled: trueperiod: 10s# ES节点地址,这里我们采集当前的监控集群,集群有多个es节点,将IP全加入hosts: ["http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200",]username: "${ELASTICSEARCH_USERNAME}"password: "${ELASTICSEARCH_PASSWORD}"# 集群模式scope: cluster# 定义 metricbeat 采集的指标推送到哪个 es# 这里是推送到监控集群output.elasticsearch:hosts: ["http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200", "http://xx.xx.xx.xxx:9200"]username: "${ELASTICSEARCH_USERNAME}"password: "${ELASTICSEARCH_PASSWORD}"_source.enabled: true#preset: balanced#protocol: "http"#ssl.enabled: false#ssl.certificate_authorities: ["/etc/metricbeat/certs/ca.crt"]# 增加自定义的标签信息processors:- add_host_metadata: ~- add_docker_metadata: ~- add_kubernetes_metadata: ~# 配置日志记录等级,error 错误才需要记录logging.level: errormonitoring.enabled: true# 这里的 uuid 是 metricbeat.modules 里面的 es 集群的 uuid## 可以通过 kibana 或者 curl 使用 GET /_cluster/state 来获取monitoring.cluster_uuid: "Jxxxxg"# 启用 http,便于健康检查http.enabled: truehttp.host: 0.0.0.0http.port: 5066setup:template.enabled: falseilm.enabled: false#template.settings:# index.number_of_shards: 1#index.codec: best_compression
# 我不是 https 的集群,不需要证书,这里直接注释掉,记得在 secretMounts 后面加上 []secretMounts: []#- name: elasticsearch-master-certs# secretName: elastic-certificates#path: /usr/share/metricbeat/certs/
# 配置资源限制,根据自己情况选择改不改resources:requests:cpu: "100m"memory: "100Mi"limits:cpu: "1000m"memory: "1Gi"
# 修改镜像 tag 注意,此处的版本一定一定一定要和es的版本匹配!!我这里使用的是es7.7.1,metricbeat版本也为7.7.1
imageTag: "7.7.1"
# 不安装 kube_state_metrics,因为不监控 k8s 集群,若没用加这段参数,将默认开启对k8s集群的监控,需要拉取k8s监控镜像k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.4.1
kube_state_metrics:enabled: false
部署
helm install metricbeat elastic/metricbeat -n devops -f values.yaml
当看到jkibana的stackmonitoring页面节点详情如下时,则部署成功。
Metricbeat监控配置避坑指南
-
版本兼容性控制 必须确保Metricbeat与Elasticsearch、Kibana版本严格对齐(如均采用7.7.1版本)。版本差异可能导致监控指标丢失、数据格式不兼容等问题,极端情况下甚至引发模块初始化失败。
-
注意缩进格式,尤其是在
metricbeatConfig.metricbeat.yml.module.metricsets
部分,因为直接使用helm安装只能看打印的日志报错,如:ERROR instance/beat.go:932 Exiting: The elasticsearch module with xpack.enabled: true must have metricsets: [ccr enrich cluster_stats index index_recovery index_summary ml_job node_stats shard]。没有配置上metricsets,但按理来说如果将这整段去掉会使用自带的默认配置,但是并没有,所以还是得手动配上,如果没注意换行,报错只会一直这样提示你没有这段,让人很puzzle。 -
这里没有设置专门的es节点用于监控,所以
output.elasticsearch.hosts
这里配置为被监控的节点地址即可,如output.elasticsearch: hosts: ["http://xx.xx.xx.xx:9200"],由于Elasticsearch 集群机制,即使你只配置了一个节点,Elasticsearch 集群内部会自动将数据同步到其他节点。因此,从集群的角度来看,数据仍然是分布式的,如果不放心,也可以将所有节点全部写入,如上面的yaml。 -
关于安全证书,一开始我以为es可以通过https访问有ssl安全证书则metricbeat就也要配置ssl,但其实不然,metricbeat监控是针对es集群所在的节点进行监控,所以只要是配置在相同的网段下通过ip+port能访问就行,而用k8s部署是在同一个namespace下,环境也都一样,就不存在网络不同的情况,所以ssl证书问题不用考虑,直接将ip配上即可(都是内网),metricbeat也是将数据再发回es。配证书的话更麻烦。
-
关于登录es的配置,这里是直接使用K8s的secret统一管理,也可以直接配置为:hosts: ["http://xx.xx.xx.xx:9200"]username: "username"password: "pwd",看场景选择。
参考链接:
k8s 部署 metribeat 实现 kibana 可视化 es 多集群监控指标-CSDN博客
使用metricbeat 监控多ES集群-CSDN博客
metricbeat收集elasticsearch、kibana监控数据_kibana metricbeat监测-CSDN博客
metricbeat. elasticsearch-xpack module failed to start. · Issue #14984 · elastic/beats · GitHub