Rancher 通过匿名导入 Go 标准库 net/http/pprof,并使用默认 HTTP mux 监听 127.0.0.1:6060。
Profile 文件可用于性能分析。当 Rancher 出现性能问题时,例如 CPU 使用率异常高、内存持续上涨、Goroutine 数量异常增加等,可以通过以下方式收集 Profile 文件进行分析。
CPU Profile
CPU Profile 用于采集一段时间内的 CPU 使用情况,默认采集 30 秒,适合用于排查 CPU 使用率异常高的问题。
1
| GET /debug/pprof/profile
|
请求示例:
1 2
| curl -fsS 'http://127.0.0.1:6060/debug/pprof/profile?seconds=30' \ --output /tmp/$HOSTNAME-cpu-30s.pb.gz
|
一条命令获取所有 Rancher Pod 的 CPU Profile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ts=$(date +%Y%m%d-%H%M%S); out="rancher-cpu-${ts}"; mkdir -p "$out";
kubectl -n cattle-system get pods \ -l app=rancher \ --field-selector=status.phase=Running \ -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | while IFS= read -r pod; do echo "Collecting CPU Profile from $pod" kubectl -n cattle-system exec "$pod" -c rancher -- \ curl -fsS 'http://127.0.0.1:6060/debug/pprof/profile?seconds=30' \ > "$out/${pod}-cpu-30s-${ts}.pb.gz" || echo "Failed: $pod" >&2 done
|
Heap Profile
Heap Profile 用于查看堆内存分配情况,适合用于排查内存泄漏、内存持续上涨等问题。
请求示例:
1 2
| curl -fsS 'http://127.0.0.1:6060/debug/pprof/heap' \ --output /tmp/$HOSTNAME-heap.pb.gz
|
一条命令获取所有 Rancher Pod 的 Heap Profile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ts=$(date +%Y%m%d-%H%M%S); out="rancher-heap-${ts}"; mkdir -p "$out";
kubectl -n cattle-system get pods \ -l app=rancher \ --field-selector=status.phase=Running \ -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | while IFS= read -r pod; do echo "Collecting Heap Profile from $pod" kubectl -n cattle-system exec "$pod" -c rancher -- \ curl -fsS http://127.0.0.1:6060/debug/pprof/heap \ > "$out/${pod}-heap-${ts}.pb.gz" || echo "Failed: $pod" >&2 done
|
Goroutine Profile
Goroutine Profile 用于查看所有 Goroutine 的调用栈,常用于分析 Goroutine 泄漏、Goroutine 数量持续增加、Goroutine 阻塞在同一位置、Channel / 锁 / 网络或 I/O 阻塞、deadlock、请求堆积等问题。
1
| GET /debug/pprof/goroutine
|
请求示例:
1 2
| curl -fsS 'http://127.0.0.1:6060/debug/pprof/goroutine' \ --output /tmp/$HOSTNAME-goroutine.pb.gz
|
一条命令获取所有 Rancher Pod 的 Goroutine Profile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ts=$(date +%Y%m%d-%H%M%S); out="rancher-goroutine-${ts}"; mkdir -p "$out";
kubectl -n cattle-system get pods \ -l app=rancher \ --field-selector=status.phase=Running \ -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | while IFS= read -r pod; do echo "Collecting Goroutine Profile from $pod" kubectl -n cattle-system exec "$pod" -c rancher -- \ curl -fsS http://127.0.0.1:6060/debug/pprof/goroutine \ > "$out/${pod}-goroutine-${ts}.pb.gz" || echo "Failed: $pod" >&2 done
|
Allocations Profile
Allocations Profile 用于显示程序启动以来累计发生的内存分配采样,适合用于排查 GC 压力大、内存分配频繁、当前内存不高但 CPU 大量消耗在 GC、高频短生命周期对象等问题。
请求示例:
1 2
| curl -fsS 'http://127.0.0.1:6060/debug/pprof/allocs' \ --output /tmp/$HOSTNAME-allocs.pb.gz
|
一条命令获取所有 Rancher Pod 的 Allocations Profile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ts=$(date +%Y%m%d-%H%M%S); out="rancher-allocs-${ts}"; mkdir -p "$out";
kubectl -n cattle-system get pods \ -l app=rancher \ --field-selector=status.phase=Running \ -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | while IFS= read -r pod; do echo "Collecting Allocations Profile from $pod" kubectl -n cattle-system exec "$pod" -c rancher -- \ curl -fsS http://127.0.0.1:6060/debug/pprof/allocs \ > "$out/${pod}-allocs-${ts}.pb.gz" || echo "Failed: $pod" >&2 done
|