diff --git a/docs/book/src/cronjob-tutorial/images/pprof-result-visualization.png b/docs/book/src/cronjob-tutorial/images/pprof-result-visualization.png new file mode 100644 index 00000000000..a50fd1e4064 Binary files /dev/null and b/docs/book/src/cronjob-tutorial/images/pprof-result-visualization.png differ diff --git a/docs/book/src/cronjob-tutorial/pprof-tutorial.md b/docs/book/src/cronjob-tutorial/pprof-tutorial.md new file mode 100644 index 00000000000..7cb5edb0280 --- /dev/null +++ b/docs/book/src/cronjob-tutorial/pprof-tutorial.md @@ -0,0 +1,59 @@ +# Using Pprof for Controller Profiling + +Pprof is a Go based library designed for profiling the binary in order to gather CPU, and Memory related data. Pprof is help in finding any sort of bottle necks that are either appearing at Memory, CPU, or both levels. + +Pprof itself runs as a tiny HTTP server as part of the the Controller. You can extract the statistics, and visualize them on an interactive website. Furthermore, **you don't need to install this package seperately**. Pprof comes packaged as part of Kubebuilder `controller-runtime` [dependency](https://github.com/kubernetes-sigs/controller-runtime/pull/1943) within the scafolded project. + +For further information about the Pprof usage, check out the official [docs](https://github.com/google/pprof). + +# How to use Pprof in your Codebase + +1. In your `cmd/main.go` file, specify the `PprofBindAddress` URL field as part of Controller Manager's configurations: + + ```golang + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ + .... + Scheme: scheme, + LeaderElection: enableLeaderElection, + PprofBindAddress: "127.0.0.1:8082", // Specify your own URL here. + ... + }) + ``` + +2. Reinstall the Controller binary. + + ```bash + make install + ``` + +3. Start your controller on your localhost: + + ```bash + export ENABLE_WEBHOOKS=false # If you're not using a Webhook. Else, set it to true. + make run + ``` + +4. Deploy your custom resource on Kind, or any other Kubernetes cluster. Wait for the pods to be ready. + + ```bash + kubectl apply -f batch_v1_cronjob.yaml + ``` + +5. Use `curl` to export the profiling statistics into a file. Note that the **URL hit should be same as the one specified in the `PprofBindAddress` field of your Controller's Manager**. + + ```bash + curl -s "http://127.0.0.1:8082/debug/pprof/profile" > ./cpu-profile.out + ``` + +6. Visualize the file on an interactive dashboard. + + ```bash + # Go tool will open a session on port 8080. + # You can change this as per your own need. + go tool pprof -http=:8080 ./cpu-profile.out + ``` + + Visualizaion resutl will vary depending on the deployed workload, and the Controller's behavior. + However, you'll see a dashboard on your browser similar to this one: + + ![pprof-result-visualization](./images/pprof-result-visualization.png)