┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$kubectl trace run --help Execute a bpftrace program on resources
Usage: trace run (POD | TYPE/NAME) [-c CONTAINER] [--attach] [flags]
Examples:
# Count system calls using tracepoints on a specific node kubectl trace run node/kubernetes-node-emt8.c.myproject.internal -e 'kprobe:do_sys_open { printf("%s: %s\n", comm, str(arg1)) }'
# Execute a bpftrace program from file on a specific node kubectl trace run node/kubernetes-node-emt8.c.myproject.internal -f read.bt
# Run an bpftrace inline program on a pod container kubectl trace run pod/nginx -c nginx -e "tracepoint:syscalls:sys_enter_* { @[probe] = count(); }" kubectl trace run pod/nginx nginx -e "tracepoint:syscalls:sys_enter_* { @[probe] = count(); }"
# Run a bpftrace inline program on a pod container with a custom image for the init container responsible to fetch linux headers kubectl trace run pod/nginx nginx -e "tracepoint:syscalls:sys_enter_* { @[probe] = count(); } --init-imagename=quay.io/custom-init-image-name --fetch-headers"
# Run a bpftrace inline program on a pod container with a custom image for the bpftrace container that will run your program in the cluster kubectl trace run pod/nginx nginx -e "tracepoint:syscalls:sys_enter_* { @[probe] = count(); } --imagename=quay.io/custom-bpftrace-image-name" ............... ┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$
┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$kubectl trace run node/vms103.liruilongs.github.io -e 'kprobe:do_sys_open { printf("%s: %s\n", comm, str(arg1)) }' trace a659c18c-e50c-11ee-ba0d-000c290e5d5f created
会启动一个 job 在对应的 节点上
1 2 3 4 5
┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$kubectl get jobs NAME COMPLETIONS DURATION AGE kubectl-trace-a659c18c-e50c-11ee-ba0d-000c290e5d5f 0/1 20s 20s webhook-cert-setup 0/1 124d 124d
获取 pod 信息
1 2 3 4 5 6
┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$kubectl get jobs | grep a659c18c-e50c-11ee-ba0d-000c290e5d5f kubectl-trace-a659c18c-e50c-11ee-ba0d-000c290e5d5f 0/1 29s 29s ┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$kubectl get pods | grep a659c18c-e50c-11ee-ba0d-000c290e5d5f kubectl-trace-a659c18c-e50c-11ee-ba0d-000c290e5d5f-7292n 1/1 Running 0 65s
┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$kubectl trace run vms105.liruilongs.github.io -e "tracepoint:syscalls:sys_enter_execve { @[comm] = count() }" --fetch-headers trace 305a7d60-e5a0-11ee-ba88-000c290e5d5f created ┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$kubectl get pods -w NAME READY STATUS RESTARTS AGE hello-webhook-deployment-7f599b95c4-hjx86 1/1 Running 1 (2d16h ago) 18d kubectl-trace-305a7d60-e5a0-11ee-ba88-000c290e5d5f-ctfl6 0/1 Init:0/1 0 4s kubectl-trace-305a7d60-e5a0-11ee-ba88-000c290e5d5f-ctfl6 0/1 PodInitializing 0 15s kubectl-trace-305a7d60-e5a0-11ee-ba88-000c290e5d5f-ctfl6 1/1 Running 0 21s ┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$
查看日志信息
1 2 3 4 5
┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$kubectl logs kubectl-trace-3d9981a0-e59b-11ee-a570-000c290e5d5f-wwprt Defaulted container "kubectl-trace-3d9981a0-e59b-11ee-a570-000c290e5d5f" out of: kubectl-trace-3d9981a0-e59b-11ee-a570-000c290e5d5f, kubectl-trace-init (init) if your program has maps to print, send a SIGINT using Ctrl-C, if you want to interrupt the execution send SIGINT two times Attaching 1 probe...
┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$kubectl logs kubectl-trace-305a7d60-e5a0-11ee-ba88-000c290e5d5f-ctfl6 -f Defaulted container "kubectl-trace-305a7d60-e5a0-11ee-ba88-000c290e5d5f" out of: kubectl-trace-305a7d60-e5a0-11ee-ba88-000c290e5d5f, kubectl-trace-init (init) if your program has maps to print, send a SIGINT using Ctrl-C, if you want to interrupt the execution send SIGINT two times Attaching 1 probe...
┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$cat opensnoop.bt #!/usr/bin/bpftrace /* * opensnoop Trace open() syscalls. * For Linux, uses bpftrace and eBPF. * * Also a basic example of bpftrace. * * USAGE: opensnoop.bt * * This is a bpftrace version of the bcc tool of the same name. * * Copyright 2018 Netflix, Inc. * Licensed under the Apache License, Version 2.0 (the "License") * * 08-Sep-2018 Brendan Gregg Created this. */
BEGIN { printf("Tracing open syscalls... Hit Ctrl-C to end.\n"); printf("%-6s %-16s %4s %3s %s\n", "PID", "COMM", "FD", "ERR", "PATH"); }
┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$kubectl trace run node/vms103.liruilongs.github.io -f opensnoop.bt trace fc86c785-e513-11ee-8e04-000c290e5d5f created
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$kubectl get pods | grep fc86c785-e513-11ee-8e04-000c290e5d5f kubectl-trace-fc86c785-e513-11ee-8e04-000c290e5d5f-g67gm 0/1 ContainerCreating 0 8s ┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$kubectl get pods kubectl-trace-fc86c785-e513-11ee-8e04-000c290e5d5f-g67gm -w NAME READY STATUS RESTARTS AGE kubectl-trace-fc86c785-e513-11ee-8e04-000c290e5d5f-g67gm 0/1 Completed 0 19s kubectl-trace-fc86c785-e513-11ee-8e04-000c290e5d5f-g67gm 0/1 Completed 0 20s kubectl-trace-fc86c785-e513-11ee-8e04-000c290e5d5f-g67gm 0/1 Completed 0 21s kubectl-trace-fc86c785-e513-11ee-8e04-000c290e5d5f-g67gm 0/1 Terminating 0 26s kubectl-trace-fc86c785-e513-11ee-8e04-000c290e5d5f-g67gm 0/1 Terminating 0 26s ^C┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$ ┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$kubectl logs kubectl-trace-fc86c785-e513-11ee-8e04-000c290e5d5f-g67gm Error from server (NotFound): pods "kubectl-trace-fc86c785-e513-11ee-8e04-000c290e5d5f-g67gm" not found
日志信息
1 2
if your program has maps to print, send a SIGINT using Ctrl-C, if you want to interrupt the execution send SIGINT two ││ times │ │/bpftrace/include/clang_workarounds.h:14:10: fatal error: 'linux/types.h' file not found ││exit status 1
执行环境
1 2 3 4 5 6 7 8 9 10 11 12
┌──[root@vms100.liruilongs.github.io]-[~/ansible/trace] └─$hostnamectl Static hostname: vms100.liruilongs.github.io Icon name: computer-vm Chassis: vm Machine ID: e93ae3f6cb354f3ba509eeb73568087e Boot ID: 51ad5f1933914654affd2dcf9ebca862 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 5.4.266-1.el7.elrepo.x86_64 Architecture: x86-64