Skip to content

Monitoring

The operator exposes Prometheus metrics about every ManagedResource (status, errors, reconcile durations), along with the standard metrics of its controllers and of the Go process. This page covers how to scrape them, what they mean, example alerts and a ready-made Grafana dashboard.

Available from v0.16.0

The kubensync_* metrics, the prometheus.labels chart value and the Grafana dashboard need v0.16.0 or later. Earlier versions expose only the standard metrics, and the ServiceMonitor of their chart does not select the metrics Service, so Prometheus finds no target.

Metrics endpoint

The operator serves its metrics over HTTPS on port 8443, at /metrics, through the kubensync-controller-manager-metrics-service Service. Requests must carry a token allowed to get the /metrics non-resource URL; the Helm chart creates the kubensync-metrics-reader ClusterRole for that. The operator serves a self-signed certificate it generates at startup; with certmanager.enable, cert-manager issues the certificate and the chart's ServiceMonitor verifies it.

To look at the metrics by hand, bind that role to a ServiceAccount and use its token:

kubectl -n kubensync-system create serviceaccount metrics-check
kubectl create clusterrolebinding kubensync-metrics-check \
  --clusterrole=kubensync-metrics-reader --serviceaccount=kubensync-system:metrics-check

kubectl -n kubensync-system port-forward svc/kubensync-controller-manager-metrics-service 8443:8443 &
curl -sk -H "Authorization: Bearer $(kubectl -n kubensync-system create token metrics-check)" \
  https://localhost:8443/metrics | grep '^kubensync_'

Note

Only the leader reconciles, so only the leader reports the kubensync_managedresource_* metrics. With more than one replica, port-forward to the leader pod, the one named in kubectl -n kubensync-system get lease 07c33303.kubensync.com -o jsonpath='{.spec.holderIdentity}'.

Scraping with the Prometheus Operator

The chart can create a ServiceMonitor. Its labels must match the serviceMonitorSelector of your Prometheus; with kube-prometheus-stack that is the release label:

prometheus:
  enable: true
  labels:
    release: kube-prometheus-stack # (1)!
  1. Info

    Use the name of your kube-prometheus-stack release, or whatever labels your Prometheus selects ServiceMonitors by.

The service account Prometheus scrapes with also needs to get /metrics. kube-prometheus-stack already grants it; otherwise bind the kubensync-metrics-reader ClusterRole to that service account.

Warning

With networkPolicy.enable: true, only pods in namespaces labelled metrics: enabled can reach the metrics port. Label the namespace Prometheus runs in.

Metrics

All kubensync metrics carry the kubensync_ prefix. Metrics about a single ManagedResource have a managedresource label with its name.

  • kubensync_managedresource_status (gauge, labels managedresource and status): 1 for the current status of the ManagedResource, 0 for the other two.
  • kubensync_managedresource_namespaces (gauge, label managedresource): namespaces the namespaceSelector currently selects. 0 when the selector is invalid.
  • kubensync_managedresource_created_resources (gauge, label managedresource): resources listed in status.createdResources.
  • kubensync_managedresource_reconcile_duration_seconds (histogram, labels managedresource and result): duration of the reconciles of the ManagedResource across all its namespaces. result is success or error, and the _count series gives the number of reconciles. Namespace events, which only reconcile the namespace that changed, are not included.
  • kubensync_managedresource_errors_total (counter, labels managedresource and stage): reconcile errors, by the stage they came from.
  • kubensync_managedresource_delete_failures_total (counter, label managedresource): failed deletes of resources the ManagedResource no longer renders. They stay tracked and are retried on the next reconcile.
  • kubensync_data_watches (gauge): objects watched because a ManagedResource references them in template.data with watch: true. Each one is a watch on the API server.
  • kubensync_build_info (gauge, label version): always 1, labelled with the version of the running operator.

Statuses

kubensync_managedresource_status follows the Ready condition:

  • synced: Ready is True for the current spec.
  • failed: Ready is False, the last reconcile failed in some or all of the namespaces.
  • unprocessed: the current spec has no result yet. The ManagedResource is new, or was edited and has not been reconciled since.

Error stages

The stage label of kubensync_managedresource_errors_total tells where an error happened:

  • namespaces: listing namespaces or evaluating the namespaceSelector, for example an invalid regex.
  • data: reading the resources referenced in template.data.
  • template: parsing or rendering the template, or decoding the rendered YAML.
  • mapping: resolving the kind of a rendered resource, for example a kind whose CRD is not installed.
  • apply: reading, creating or updating a rendered resource, for example when the operator lacks permissions.
  • status: writing the status of the ManagedResource.
  • other: errors none of the stages above claims.

Note

kubensync_managedresource_errors_total counts one error per failing namespace and per attempt, both in reconciles of the ManagedResource and in the passes namespace events trigger. A failing ManagedResource is retried with exponential backoff, and so is every namespace it fails in, so the counter of a broken ManagedResource grows quickly and can exceed its number of reconciles. Alert on kubensync_managedresource_status, and use the errors to find out why it fails.

Note

The time a reconcile waits for another one to finish is not part of kubensync_managedresource_reconcile_duration_seconds: reconciles run one at a time.

Standard metrics

The operator also exposes the standard controller-runtime metrics:

  • controller_runtime_reconcile_*: reconciles, errors and durations of the managedresource and namespace controllers.
  • workqueue_*: depth, latency and retries of their work queues.
  • rest_client_requests_total: requests to the Kubernetes API by method and status code.
  • leader_election_master_status: 1 on the leader.
  • go_* and process_*: memory, CPU, goroutines and garbage collection of the operator process. The go_godebug_* metrics are only exposed when a GODEBUG setting differs from its default.

Useful queries

# ManagedResources by status
sum by (status) (kubensync_managedresource_status)

# Failing ManagedResources
kubensync_managedresource_status{status="failed"} == 1

# Errors per second by ManagedResource and stage
sum by (managedresource, stage) (rate(kubensync_managedresource_errors_total[5m]))

# 95th percentile reconcile duration by ManagedResource
histogram_quantile(0.95, sum by (managedresource, le) (rate(kubensync_managedresource_reconcile_duration_seconds_bucket[30m])))

Alerts

A starting point for alerts, as a PrometheusRule for the Prometheus Operator. Like ServiceMonitors, its labels must match the ruleSelector of your Prometheus.

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: kubensync
  namespace: kubensync-system
  labels:
    release: kube-prometheus-stack
spec:
  groups:
  - name: kubensync
    rules:
    - alert: KubensyncManagedResourceFailed
      expr: max by (managedresource) (kubensync_managedresource_status{status="failed"}) == 1
      for: 15m
      labels:
        severity: warning
      annotations:
        summary: ManagedResource {{ $labels.managedresource }} fails to reconcile.
        description: Its Ready condition names the failing namespaces and errors.
    - alert: KubensyncManagedResourceUnprocessed
      expr: max by (managedresource) (kubensync_managedresource_status{status="unprocessed"}) == 1
      for: 30m
      labels:
        severity: warning
      annotations:
        summary: ManagedResource {{ $labels.managedresource }} has not been reconciled for 30 minutes.
    - alert: KubensyncDeleteFailures
      expr: sum by (managedresource) (increase(kubensync_managedresource_delete_failures_total[30m])) > 0
      labels:
        severity: warning
      annotations:
        summary: ManagedResource {{ $labels.managedresource }} cannot delete resources it no longer renders.
        description: The operator logs show which resources and why.
    - alert: KubensyncManagedResourceSelectsNoNamespace
      expr: max by (managedresource) (kubensync_managedresource_namespaces) == 0
      for: 1h
      labels:
        severity: info
      annotations:
        summary: ManagedResource {{ $labels.managedresource }} selects no namespace.
    - alert: KubensyncOperatorMetricsMissing
      expr: absent(kubensync_build_info)
      for: 10m
      labels:
        severity: critical
      annotations:
        summary: No kubensync operator is being scraped.

Grafana dashboard

A dashboard is available in the repository at dist/grafana/kubensync-dashboard.json. It shows:

  • Overview: scraped pods, leader, restarts, reconcile rate, error ratio and queue depth.
  • ManagedResources: how many are synced, failed and unprocessed, a table with the status, namespaces, resources, errors and durations of each one, their status over time, errors by stage and the slowest ones.
  • Reconciliation, Work queue and Kubernetes API: the controller-runtime metrics of both controllers.
  • Resources: CPU, memory, garbage collection, goroutines, network and file descriptors of the operator.
  • Leader election: which pod led when, and how many times leadership changed.

Import it in Grafana from Dashboards > New > Import, or, with the dashboard sidecar of kube-prometheus-stack, load it from a ConfigMap:

curl -sLO https://raw.githubusercontent.com/eryalito/kubensync-operator/master/dist/grafana/kubensync-dashboard.json
kubectl -n monitoring create configmap kubensync-dashboard --from-file=kubensync-dashboard.json
kubectl -n monitoring label configmap kubensync-dashboard grafana_dashboard=1

Info

The dashboard asks for a Prometheus data source, and filters by job, namespace, pod, controller and ManagedResource. The CPU and memory panels also show the requests and limits of the operator when kube-state-metrics is available.