如何用 grab-profiles.sh 抓取运行中 Kubernetes 组件的 CPU 与内存 profile

发布时间:2026/9/9 12:41:45
如何用 grab-profiles.sh 抓取运行中 Kubernetes 组件的 CPU 与内存 profile 如何用 grab-profiles.sh 抓取运行中 Kubernetes 组件的 CPU 与内存 profile【免费下载链接】kubernetesProduction-Grade Container Scheduling and Management项目地址: https://gitcode.com/GitHub_Trending/kuber/kubernetes当运行中的 Kubernetes 集群里某个组件 CPU 占用偏高或内存持续增长时你可以用 Kubernetes 仓库自带的 hack/grab-profiles.sh 脚本抓取该组件的 pprof 数据CPU profile 和 heap profile并直接用go tool pprof渲染成 PDF拿到带函数级符号的调用图。脚本支持 four 类组件apiservermaster、kubelet、kube-scheduler、kube-controller-manager。适用前提脚本通过gcloud compute ssh建立到目标机器的端口转发隧道、并固定转发到目标机的localhost:8080因此本地需要 gcloud CLI目标机的 apiserver 需监听 8080 端口。脚本的工作方式脚本本身不直接访问组件它的执行链条是以下行为均来自 hack/grab-profiles.sh 与 hack/lib/util.sh 的源码后台启动gcloud compute ssh server --ssh-flag-nN --ssh-flag-Ltunnel_port:localhost:8080把目标机 8080apiserver转发到本地tunnel_port由环境变量tunnel_port控制默认 1234。轮询http://localhost:tunnel_port/healthz直到返回成功才继续。依次对每个选中组件执行go tool pprof请求 profile 端点输出 PDF 到指定目录。注册EXIT/SIGTERMtrap退出时自动kill掉那条 SSH 隧道进程。脚本开启set -o errexit任何一步命令失败都会立即终止不会继续处理后续组件。整个脚本只读取 profile 端点、在本地输出目录写 PDF不会修改集群中的对象。各组件的 pprof 端点路径不同脚本内硬编码的前缀组件pprof 路径前缀经本地隧道访问masterapiserver空直接访问/debug/pprof/...kubelet/api/v1/proxy/nodes/node/proxyscheduler/api/v1/namespaces/kube-system/pods/kube-scheduler/proxy-server:scheduler_portcontroller-manager/api/v1/namespaces/kube-system/pods/kube-controller-manager/proxy-server:controller_manager_port其中--scheduler-port默认 10251--controller-manager-port默认 10252。实际执行的抓取命令是# CPU profile go tool pprof -pdf 组件二进制 http://localhost:tunnel_port路径前缀/debug/pprof/profile # 堆内存 profile每一种 --inuse-*/--alloc-* 选项单独请求一次 go tool pprof -inuse_space -pdf 组件二进制 http://localhost:tunnel_port路径前缀/debug/pprof/heap注意go tool pprof的第一个参数是组件的二进制文件用于符号解析所以本地要有对应组件的二进制本地构建产物或指定现成的路径。准备条件在 Kubernetes 仓库根目录下运行hack/README.md 要求所有hack/脚本从仓库根目录执行。本地已安装 gcloud CLI且gcloud compute ssh server可达目标机器本地安装 Go脚本调用go tool pprof本地安装 curlhack/lib/util.sh中的kube::util::wait_for_url用 curl 轮询未安装会直接报curl must be installed退出。目标机 apiserver 监听 8080。被 profile 组件的二进制文件。对 kubelet脚本默认使用${KUBE_ROOT}/_output/local/bin/linux/amd64/kubelet即本地make构建产物没有构建过就用--kubelet-binary指定其他路径其他组件没有默认值用对应的--master-binary/--scheduler-binary/--controller-manager-binary指定。命令参数hack/grab-profiles.sh --help可列出选项完整的选项集合如下参数说明-s, --server addr必填。目标服务器地址传给gcloud compute ssh-m, --master抓取 apiservermasterprofile-k, --kubelet addresses抓取 kubelet profile值为节点名可给多个--scheduler抓取 kube-scheduler profile-c, --controller-manager抓取 kube-controller-manager profile--cpu请求 CPU profile--inuse-space/--inuse-objects/--alloc-space/--alloc-objects请求 heap profile四种类型各生成一份 PDF-o, --output dir输出目录默认当前目录.--kubelet-binary/--master-binary/--scheduler-binary/--controller-manager-binary传给go tool pprof的组件二进制路径--scheduler-port/--controller-manager-port覆盖默认的 10251 / 10252tunnel_port环境变量本地转发端口默认 1234三个必填项由脚本强制校验缺一个就报错退出没有-s报Server flag is required没选任何组件报Choose at least one component to profile没选任何 profile 类型报Choose at least one profiling option。使用示例下面的示例中server替换为你的目标服务器gcloud compute ssh使用的地址二进制路径替换为你本地的组件二进制位置。抓取 apiserver 的 CPU 与在用内存 profilehack/grab-profiles.sh -s server -m --cpu --inuse-space \ --master-binary kube-apiserver 二进制路径 \ -o ./profiles抓取某节点 kubelet 的 CPU 与分配内存 profilehack/grab-profiles.sh -s server -k node-name --cpu --alloc-space \ --kubelet-binary kubelet 二进制路径 \ -o ./profiles抓取 scheduler 与 controller-manager 的 heap profile需要时加--cpuhack/grab-profiles.sh -s server --scheduler -c --inuse-objects \ --scheduler-binary kube-scheduler 二进制路径 \ --controller-manager-binary kube-controller-manager 二进制路径 \ -o ./profiles同时请求多个内存类型如--inuse-space --inuse-objects --alloc-space --alloc-objects时脚本会为每个类型各发一次/debug/pprof/heap请求并各生成一份 PDF。如何判断抓取成功隧道建立脚本打印Waiting for tunnel to be created...后开始轮询http://localhost:1234/healthz。轮询成功时按hack/lib/util.sh的格式打印On try N, : 响应内容30 次尝试每次间隔 1 秒都失败则报错Timed out waiting for to answer at http://localhost:1234/healthz; tried 30 waiting 1 between each并退出。这一步失败通常说明隧道没通或 8080 不可达先解决它再谈 profile。抓取结果在输出目录下检查 PDF 文件。文件命名为组件名-profile 类型-时间戳.pdf时间戳格式为YYYYmmddHHMMSS脚本用date %Y%m%d%H%M%S生成一次。对应关系master-cpu-profile-时间戳.pdf--cpumaster-mem-inuse_space-profile-时间戳.pdf、master-mem-alloc_objects-profile-时间戳.pdf--inuse-space、--alloc-objects等下划线命名来自脚本内部 flagkubelet 同理为kubelet-cpu-profile-时间戳.pdf等脚本退出时 trap 会自动杀掉后台 SSH 隧道无需手动清理如果中途手动中断Ctrl-CSIGTERMtrap 同样会清理隧道进程。限制与注意事项隧道目标端口固定为localhost:8080无法通过参数改成其他 apiserver 端口如果你的集群 apiserver 不监听 8080脚本不适用。隧道建立硬编码为gcloud compute ssh只适用于可通过 gcloud 访问的机器换一种 SSH 方式需要改动脚本。-k传多个节点时所有节点共用同一个输出前缀和同一次生成的时间戳后抓取的节点会覆盖之前节点的 PDF。需要保留多个节点的结果时逐个节点分别运行脚本即可。请求/debug/pprof/profile会按该端点自身的采样行为占用组件一定时间建议在可接受性能波动的测试/开发集群上做而不是对生产关键组件做。继续分析生成的 PDF 就是go tool pprof -pdf的渲染结果直接打开即可看到函数级的调用与采样信息对同一次运行想换一种视图如文本 top 函数可以用同一条go tool pprof命令把-pdf换成其他输出形式或保存原始 profile 数据后再次分析。脚本入口与相关逻辑可回溯到 hack/grab-profiles.sh 和 hack/lib/util.sh。【免费下载链接】kubernetesProduction-Grade Container Scheduling and Management项目地址: https://gitcode.com/GitHub_Trending/kuber/kubernetes创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考