「Grafana Agent」- 采集 Kubernetes Events 日志

问题描述

该笔记将记录:通过 Grafana Agent 采集 Kubernetes Events 日志的方法,以及相关问题的解决办法。

解决方案

第一步、部署 Grafana Agent Operator 组件

参考 Grafana Agent documentation/Installing Grafana Agent Operator with Helm 文档,以获取更多安装细节;
或者,参考 Installing and Upgrading 笔记;

我们通过 Grafana Agent Operator 组件来完成 Grafana Agent 的部署及管理;

第二步、部署 Grafana Agent 实例

参考 Custom Resource Quickstart/Step 1: Deploy GrafanaAgent resource 文档,该文档足够我们运行一个 Grafana Agent 实例;

但是,针对 Kubernetes Events 采集,需要增加额外的 Integration 配置:

apiVersion: monitoring.grafana.com/v1alpha1
kind: GrafanaAgent
metadata:
  name: grafana-agent
  namespace: observing-client
  labels:
    app: grafana-agent
spec:
...
  integrations:
    selector:
      matchLabels:
        agent: grafana-agent-integrations
...

第三步、部署 Log Instance 资源

---
apiVersion: monitoring.grafana.com/v1alpha1
kind: LogsInstance
metadata:
  name: infra-log-central
  namespace: observing-client
  labels:
    agent: grafana-agent-logs
spec:
  clients:
  
  # 专用于 kubernetes events 的日志
  - url: http://loki-loki-distributed-gateway.loki.svc.cluster.local/loki/api/v1/push
    basicAuth:
      username:
        name: infra-log-central
        key: username
      password:
        name: infra-log-central
        key: password
    externalLabels:
      cluster_name: d3rm-infrastructure
      # 鉴于 Grafana Agent 目前暂不支持 Pipeline 与 relabel 配置,所以我们通过该方式添加日志分类
      log_category: kubernetes-events

---
apiVersion: v1
kind: Secret
metadata:
  name: infra-log-central
  namespace: observing-client
stringData:
  username: 'xxxxxxxxxxxxxxxx'
  password: 'xxxxxxxxxxxxxxxx'

第四步、部署 Integration eventhandler 实例

---
apiVersion: monitoring.grafana.com/v1alpha1
kind: Integration
metadata:
  name: eventhandler
  namespace: observing-client
  labels:
    agent: grafana-agent-integrations
spec:
  name: eventhandler
  type: 
    unique: true
    allNodes: false
  config:
    cache_path: "/etc/eventhandler/eventhandler.cache"
    logs_instance: "observing-client/infra-log-central"

关于 logs_instance 字段:在实践中,我们发现通过 Operator 方式部署,最后 LogsInstance 写入配置文件中的命名为 <namespace>/<log-instance-name> 形式;

当创建成功后,将在命名空间下看到名为 grafana-agent-integrations-deploy-xxxx 的 Pod 实例。

第五步、检查 Grafana Loki 中的日志信息

检查 Grafana Loki 是否收集到来自 Grafana Agent 的日志信息;

相关连接

鉴于官方对各 CRD kind 的描述较少,需要我们自己通过阅读 CRD 来了解对象的结构:agent/production/operator/crds

参考文献

Custom Resource Quickstart | Grafana Agent documentation