旧版 Profiler 注入¶
Profiler 注入仅用于兼容旧版部署
新接入请使用 Flameshot。Flameshot 支持按进程配置采集时长,也可以定时采集,或在 CPU、内存达到阈值时触发采集。
旧版 datakit-profiler Sidecar 使用 async-profiler 对 Java 应用采样,使用 py-spy 对 Python 应用采样。
前置条件¶
- DataKit 已开启 Profile 采集器。
- Pod 安全策略允许 Sidecar 增加
SYS_PTRACE和SYS_ADMINcapability。
Operator 配置¶
为需要采样的语言配置 admission_inject_v2.profilers。下面的两条规则分别匹配 production 命名空间中带有对应语言标签的 Pod。
{
"admission_inject_v2": {
"profilers": [
{
"name": "legacy-java-profiler",
"language": "java",
"namespace_selectors": [
"^production$"
],
"label_selectors": [
"profiling=async-profiler"
],
"check_annotation": false,
"image": "pubrepo.guance.com/datakit-operator/async-profiler:0.6.0",
"envs": {
"DK_AGENT_HOST": "datakit-service.datakit.svc.cluster.local",
"DK_AGENT_PORT": "9529",
"DK_PROFILE_DURATION": "240",
"DK_PROFILE_SCHEDULE": "0 * * * *"
}
},
{
"name": "legacy-python-profiler",
"language": "python",
"namespace_selectors": [
"^production$"
],
"label_selectors": [
"profiling=py-spy"
],
"check_annotation": false,
"image": "pubrepo.guance.com/datakit-operator/py-spy:0.1.0",
"envs": {
"DK_AGENT_HOST": "datakit-service.datakit.svc.cluster.local",
"DK_AGENT_PORT": "9529",
"DK_PROFILE_DURATION": "240",
"DK_PROFILE_SCHEDULE": "0 * * * *"
}
}
]
}
}
如果启用 check_annotation: true,请按语言在 Pod 上设置 admission.datakit/java-profiler.version 或 admission.datakit/python-profiler.version,其值用于替换镜像 tag。要对单个 Pod 停用旧版 Profiler,设置 admission.datakit/profiler.enabled: "false"。
匹配后,Operator 会添加 datakit-profiler Sidecar、共享进程命名空间以及工作目录、/tmp 和 /etc/localtime 挂载,并将 Pod restartPolicy 设置为 Always。上线前应确认这些变更符合 Pod 安全策略。
Java (async-profiler)¶
节点允许 perf_events,通常要求 kernel.perf_event_paranoid 不大于 2。
apiVersion: apps/v1
kind: Deployment
metadata:
name: movies-java
namespace: production
spec:
replicas: 1
selector:
matchLabels:
app: movies-java
template:
metadata:
labels:
app: movies-java
profiling: async-profiler
spec:
containers:
- name: app
image: example/movies-java:1.2.3
securityContext:
seccompProfile:
type: Unconfined
创建后执行 kubectl get pod -n production -l app=movies-java -o yaml,确认存在 datakit-profiler。没有数据时检查 Sidecar 日志、内核参数、capability 和 DataKit Profile 接收地址。
Python (py-spy)¶
py-spy 仅支持 CPython。
apiVersion: apps/v1
kind: Deployment
metadata:
name: movies-python
namespace: production
spec:
replicas: 1
selector:
matchLabels:
app: movies-python
template:
metadata:
labels:
app: movies-python
profiling: py-spy
spec:
containers:
- name: app
image: example/movies-python:1.2.3
创建后执行 kubectl get pod -n production -l app=movies-python -o yaml,确认存在 datakit-profiler。没有数据时检查 Sidecar 日志、capability、目标进程是否为 CPython,以及 DataKit Profile 接收地址。