|  | 
|  | 1 | +## Metrics | 
|  | 2 | + | 
|  | 3 | +By default, controller-runtime builds a global prometheus registry and | 
|  | 4 | +publishes a collection of performance metrics for each controller. | 
|  | 5 | + | 
|  | 6 | +### Protecting the Metrics | 
|  | 7 | + | 
|  | 8 | +These metrics are protected by [kube-auth-proxy](https://github.com/brancz/kube-rbac-proxy) | 
|  | 9 | +by default. | 
|  | 10 | + | 
|  | 11 | +You will need to grant permissions to your Prometheus server so that it can | 
|  | 12 | +scrape the protected metrics. To achieve that, you can create a `clusterRole` and a  | 
|  | 13 | +`clusterRoleBinding` to bind to the service account that your Prometheus server uses. | 
|  | 14 | + | 
|  | 15 | +Create a YAML file named `capi-metrics-reader-clusterrole.yaml` with following content | 
|  | 16 | + | 
|  | 17 | +```yaml | 
|  | 18 | +apiVersion: rbac.authorization.k8s.io/v1 | 
|  | 19 | +kind: ClusterRole | 
|  | 20 | +metadata: | 
|  | 21 | +  name: capi-metrics-reader | 
|  | 22 | +rules: | 
|  | 23 | +- nonResourceURLs: ["/metrics"] | 
|  | 24 | +  verbs: ["get"] | 
|  | 25 | +``` | 
|  | 26 | +
 | 
|  | 27 | +and apply the `clusterRole` with | 
|  | 28 | + | 
|  | 29 | +```bash | 
|  | 30 | +kubectl apply -f capi-metrics-reader-clusterrole.yaml | 
|  | 31 | +``` | 
|  | 32 | + | 
|  | 33 | +You can run the following kubectl command to create a `clusterRoleBinding` and grant access on the `/metrics` endpoint to your Prometheus instance (`<namespace>` must be the namespace where your Prometheus instance is running. `<service-account-name>` must be the service account name which is configured in your Prometheus instance). | 
|  | 34 | + | 
|  | 35 | +```bash | 
|  | 36 | +kubectl create clusterrolebinding capi-metrics-reader --clusterrole=capi-metrics-reader --serviceaccount=<namespace>:<service-account-name> | 
|  | 37 | +``` | 
|  | 38 | + | 
|  | 39 | +### Scraping the Metrics with Prometheus | 
|  | 40 | + | 
|  | 41 | +To scrape metrics, your Prometheus instance need at least following [`kubernetes_sd_config`](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config) section. | 
|  | 42 | + | 
|  | 43 | + | 
|  | 44 | +```yaml | 
|  | 45 | +      # This job is primarily used for Pods with multiple metrics port. | 
|  | 46 | +      # Per port one service is created and scraped. | 
|  | 47 | +      - job_name: 'kubernetes-service-endpoints' | 
|  | 48 | +        tls_config: | 
|  | 49 | +          # if service endpoints use their own CA (e.g. via cert-manager) which aren't | 
|  | 50 | +          # signed by the cluster-internal CA we must skip the cert validation | 
|  | 51 | +          insecure_skip_verify: true | 
|  | 52 | +        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token | 
|  | 53 | +        kubernetes_sd_configs: | 
|  | 54 | +          - role: endpoints | 
|  | 55 | +        relabel_configs: | 
|  | 56 | +          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape] | 
|  | 57 | +            action: keep | 
|  | 58 | +            regex: true | 
|  | 59 | +          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme] | 
|  | 60 | +            action: replace | 
|  | 61 | +            target_label: __scheme__ | 
|  | 62 | +            regex: (https?) | 
|  | 63 | +          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path] | 
|  | 64 | +            action: replace | 
|  | 65 | +            target_label: __metrics_path__ | 
|  | 66 | +            regex: (.+) | 
|  | 67 | +          - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port] | 
|  | 68 | +            action: replace | 
|  | 69 | +            target_label: __address__ | 
|  | 70 | +            regex: ([^:]+)(?::\d+)?;(\d+) | 
|  | 71 | +            replacement: $1:$2 | 
|  | 72 | +          - action: labelmap | 
|  | 73 | +            regex: __meta_kubernetes_service_label_(.+) | 
|  | 74 | +``` | 
|  | 75 | + | 
|  | 76 | +You are no able to check for metrics in your Prometheus instance. To verify, you could search with e.g. `{namespace="capi-system"}` to get all metrics from components running in `capi-system` Namespace. | 
0 commit comments