在 Kubernetes 上设置 eBPF 性能分析
要在 Kubernetes 上使用 Grafana Agent 设置 eBPF 性能分析,您需要
- 验证您的集群是否满足先决条件。
- 添加 Grafana helm 存储库。
- 创建一个 Grafana Agent 配置文件。有关更多信息,请参阅配置参考。
- 安装 Grafana Agent。
- 验证是否已收到配置文件。
注意
Grafana Alloy 是我们分发的 OTel 收集器的名称。Grafana Agent 已弃用,并且在 2025 年 10 月 31 日之前处于长期支持 (LTS) 状态。Grafana Agent 将于 2025 年 11 月 1 日达到使用寿命终止 (EOL)。详细了解我们建议迁移到Grafana Alloy的原因。
先决条件
在开始之前,您需要
- Helm 和 kubectl 安装并可以访问您的 Kubernetes 集群。
- 一个 Pyroscope 服务器,Agent 将向其发送性能分析数据。
- 可以访问 Grafana 并已配置 Grafana Pyroscope 数据源。
注意
如果您没有 Grafana 和/或 Pyroscope 服务器,您可以使用 [Grafana Cloud][gcloud] 免费计划开始使用。
验证您的集群是否满足要求
eBPF 性能分析器需要 Linux 内核版本 >= 4.9(由于 BPF_PROG_TYPE_PERF_EVENT)。
BPF_PROG_TYPE_PERF_EVENT
是一种 eBPF 程序类型,可以附加到 Linux 内核中的硬件或软件事件(例如性能监控计数器或跟踪点)。
要打印集群中每个节点的内核版本,请运行
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.nodeInfo.kernelVersion}{"\n"}{end}'
确保所有节点的内核版本都大于等于 4.9。
添加 Grafana Helm 仓库
我们建议使用 Helm 来安装 Grafana Agent。要添加 Grafana Helm 仓库,请运行以下命令:
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
通过运行以下命令验证仓库是否已成功添加:
helm search repo grafana/grafana-agent
它应该会返回 Grafana Agent 可用版本的列表。
创建 Grafana Agent 配置文件
创建一个名为 values.yaml
的文件,内容如下:
agent:
mode: 'flow'
configMap:
create: true
content: |
discovery.kubernetes "local_pods" {
selectors {
field = "spec.nodeName=" + env("HOSTNAME")
role = "pod"
}
role = "pod"
}
pyroscope.ebpf "instance" {
forward_to = [pyroscope.write.endpoint.receiver]
targets = discovery.kubernetes.local_pods.targets
}
pyroscope.write "endpoint" {
endpoint {
basic_auth {
password = "<PASSWORD>"
username = "<USERNAME>"
}
url = "<URL>"
}
}
securityContext:
privileged: true
runAsGroup: 0
runAsUser: 0
controller:
hostPID: true
将 <URL>
占位符替换为相应的服务器 URL。这可能是 Grafana Cloud URL 或您自己的自定义 Pyroscope 服务器 URL。
如果您需要将数据发送到 Grafana Cloud,则必须配置 HTTP 基本身份验证。将 <User>
替换为您的 Grafana Cloud 堆栈用户,并将 <Password>
替换为您的 Grafana Cloud API 密钥。
注意
如果您使用的是自己的 Pyroscope 服务器,则可以完全删除basic_auth
部分。
有关更多信息,请参阅 配置 Grafana Pyroscope 数据源文档。
安装 Grafana Agent
要安装 Grafana Agent,请运行以下命令:
helm install pyroscope-ebpf grafana/grafana-agent -f values.yaml
配置完成后,Agent 将开始收集 eBPF 剖析信息并将其发送到 Pyroscope 服务器。
验证是否已收到剖析信息
要验证 Pyroscope 服务器是否已收到剖析信息,请转到 Pyroscope UI 或 Grafana Pyroscope 数据源。从下拉菜单中选择剖析信息类型和服务。