在 Kubernetes 上设置 eBPF Profiling
要在 Kubernetes 上使用 Grafana Alloy 设置 eBPF profiling,您需要执行以下操作:
开始之前
在开始之前,您需要:
- 已安装 Helm 和 kubectl,并具有访问 Kubernetes 集群的权限。
- 一个 Grafana Pyroscope 服务器,Alloy 可以向其发送 profiling 数据。
- 可访问已配置 Grafana Pyroscope 数据源 的 Grafana。
注意
如果您没有 Grafana 或 Pyroscope 服务器,可以使用 [Grafana Cloud][gcloud] 免费计划开始使用。
验证您的集群满足要求
eBPF profiler 需要 Linux 内核版本 >= 4.9(由于 BPF_PROG_TYPE_PERF_EVENT)。
BPF_PROG_TYPE_PERF_EVENT
是一种 eBPF 程序类型,可以附加到 Linux 内核中的硬件或软件事件,例如性能监控计数器或 tracepoints。
要打印集群中每个节点的内核版本,运行:
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.nodeInfo.kernelVersion}{"\n"}{end}'
确保所有节点的内核版本 >= 4.9。
添加 Grafana Helm 仓库
使用 Helm 安装 Alloy。要添加 Grafana Helm 仓库,运行:
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
运行以下命令验证仓库是否成功添加:
helm search repo grafana/alloy
该命令返回 Alloy 可用版本的列表。
创建 Alloy 配置文件
创建一个名为 values.yaml
的文件,其内容来自示例配置文件。
alloy:
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
有关配置 Alloy 的信息,请参考Kubernetes 上的 Grafana Alloy。
有关使用的具体块的信息,请参考Grafana Alloy 参考。
将 <URL>
占位符替换为适当的服务器 URL。这可以是 Grafana Cloud URL 或您自己的自定义 Pyroscope 服务器 URL。
如果您需要将数据发送到 Grafana Cloud,则必须配置 HTTP 基本身份验证。将 <User>
替换为您的 Grafana Cloud 堆栈用户,将 <Password>
替换为您的 Grafana Cloud API 密钥。
更多信息请参考配置 Grafana Pyroscope 数据源文档。
注意
如果您使用自己的 Pyroscope 服务器,则可以完全删除
basic_auth
部分。
安装 Alloy
要安装 Alloy,运行:
helm install pyroscope-ebpf grafana/alloy -f values.yaml
配置完成后,Alloy 会开始收集 eBPF profiles 并将其发送到 Pyroscope 服务器。
验证是否收到 profiles
要验证 Pyroscope 服务器是否收到 profiles,请执行以下操作:
- 转到 Pyroscope UI 或 Grafana Pyroscope 数据源。
- 从下拉菜单中选择 profile 类型和服务。
容器中应用程序 profiling 的注意事项
使用 Alloy 中的 pyroscope.ebpf
组件 在容器中 profiling Python 应用程序时,请考虑以下事项:
内核版本:确保宿主系统的内核版本 >= 4.9,这是 eBPF 所需的。这对于 profiler 正确运行至关重要
容器权限:eBPF profiler 需要某些权限才能访问内核特性。确保运行 profiler 的容器具有必要的权限。这通常涉及将容器设置为特权模式运行或调整安全上下文
宿主 PID 命名空间:Profiler 可能需要访问宿主的 PID 命名空间才能正确附加到进程。确保您的 Kubernetes 配置中
hostPID
设置为true
网络访问:确保容器具有网络访问权限,可以将 profiling 数据发送到 Pyroscope 服务器。这可能涉及配置网络策略或服务账户