Monitoring Database with KubeDB

KubeDB has native support for monitoring via Prometheus. You can use builtin Prometheus scrapper or CoreOS Prometheus Operator to monitor KubeDB managed databases. This tutorial will show you how database monitoring works with KubeDB and how to configure Database crd to enable monitoring.


KubeDB uses Prometheus exporter images to export Prometheus metrics for respective databases. Following diagram shows the logical flow of database monitoring with KubeDB.

  Database Monitoring Flow

When a user creates a database crd with spec.monitor section configured, KubeDB operator provisions the respective database and injects an exporter image as sidecar to the database pod. It also creates a dedicated stats service with name {database-crd-name}-stats for monitoring. Prometheus server can scrape metrics using this stats service.

Configure Monitoring

In order to enable monitoring for a database, you have to configure spec.monitor section. KubeDB provides following options to configure spec.monitor section:

Field Type Uses
spec.monitor.agent Required Type of the monitoring agent that will be used to monitor this database. It can be or
spec.monitor.prometheus.namespace Optional Namespace where the Prometheus server is running or will be deployed. For agent type, ServiceMonitor crd will be created in this namespace.
spec.monitor.prometheus.labels Optional Labels for ServiceMonitor crd.
spec.monitor.prometheus.port Optional Port number where the exporter side car will serve metrics.
spec.monitor.prometheus.interval Optional Interval at which metrics should be scraped.
spec.monitor.args Optional Arguments to pass to the exporter sidecar.
spec.monitor.env Optional List of environment variables to set in the exporter sidecar container.
spec.monitor.resources Optional Resources required by exporter sidecar container.
spec.monitor.securityContext Optional Security options the exporter should run with.

Sample Configuration

A sample YAML for Redis crd with spec.monitor section configured to enable monitoring with CoreOS prometheus-operator is shown below.

kind: Redis
  name: sample-redis
  namespace: databases
  version: "4.0-v1"
  terminationPolicy: WipeOut
  configSource: # configure Redis to use password for authentication
      name: redis-config
  storageType: Durable
    storageClassName: default
    - ReadWriteOnce
        storage: 5Gi
      namespace: monitoring
        k8s-app: prometheus
    - --redis.password=$(REDIS_PASSWORD)
    - name: REDIS_PASSWORD
          name: _name_of_secret_with_redis_password
          key: password # key with the password
        memory: 512Mi
        cpu: 200m
        memory: 512Mi
        cpu: 250m
      runAsUser: 2000
      allowPrivilegeEscalation: false

Assume that above Redis is configured to use basic authentication. So, exporter image also need to provide password to collect metrics. We have provided it through spec.monitor.args field.

Here, we have specified that we are going to monitor this server using CoreOS prometheus-operator through spec.monitor.agent: KubeDB will create a ServiceMonitor crd in monitoring namespace and this ServiceMonitor will have k8s-app: prometheus label.

Next Steps

Subscribe to our free technical newsletter!

Join thousands of subscribers and stay up-to-date on AppsCode.