forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add new file for Pprof tutorial.
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# 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 helpful in finding any sort of bottlenecks that are appearing either at Memory, CPU, or both levels. | ||
|
||
Pprof itself runs 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 is the TCP address that the controller should bind to | ||
// for serving pprof. Specify the manager address and the port that should be bind. | ||
PprofBindAddress: "127.0.0.1:8082", | ||
... | ||
}) | ||
``` | ||
|
||
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) |