如何在Kubernetes上使用Metricbeat监控ELK(兼容v8且简单的解决方案)

71次阅读
没有评论

问题描述

在使用ELK(Elasticsearch, Logstash, Kibana)时,注意到xpack monitoring已经被标记为即将废弃,建议使用其他解决方案。用户希望找到一个简单的解决方案,在Kubernetes上使用Metricbeat监控ELK,并且能够在任何地方部署。

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案

以下是一个简单的解决方案,需要为每个被监控的实体部署一个额外的Metricbeat实例。这个方法也适用于其他非Kubernetes环境,比如使用docker-compose,只需要稍作调整。

  1. 部署一个sidecar容器来监控某个实体(在这个例子中是Logstash,但是对于Elasticsearch或Kibana来说,只需要替换监控模块和可能的指标端口即可)。
  2. 在logstash.yml(或elastic/kibana)中设置xpack.monitoring.enabled: false
  3. 使用Metricbeat通过本地主机的9600端口获取指标。
  4. 使用挂载的configmaps配置Metricbeat。

以下是一个示例配置,假设没有使用TLS/RBAC连接到Elasticsearch,如果使用了TLS,需要相应地修改配置:

# Configmaps for metricbeat conf files
apiVersion: v1
kind: ConfigMap
metadata:
  name: metricbeat-modules
  namespace: logstash
data:
  logstash-xpack.yml: |-
    - module: logstash
      xpack.enabled: true # 这是打开xpack监控指标导出的开关
      period: 10s
      hosts: ["localhost:9600"] # 使用pod内部的localhost连接到logstash的指标API

apiVersion: v1
kind: ConfigMap
metadata:
  name: metricbeat-config
  namespace: logstash
data:
  metricbeat.yml: |-
    metricbeat.config.modules:
      path: ${path.config}/modules.d/*.yml
      reload.enabled: true
      reload.period: 10s
    metricbeat.modules: # 禁用系统指标
    - module: system
      enabled: false
    output.elasticsearch:
      hosts: ["http://elasticsearch:9200"] # 你可能需要修改elasticsearch的URL
    setup.ilm.enabled: true

在上面的示例中,我们定义了两个configmaps:metricbeat-modulesmetricbeat-configmetricbeat-modules包含了Metricbeat的监控配置,metricbeat-config包含了Metricbeat的全局配置。

然后,在Pod的容器部分,我们添加一个名为metricbeat-sidecar的容器,并挂载上述的configmaps:

containers:
  - name: metricbeat-sidecar
    image: docker.elastic.co/beats/metricbeat:7.14.0
    volumeMounts:
    - name: config
      mountPath: /usr/share/metricbeat/metricbeat.yml
      readOnly: true
      subPath: metricbeat.yml
    - name: modules
      mountPath: /usr/share/metricbeat/modules.d
      readOnly: true

最后,在volumes部分,我们将configmaps挂载到容器中:

volumes:
  - name: config
    configMap:
      defaultMode: 0640
      name: metricbeat-config
  - name: modules
    configMap:
      defaultMode: 0640
      name: metricbeat-modules

请注意,以上示例中的配置是基于Kubernetes的部署,如果你使用其他平台或工具,请根据实际情况进行相应的调整。

参考资料

更新:对于Kibana的类似解决方案,可以参考以下配置:

# Configmaps for metricbeat conf files
apiVersion: v1
kind: ConfigMap
metadata:
  name: kibana-metricbeat-modules
  namespace: elk
data:
  xpack.yml: |-
    - module: kibana
      xpack.enabled: true # 这是打开报告的开关
      period: 10s
      hosts: ["localhost:5601"]

apiVersion: v1
kind: ConfigMap
metadata:
  name: kibana-metricbeat-config
  namespace: elk
data:
  metricbeat.yml: |-
    metricbeat.config.modules:
      path: ${path.config}/modules.d/*.yml
      reload.enabled: true
      reload.period: 10s
    metricbeat.modules: # 禁用系统指标
    - module: system
      enabled: false
    output.elasticsearch:
      hosts: ["http://elasticsearch:9200"]
    setup.ilm.enabled: true

在Pod的容器部分,添加一个名为metricbeat-sidecar的容器,并挂载上述的configmaps:

containers:
  - name: metricbeat-sidecar
    image: docker.elastic.co/beats/metricbeat:7.14.0
    volumeMounts:
    - name: config
      mountPath: /usr/share/metricbeat/metricbeat.yml
      readOnly: true
      subPath: metricbeat.yml
    - name: modules
      mountPath: /usr/share/metricbeat/modules.d
      readOnly: true

最后,在volumes部分,将configmaps挂载到容器中:

volumes:
  - name: config
    configMap:
      defaultMode: 0640
      name: kibana-metricbeat-config
  - name: modules
    configMap:
      defaultMode: 0640
      name: kibana-metricbeat-modules

以上是一个简单的解决方案,根据实际情况进行相应的调整。

希望对你有帮助!

正文完