|
| 1 | +Monitoring |
| 2 | +---------- |
| 3 | + |
| 4 | +Kubebuilder projects use [controller-runtime](https://sigs.k8s.io/controller-runtime) |
| 5 | +to implement controllers and admission webhooks. `controller-runtime` instruments several key metrics |
| 6 | +related to controllers and webhooks by default using [kubernetes instrumentation guidelines](https://github.com/kubernetes/community/blob/master/contributors/devel/instrumentation.md). |
| 7 | +and makes them available via HTTP endpoint in [prometheus metric format](https://prometheus.io/docs/instrumenting/exposition_formats/). |
| 8 | + |
| 9 | +Following metrics are instrumented by default: |
| 10 | + |
| 11 | + - Total number of reconcilation errors per controller |
| 12 | + - Length of reconcile queue per controller |
| 13 | + - Reconcilation latency |
| 14 | + - Usual resource metrics such as CPU, memory usage, file descriptor usage |
| 15 | + - Go runtime metrics such as number of Go routines, GC duration |
| 16 | + |
| 17 | +{% panel style="info", title="Metrics support" %} |
| 18 | +Please note that metrics support has been added in controller-runtime `0.1.8+` |
| 19 | +release which is the default version for Kubebuilder `1.0.6+` releases. So if your |
| 20 | +project was created using `1.0.5 or older` kubebuilder, then update the |
| 21 | +controller-runtime dependencies to `0.1.8 or higher`. |
| 22 | +{% endpanel %} |
| 23 | + |
| 24 | +To quickly examine metrics in your development environment, you can run the |
| 25 | +following: |
| 26 | + |
| 27 | +```sh |
| 28 | + |
| 29 | +# launch manager |
| 30 | +$ make run |
| 31 | + |
| 32 | +# in another terminal, access the metrics |
| 33 | + |
| 34 | +$ curl http://localhost:8080/metrics |
| 35 | +# HELP controller_runtime_reconcile_errors_total Total number of reconcile errors per controller |
| 36 | +# TYPE controller_runtime_reconcile_errors_total counter |
| 37 | +controller_runtime_reconcile_errors_total{controller="mysql-controller"} 10 |
| 38 | +# HELP controller_runtime_reconcile_queue_length Length of reconcile queue per controller |
| 39 | +# TYPE controller_runtime_reconcile_queue_length gauge |
| 40 | +controller_runtime_reconcile_queue_length{controller="mysql-controller"} 0 |
| 41 | +# HELP controller_runtime_reconcile_time_seconds Length of time per reconcile per controller |
| 42 | +# TYPE controller_runtime_reconcile_time_seconds histogram |
| 43 | +controller_runtime_reconcile_time_seconds_bucket{controller="mysql-controller",le="0.005"} 10 |
| 44 | +controller_runtime_reconcile_time_seconds_bucket{controller="mysql-controller",le="0.01"} 10 |
| 45 | +controller_runtime_reconcile_time_seconds_bucket{controller="mysql-controller",le="0.025"} 10 |
| 46 | +controller_runtime_reconcile_time_seconds_bucket{controller="mysql-controller",le="10"} 10 |
| 47 | +controller_runtime_reconcile_time_seconds_bucket{controller="mysql-controller",le="+Inf"} 10 |
| 48 | +controller_runtime_reconcile_time_seconds_sum{controller="mysql-controller"} 2.3416e-05 |
| 49 | +controller_runtime_reconcile_time_seconds_count{controller="mysql-controller"} 10 |
| 50 | +# HELP go_gc_duration_seconds A summary of the GC invocation durations. |
| 51 | +# TYPE go_gc_duration_seconds summary |
| 52 | +go_gc_duration_seconds{quantile="0"} 7.69e-05 |
| 53 | +go_gc_duration_seconds{quantile="0.25"} 0.0001225 |
| 54 | +go_gc_duration_seconds{quantile="0.5"} 0.000124351 |
| 55 | +go_gc_duration_seconds{quantile="0.75"} 0.000236344 |
| 56 | +go_gc_duration_seconds{quantile="1"} 0.000262102 |
| 57 | +go_gc_duration_seconds_sum 0.000822197 |
| 58 | +go_gc_duration_seconds_count 5 |
| 59 | +# HELP go_goroutines Number of goroutines that currently exist. |
| 60 | +# TYPE go_goroutines gauge |
| 61 | +go_goroutines 39 |
| 62 | +# HELP go_info Information about the Go environment. |
| 63 | +# TYPE go_info gauge |
| 64 | +go_info{version="go1.9.4"} 1 |
| 65 | +# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use. |
| 66 | +..... |
| 67 | +.... |
| 68 | + |
| 69 | +``` |
| 70 | + |
| 71 | +Is the metrics endpoint protected ? |
| 72 | +----------------------------------- |
| 73 | +Yes. By default, kubebuilder generated YAML manifests (under `config/` dir) |
| 74 | +ensures that the access to metrics endpoint is authenticated and authorized using |
| 75 | +an [auth proxy](https://github.com/brancz/kube-rbac-proxy) which is deployed as |
| 76 | +sidecar container in the manager pod. You can read more details about the |
| 77 | +auth proxy based approach [here](https://brancz.com/2018/02/27/using-kube-rbac-proxy-to-secure-kubernetes-workloads/). |
| 78 | + |
| 79 | +If you want to disable the auth proxy, which is not recommended, you can follow |
| 80 | +the instructions in the Kustomization file located in `config/default/kustomization.yaml` |
| 81 | + |
| 82 | +How do I configure Prometheus Server to access the metrics? |
| 83 | +----------------------------------------------------------- |
| 84 | + |
| 85 | +Kubebuilder generated manifests for manager have annotations such as |
| 86 | +`prometheus.io/scrape`, `prometheus.io/path` on the metrics service so |
| 87 | +that it can be easily discovered by the prometheus server deployed in your |
| 88 | +kubernetes cluster. |
| 89 | + |
| 90 | +Assuming auth is enabled, which is by default, you will have to add the |
| 91 | +following to the job which is configured to scrap kubernetes service endpoints. |
| 92 | + |
| 93 | +```yaml |
| 94 | + |
| 95 | +tls_config: |
| 96 | + insecure_skip_verify: true |
| 97 | + |
| 98 | +bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token |
| 99 | + |
| 100 | +``` |
0 commit comments