|
| 1 | +# Grafana Plugin (`grafana/v1-alpha`) |
| 2 | + |
| 3 | +The Grafana plugin is an optional plugin that can be used to scaffold Grafana Dashboards to allow you to check out the default metrics which are exported by projects using [controller-runtime][controller-runtime]. |
| 4 | + |
| 5 | +<aside class="note"> |
| 6 | +<h1>Examples</h1> |
| 7 | + |
| 8 | +You can check its default scaffold by looking at the `project-v3-with-grafana` projects under the [testdata][testdata] directory on the root directory of the Kubebuilder project. |
| 9 | + |
| 10 | +</aside> |
| 11 | + |
| 12 | +## When to use it ? |
| 13 | + |
| 14 | +- If you are looking to observe the metrics exported by [controller metrics][controller-metrics] and collected by Prometheus via [Grafana][grafana]. |
| 15 | + |
| 16 | +## How to use it ? |
| 17 | + |
| 18 | +### Prerequisites: |
| 19 | + |
| 20 | +- Your project must be using [controller-runtime][controller-runtime] to expose the metrics via the [controller default metrics][controller-metrics] and they need to be collected by Prometheus. |
| 21 | +- Access to [Prometheus][prometheus]. |
| 22 | + - Prometheus should have an endpoint exposed. (For `prometheus-operator`, this is similar as: http://prometheus-k8s.monitoring.svc:9090 ) |
| 23 | + - The endpoint is ready to/already become the datasource of your Grafana. See [Add a data source](https://grafana.com/docs/grafana/latest/datasources/add-a-data-source/) |
| 24 | +- Access to [Grafana](https://grafana.com/docs/grafana/latest/setup-grafana/installation/). Make sure you have: |
| 25 | + - [Dashboard edit permission](https://grafana.com/docs/grafana/next/administration/roles-and-permissions/#dashboard-permissions) |
| 26 | + - Prometheus Data source |
| 27 | +  |
| 28 | + |
| 29 | +<aside class="note"> |
| 30 | + |
| 31 | +Check the [metrics][reference-metrics-doc] to know how to enable the metrics for your projects scaffold with Kubebuilder. |
| 32 | + |
| 33 | +See that in the [config/prometheus][kustomize-plugin] you will find the ServiceMonitor to enable the metrics in the default endpoint `/metrics`. |
| 34 | + |
| 35 | +</aside> |
| 36 | + |
| 37 | +### Basic Usage |
| 38 | + |
| 39 | +The Grafana plugin is attached to the `init` subcommand and the `edit` subcommand: |
| 40 | + |
| 41 | +```sh |
| 42 | +# Initialize a new project with grafana plugin |
| 43 | +kubebuilder init --plugins grafana.kubebuilder.io/v1-alpha |
| 44 | + |
| 45 | +# Enable grafana plugin to an existing project |
| 46 | +kubebuilder edit --plugins grafana.kubebuilder.io/v1-alpha |
| 47 | +``` |
| 48 | + |
| 49 | +The plugin will create a new directory and scaffold the JSON files under it (i.e. `grafana/controller-runtime-metrics.json`). |
| 50 | + |
| 51 | +#### Show case: |
| 52 | + |
| 53 | +See an example of how to use the plugin in your project: |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +#### Now, let's check how to use the Grafana dashboards |
| 58 | + |
| 59 | +1. Copy the JSON file |
| 60 | +2. Visit `<your-grafana-url>/dashboard/import` to [import a new dashboard](https://grafana.com/docs/grafana/latest/dashboards/export-import/#import-dashboard). |
| 61 | +3. Paste the JSON content to `Import via panel json`, then press `Load` button |
| 62 | + <img width="644" alt="Screen Shot 2022-06-28 at 3 40 22 AM" src="https://user-images.githubusercontent.com/18136486/176121955-1c4aec9c-0ba4-4271-9767-e8d1726d9d9a.png"> |
| 63 | +4. Select the data source for Prometheus metrics |
| 64 | + <img width="633" alt="Screen Shot 2022-06-28 at 3 41 26 AM" src="https://user-images.githubusercontent.com/18136486/176122261-e3eab5b0-9fc4-45fc-a68c-d9ce1cfe96ee.png"> |
| 65 | +5. Once the json is imported in Grafana, the dashboard is ready. |
| 66 | + |
| 67 | +### Grafana Dashboard |
| 68 | + |
| 69 | +#### Controller Runtime Reconciliation total & errors |
| 70 | + |
| 71 | +- Metrics: |
| 72 | + - controller_runtime_reconcile_total |
| 73 | + - controller_runtime_reconcile_errors_total |
| 74 | +- Query: |
| 75 | + - sum(rate(controller_runtime_reconcile_total{job="$job"}[5m])) by (instance, pod) |
| 76 | + - sum(rate(controller_runtime_reconcile_errors_total{job="$job"}[5m])) by (instance, pod) |
| 77 | +- Description: |
| 78 | + - Per-second rate of total reconciliation as measured over the last 5 minutes |
| 79 | + - Per-second rate of reconciliation errors as measured over the last 5 minutes |
| 80 | + |
| 81 | +<img width="1430" alt="Screen Shot 2022-06-28 at 3 43 10 AM" src="https://user-images.githubusercontent.com/18136486/176122555-f3493658-6c99-4ad6-a9b7-63d85620d370.png"> |
| 82 | + |
| 83 | +## Subcommands |
| 84 | + |
| 85 | +The Grafana plugin implements the following subcommands: |
| 86 | + |
| 87 | +- edit (`$ kubebuilder edit [OPTIONS]`) |
| 88 | + |
| 89 | +- init (`$ kubebuilder init [OPTIONS]`) |
| 90 | + |
| 91 | +## Affected files |
| 92 | + |
| 93 | +The following scaffolds will be created or updated by this plugin: |
| 94 | + |
| 95 | +- `grafana/*.json` |
| 96 | + |
| 97 | +## Further resources |
| 98 | + |
| 99 | +- Refer to a sample of `servicemonitor` provided by [kustomize plugin][kustomize-plugin] |
| 100 | +- Check the [plugin implementation][plugin-implementation] |
| 101 | +- [Grafana Docs][grafana-docs] of importing JSON file |
| 102 | +- The usage of servicemonitor by [Prometheus Operator][servicemonitor] |
| 103 | + |
| 104 | +[controller-metrics]: https://book.kubebuilder.io/reference/metrics-reference.html |
| 105 | +[controller-runtime]: https://github.com/kubernetes-sigs/controller-runtime |
| 106 | +[grafana]: https://grafana.com/docs/grafana/next/ |
| 107 | +[grafana-docs]: https://grafana.com/docs/grafana/latest/dashboards/export-import/#import-dashboard |
| 108 | +[kustomize-plugin]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v3/config/prometheus/monitor.yaml |
| 109 | +[kube-prometheus]: https://github.com/prometheus-operator/kube-prometheus |
| 110 | +[plugin-implementation]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/pkg/plugins/optional/grafana/alphav1 |
| 111 | +[prometheus]: https://prometheus.io/docs/introduction/overview/ |
| 112 | +[prom-operator]: https://prometheus-operator.dev/docs/prologue/introduction/ |
| 113 | +[reference-metrics-doc]: https://book.kubebuilder.io/reference/metrics.html#exporting-metrics-for-prometheus |
| 114 | +[servicemonitor]: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/user-guides/getting-started.md#related-resources |
| 115 | +[testdata]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/testdata |
0 commit comments