New to KubeDB? Please start here.

Monitoring Elasticsearch Using CoreOS Prometheus Operator

CoreOS prometheus-operator provides simple and Kubernetes native way to deploy and configure Prometheus server. This tutorial will show you how to use CoreOS Prometheus operator to monitor Elasticsearch database deployed with KubeDB.

Before You Begin

  • At first, you need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using Minikube.

  • To learn how Prometheus monitoring works with KubeDB in general, please visit here.

  • To keep Prometheus resources isolated, we are going to use a separate namespace called monitoring to deploy respective monitoring resources. We are going to deploy database in demo namespace.

  $ kubectl create ns monitoring
  namespace/monitoring created

  $ kubectl create ns demo
  namespace/demo created
  • We need a CoreOS prometheus-operator instance running. If you don’t already have a running instance, deploy one following the docs from here.

  • If you already don’t have a Prometheus server running, deploy one following tutorial from here.

Note: YAML files used in this tutorial are stored in docs/examples/elasticsearch folder in GitHub repository kubedb/cli.

Find out required labels for ServiceMonitor

We need to know the labels used to select ServiceMonitor by a Prometheus crd. We are going to provide these labels in spec.monitor.prometheus.labels field of Elasticsearch crd so that KubeDB creates ServiceMonitor object accordingly.

At first, let’s find out the available Prometheus server in our cluster.

$ kubectl get prometheus --all-namespaces
monitoring   prometheus   18m

If you don’t have any Prometheus server running in your cluster, deploy one following the guide specified in Before You Begin section.

Now, let’s view the YAML of the available Prometheus server prometheus in monitoring namespace.

$ kubectl get prometheus -n monitoring prometheus -o yaml
kind: Prometheus
  annotations: |
  creationTimestamp: 2019-01-03T13:41:51Z
  generation: 1
    prometheus: prometheus
  name: prometheus
  namespace: monitoring
  resourceVersion: "44402"
  selfLink: /apis/
  uid: 5324ad98-0f5d-11e9-b230-080027f306f3
  replicas: 1
      memory: 400Mi
  serviceAccountName: prometheus
      k8s-app: prometheus

Notice the spec.serviceMonitorSelector section. Here, k8s-app: prometheus label is used to select ServiceMonitor crd. So, we are going to use this label in spec.monitor.prometheus.labels field of Elasticsearch crd.

Deploy Elasticsearch with Monitoring Enabled

At first, let’s deploy an Elasticsearch database with monitoring enabled. Below is the Elasticsearch object that we are going to create.

kind: Elasticsearch
  name: coreos-prom-es
  namespace: demo
  version: "6.3-v1"
  terminationPolicy: WipeOut
    storageClassName: "standard"
    - ReadWriteOnce
        storage: 1Gi
      namespace: monitoring
        k8s-app: prometheus
      interval: 10s


  • monitor.agent: indicates that we are going to monitor this server using CoreOS prometheus operator.
  • monitor.prometheus.namespace: monitoring specifies that KubeDB should create ServiceMonitor in monitoring namespace.

  • monitor.prometheus.labels specifies that KubeDB should create ServiceMonitor with these labels.

  • monitor.prometheus.interval indicates that the Prometheus server should scrape metrics from this database with 10 seconds interval.

Let’s create the Elasticsearch object that we have shown above,

$ kubectl create -f created

Now, wait for the database to go into Running state.

$ kubectl get es -n demo coreos-prom-es
NAME             VERSION   STATUS    AGE
coreos-prom-es   6.3-v1    Running   5m

KubeDB will create a separate stats service with name {Elasticsearch crd name}-stats for monitoring purpose.

$ kubectl get svc -n demo --selector=""
NAME                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE
coreos-prom-es          ClusterIP     <none>        9200/TCP    10m
coreos-prom-es-master   ClusterIP   <none>        9300/TCP    10m
coreos-prom-es-stats    ClusterIP    <none>        56790/TCP   9m35s

Here, coreos-prom-es-stats service has been created for monitoring purpose.

Let’s describe this stats service.

$ kubectl describe svc -n demo coreos-prom-es-stats
Name:              coreos-prom-es-stats
Namespace:         demo
Type:              ClusterIP
Port:              prom-http  56790/TCP
TargetPort:        prom-http/TCP
Session Affinity:  None
Events:            <none>

Notice the Labels and Port fields. ServiceMonitor will use these information to target its endpoints.

KubeDB will also create a ServiceMonitor crd in monitoring namespace that select the endpoints of coreos-prom-es-stats service. Verify that the ServiceMonitor crd has been created.

$ kubectl get servicemonitor -n monitoring
NAME                         AGE
kubedb-demo-coreos-prom-es   6m

Let’s verify that the ServiceMonitor has the label that we had specified in spec.monitor section of Elasticsearch crd.

$ kubectl get servicemonitor -n monitoring kubedb-demo-coreos-prom-es -o yaml
kind: ServiceMonitor
  creationTimestamp: 2019-01-03T14:19:50Z
  generation: 1
    k8s-app: prometheus coreos-prom-es-stats.demo
  name: kubedb-demo-coreos-prom-es
  namespace: monitoring
  resourceVersion: "47254"
  selfLink: /apis/
  uid: a1be4c48-0f62-11e9-b230-080027f306f3
  - honorLabels: true
    interval: 10s
    path: /metrics
    port: prom-http
    - demo
    matchLabels: Elasticsearch coreos-prom-es

Notice that the ServiceMonitor has label k8s-app: prometheus that we had specified in Elasticsearch crd.

Also notice that the ServiceMonitor has selector which match the labels we have seen in the coreos-prom-es-stats service. It also, target the prom-http port that we have seen in the stats service.

Verify Monitoring Metrics

At first, let’s find out the respective Prometheus pod for prometheus Prometheus server.

$ kubectl get pod -n monitoring -l=app=prometheus
NAME                      READY   STATUS    RESTARTS   AGE
prometheus-prometheus-0   3/3     Running   1          63m

Prometheus server is listening to port 9090 of prometheus-prometheus-0 pod. We are going to use port forwarding to access Prometheus dashboard.

Run following command on a separate terminal to forward the port 9090 of prometheus-prometheus-0 pod,

$ kubectl port-forward -n monitoring prometheus-prometheus-0 9090
Forwarding from -> 9090
Forwarding from [::1]:9090 -> 9090

Now, we can access the dashboard at localhost:9090. Open http://localhost:9090 in your browser. You should see prom-http endpoint of coreos-prom-es-stats service as one of the targets.

  Prometheus Target

Check the endpoint and service labels marked by red rectangle. It verifies that the target is our expected database. Now, you can view the collected metrics and create a graph from homepage of this Prometheus dashboard. You can also use this Prometheus server as data source for Grafana and create beautiful dashboard with collected metrics.

Cleaning up

To cleanup the Kubernetes resources created by this tutorial, run following commands

# cleanup database
kubectl delete -n demo es/coreos-prom-es

# cleanup prometheus resources
kubectl delete -n monitoring prometheus prometheus
kubectl delete -n monitoring clusterrolebinding prometheus
kubectl delete -n monitoring clusterrole prometheus
kubectl delete -n monitoring serviceaccount prometheus
kubectl delete -n monitoring service prometheus-operated

# cleanup prometheus operator resources
kubectl delete -n monitoring deployment prometheus-operator
kubectl delete -n dmeo serviceaccount prometheus-operator
kubectl delete clusterrolebinding prometheus-operator
kubectl delete clusterrole prometheus-operator

# delete namespace
kubectl delete ns monitoring
kubectl delete ns demo

Next Steps

Subscribe to our free technical newsletter!

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