All guides

Monitor Kubernetes with kube-prometheus-stack

The kube-prometheus-stack chart installs the Prometheus Operator, Prometheus, Alertmanager, Grafana, kube-state-metrics and the node exporter as one release. Four of its defaults need a decision: the selector label, the CRDs, the storage and the control plane targets.

A pair of glasses in front of two screens showing code

What the chart installs

The chart's README says it installs the core components of the kube-prometheus stack. Rendering chart version 93.9.0 with helm template shows what that means:

  • the Prometheus Operator, with its custom resource definitions (CRDs), such as Prometheus, Alertmanager, ServiceMonitor, PodMonitor and PrometheusRule
  • one Prometheus object and one Alertmanager object, each with one replica. The operator deploys a StatefulSet for each.
  • kube-state-metrics, the node exporter and Grafana, as dependent charts that you can switch off with kubeStateMetrics.enabled, nodeExporter.enabled and grafana.enabled
  • Grafana dashboards, and default alerting and recording rules
  • ServiceMonitors for the kubelet, the API server, CoreDNS, kube-proxy, the controller manager, the scheduler and etcd

The README adds that the chart does not install the Prometheus Adapter or the blackbox exporter, and that the operator does not support annotation-based discovery such as prometheus.io/scrape: it uses ServiceMonitor and PodMonitor objects.

Install the chart

The chart is published as an OCI artifact and in the Helm repository at https://prometheus-community.github.io/helm-charts. Its README installs from the OCI registry:

Shell

helm show values oci://ghcr.io/prometheus-community/charts/kube-prometheus-stack

helm install monitoring \
  oci://ghcr.io/prometheus-community/charts/kube-prometheus-stack \
  --namespace monitoring --create-namespace \
  --version <chart-version> \
  --values values.yaml

Pin the version. The README says a major version change indicates an incompatible breaking change that needs manual actions. monitoring is the release name and namespace chosen for this page. The chart uses the release name as a label selector.

A ServiceMonitor that gets selected

YAML

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: example-app
  labels:
    team: frontend
    release: monitoring
spec:
  selector:
    matchLabels:
      app: example-app
  endpoints:
  - port: web

This is the ServiceMonitor from the Prometheus Operator's getting-started guide with one label added, release: monitoring. It validates against the ServiceMonitor CRD schema that the chart ships. selector picks Service objects by label, in the ServiceMonitor's own namespace by default. port is the name of the Service port.

The operator's guide says the Prometheus object defines which ServiceMonitors get selected, in spec.serviceMonitorSelector. With the chart's default, serviceMonitorSelectorNilUsesHelmValues: true, the rendered Prometheus object selects matchLabels of release: monitoring. A ServiceMonitor that lacks the label does not match. The README gives the other way out, two values set to false:

YAML

prometheus:
  prometheusSpec:
    serviceMonitorSelectorNilUsesHelmValues: false
    podMonitorSelectorNilUsesHelmValues: false

With them, helm template renders serviceMonitorSelector: {}, which the values file describes as selecting all ServiceMonitors.

On namespaces the chart's own files disagree. The README says Prometheus discovers ServiceMonitors within its namespace. The values file sets serviceMonitorNamespaceSelector: {}, the rendered Prometheus object carries it, and the operator's API reference says an empty selector matches all namespaces.

CRDs on upgrade

The chart's README says Helm v3 leaves the CRDs this chart created untouched by default, and that you should update them by hand. It points to Helm's CRD documentation, which says there is no support for upgrading or deleting CRDs using Helm.

UPGRADE.md in the chart lists, for each major version, one kubectl apply --server-side -f command per CRD, to run before the upgrade. The chart also has a job that applies the CRDs, enabled with crds.upgradeJob.enabled. The values file marks it as a preview feature that is off by default.

The README adds that CRDs are not removed when you uninstall the chart. With Argo CD, sync with ServerSideApply=true, because the CRDs are larger than the annotation limit of a client-side apply.

Storage and retention

The chart's defaults are retention: 10d and storageSpec: {}. The operator's API reference says that if no storage option is specified, an EmptyDir is used. Its storage guide adds that such volumes are not persisted when the Pods are redeployed. A volume claim template changes that:

YAML

prometheus:
  prometheusSpec:
    retention: 15d
    retentionSize: 40GiB
    storageSpec:
      volumeClaimTemplate:
        spec:
          storageClassName: <storage-class>
          accessModes: ["ReadWriteOnce"]
          resources:
            requests:
              storage: 50Gi

The retention, sizes and storage class are choices made for this page. Set your own. The Prometheus documentation says that when both a time and a size retention are set, whichever triggers first is used. It recommends a retention size of at most 80 to 85% of the allocated disk, to leave room for compaction.

What managed clusters hide

kube-prometheus, the project this chart packages, states the problem in its managed-cluster addon: a managed Kubernetes cluster hides some control plane components from its customers. The addon disables the ServiceMonitors and the alert groups for the controller manager and the scheduler.

By default the chart renders Services in kube-system that select pods labeled component: kube-controller-manager and component: kube-scheduler. It also renders the alerts KubeControllerManagerDown and KubeSchedulerDown, whose expressions have the form absent(up{job="kube-scheduler"}). These values switch them off:

YAML

kubeControllerManager:
  enabled: false
kubeScheduler:
  enabled: false

With them, helm template no longer renders those Services, their ServiceMonitors or the two alerts.

For clusters on Kubernetes 1.28 and above, EKS exposes scheduler and controller manager metrics under the metrics.eks.amazonaws.com API group. GKE can be configured to send API server, scheduler and controller manager metrics to Cloud Monitoring. AKS collects control plane metrics through the managed service for Prometheus in Azure Monitor.

What to check after install

  1. Create a ServiceMonitor with the release label in another namespace, and look for its target in the Prometheus UI. That settles the namespace question for your version.
  2. Add receivers. The chart's default Alertmanager configuration has one receiver, named null, with no notification settings, and its routes all point to it.
  3. Read UPGRADE.md before you move the version pin to a new major version.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides