使用 Helm chart 部署 Pyroscope
通过 Helm chart,您可以在 Kubernetes 集群中配置、安装和升级 Pyroscope。
准备工作
这些说明适用于任何 Kubernetes 版本,并假设您了解如何安装、配置和操作 Kubernetes 集群以及如何使用 kubectl
。
硬件要求
- 单个 Kubernetes 节点,至少配备 4 核 CPU 和 16GiB RAM
软件要求
- Kubernetes 1.20 或更高版本
- 与您的 Kubernetes 版本对应的
kubectl
命令 - Helm 3 或更高版本
请确认您已具备以下条件
- 对 Kubernetes 集群的访问权限
- Kubernetes 集群中已启用持久存储,并已设置默认存储类。您可以修改默认 StorageClass。
- DNS 服务在 Kubernetes 集群中正常工作
在自定义命名空间中安装 Helm chart
使用自定义命名空间,以免在后续步骤中覆盖默认命名空间。
创建一个唯一的 Kubernetes 命名空间,例如
pyroscope-test
kubectl create namespace pyroscope-test
有关更多详细信息,请参阅 Kubernetes 文档关于创建新命名空间的部分。
使用以下命令设置 Helm 仓库
helm repo add grafana https://grafana.github.io/helm-charts helm repo update
注意
位于 https://grafana.github.io/helm-charts 的 Helm chart 是 grafana/pyroscope 源代码的发布版本。
使用以下任一选项通过 Helm chart 安装 Pyroscope
- 选项 A:将 Pyroscope 安装为单个二进制文件。仅在需要一个 Pyroscope 实例时使用此模式。多个实例之间不会共享信息。
helm -n pyroscope-test install pyroscope grafana/pyroscope
注意
该命令的输出包含后续步骤所需的查询 URL,因此对于单二进制设置,它将如下所示
[...] The in-cluster query URL is: http://pyroscope.pyroscope-test.svc.cluster.local.:4040/ [...]
- 选项 B:将 Pyroscope 安装为多个微服务。在此模式下,当您扩展实例数量时,它们将共享一个统一的存储和查询后端。
# Gather the default config for micro-services curl -Lo values-micro-services.yaml https://raw.githubusercontent.com/grafana/pyroscope/main/operations/pyroscope/helm/pyroscope/values-micro-services.yaml helm -n pyroscope-test install pyroscope grafana/pyroscope --values values-micro-services.yaml
注意
该命令的输出包含后续步骤所需的查询 URL,因此对于微服务设置,它将如下所示
[...] The in-cluster query URL is: http://pyroscope-querier.pyroscope-test.svc.cluster.local.:4040 [...]
检查 Pyroscope Pod 的状态
kubectl -n pyroscope-test get pods
在微服务模式下,结果类似于这样
kubectl -n pyroscope-test get pods NAME READY STATUS RESTARTS AGE pyroscope-agent-7d75b4f9dc-xwpsw 1/1 Running 0 3m23s pyroscope-distributor-7c474947c-2p5cc 1/1 Running 0 3m23s pyroscope-distributor-7c474947c-xbszv 1/1 Running 0 3m23s pyroscope-ingester-0 1/1 Running 0 5s pyroscope-ingester-1 1/1 Running 0 37s pyroscope-ingester-2 1/1 Running 0 69s pyroscope-minio-0 1/1 Running 0 3m23s pyroscope-querier-66bf58dfcc-89gb8 1/1 Running 0 3m23s pyroscope-querier-66bf58dfcc-p7lnc 1/1 Running 0 3m23s pyroscope-querier-66bf58dfcc-zbggm 1/1 Running 0 3m23s
请等待所有 Pod 的状态变为
Running
或Completed
,这可能需要几分钟。
在 Grafana 中查询 Profiling 数据
在安装 Pyroscope 的同一个 Kubernetes 集群中安装 Grafana。
helm upgrade -n pyroscope-test --install grafana grafana/grafana \ --set image.repository=grafana/grafana \ --set image.tag=main \ --set env.GF_INSTALL_PLUGINS=grafana-pyroscope-app \ --set env.GF_AUTH_ANONYMOUS_ENABLED=true \ --set env.GF_AUTH_ANONYMOUS_ORG_ROLE=Admin \ --set env.GF_DIAGNOSTICS_PROFILING_ENABLED=true \ --set env.GF_DIAGNOSTICS_PROFILING_ADDR=0.0.0.0 \ --set env.GF_DIAGNOSTICS_PROFILING_PORT=9094 \ --set-string 'podAnnotations.profiles\.grafana\.com/cpu\.scrape=true' \ --set-string 'podAnnotations.profiles\.grafana\.com/cpu\.port=9094' \ --set-string 'podAnnotations.profiles\.grafana\.com/memory\.scrape=true' \ --set-string 'podAnnotations.profiles\.grafana\.com/memory\.port=9094' \ --set-string 'podAnnotations.profiles\.grafana\.com/goroutine\.scrape=true' \ --set-string 'podAnnotations.profiles\.grafana\.com/goroutine\.port=9094'
详情请参阅在 Kubernetes 上部署 Grafana。
使用
kubectl
命令将 Grafana 端口转发到localhost
kubectl port-forward -n pyroscope-test service/grafana 3000:80
在浏览器中,访问 Grafana 服务器:https://:3000。
在左侧导航栏,转到配置 (Configuration) > 数据源 (Data sources)。
使用以下设置配置 Pyroscope 数据源以查询 Pyroscope 服务器
字段 值 名称 Pyroscope URL http://pyroscope-querier.pyroscope-test.svc.cluster.local.:4040/
要添加数据源,请参阅添加数据源。
验证成功
您应该能够在 Grafana Explore 中查询 Profiling 数据,并使用新配置的 Pyroscope 数据源创建仪表盘面板。
可选:持久添加数据源
Grafana 的部署没有持久数据库,因此在重启后不会保留数据源配置等设置。
为确保数据源在启动时被配置,创建以下 datasources.yaml
文件
datasources:
pyroscope.yaml:
apiVersion: 1
datasources:
- name: Pyroscope
type: grafana-pyroscope-datasource
uid: pyroscope-test
url: http://pyroscope-querier.pyroscope-test.svc.cluster.local.:4040/
通过运行以下命令修改 Helm 部署
helm upgrade -n pyroscope-test --reuse-values grafana grafana/grafana \
--values datasources.yaml
可选:抓取您自己的工作负载的 Profiling 数据
Pyroscope chart 使用默认配置,使其 Agent 抓取具有正确注解的 Pod。此功能使用了您可能在 Prometheus 或 Grafana Alloy 配置中熟悉的 relabel_config 和 kubernetes_sd_config。
要让 Pyroscope 抓取 Pod,您必须向 Pod 添加以下注解
metadata:
annotations:
profiles.grafana.com/memory.scrape: "true"
profiles.grafana.com/memory.port: "8080"
profiles.grafana.com/cpu.scrape: "true"
profiles.grafana.com/cpu.port: "8080"
profiles.grafana.com/goroutine.scrape: "true"
profiles.grafana.com/goroutine.port: "8080"
上述示例将从 Pod 的 8080
端口抓取 memory
、cpu
和 goroutine
的 Profiling 数据。
每种 Profiling 类型都有一组相应的注解,允许按 Profiling 类型自定义抓取。
metadata:
annotations:
profiles.grafana.com/<profile-type>.scrape: "true"
profiles.grafana.com/<profile-type>.port: "<port>"
profiles.grafana.com/<profile-type>.port_name: "<port-name>"
profiles.grafana.com/<profile-type>.scheme: "<scheme>"
profiles.grafana.com/<profile-type>.path: "<profile_path>"
注解支持的完整 Profiling 类型列表为 cpu
、memory
、goroutine
、block
和 mutex
。
下表描述了这些注解
注解 | 描述 | 默认值 |
---|---|---|
profiles.grafana.com/<profile-type>.scrape | 是否抓取该 Profiling 类型的数据。 | false |
profiles.grafana.com/<profile-type>.port | 用于抓取该 Profiling 类型数据的端口。 | `` |
profiles.grafana.com/<profile-type>.port_name | 用于抓取该 Profiling 类型数据的端口名称。 | `` |
profiles.grafana.com/<profile-type>.scheme | 用于抓取该 Profiling 类型数据的协议。 | http |
profiles.grafana.com/<profile-type>.path | 用于抓取该 Profiling 类型数据的路径。 | 默认 golang 路径 |
默认情况下,端口通过命名端口 http2
或以 -metrics
或 -profiles
结尾的端口名称进行发现。如果您没有命名端口,则抓取目标将被忽略。
如果您不想使用端口名称,则可以使用 profiles.grafana.com/<profile-type>.port
注解静态指定端口号。