You are looking at the documentation of a prior release. To read the documentation of the latest release, please visit here.

New to KubeDB? Please start here.

Monitoring ProxySQL Using Prometheus operator

Prometheus operator provides simple and Kubernetes native ways to deploy and configure the Prometheus server. This tutorial will show you how to use the Prometheus operator to monitor ProxySQL 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 kind.

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

  • To keep Prometheus resources isolated, we are going to use two different namespaces called,

  • monitoring to deploy respective monitoring resources

  • demo to deploy respective resources from KubeDB

    $ kubectl create ns monitoring
    namespace/monitoring created
    $ kubectl create ns demo
    namespace/demo created
  • We need a 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/proxysql folder in GitHub repository kubedb/docs.

Find Out Required Labels for ServiceMonitor

We need to know the labels used to select ServiceMonitor by a Prometheus object. We are going to provide these labels in .spec.monitor.prometheus.labels field of ProxySQL object 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   2m56s

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-11-26T11:58:19Z"
  generation: 1
    prometheus: prometheus
  name: prometheus
  namespace: monitoring
  resourceVersion: "25919"
  selfLink: /apis/
  uid: 394c5008-56a3-4f0a-af12-9beb873cbc3e
  replicas: 1
      memory: 400Mi
  serviceAccountName: prometheus
      k8s-app: prometheus

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

Deploy ProxySQL with Monitoring Enabled

ProxySQL assumes that there already exists backend servers. So we need some backend ready. Here, we will set up ProxySQL to load balance MySQL Group Replication.

Deploy Sample MySQL Group Replication

So, let’s deploy a MySQL database with Group Replication support. Below is the MySQL object that we are going to create.

kind: MySQL
  name: my-group
  namespace: demo
  version: "5.7.36"
  replicas: 3
    mode: GroupReplication
      name: "dc002fc3-c412-4d18-b1d4-66c1fbfbbc9b"
  storageType: Durable
    storageClassName: "standard"
      - ReadWriteOnce
        storage: 1Gi
  terminationPolicy: WipeOut

Let’s create the MySQL object we have shown above.

$ kubectl apply -f created

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

$ kubectl get my -n demo my-group
my-group   5.7.25    Running   3m

Deploy ProxySQL

Now we are going to create a sample ProxySQL object to load balance the previously created MySQL group. Keep note that monitoring is enabled in this sample ProxySQL object. See below:

kind: ProxySQL
  name: builtin-prom-proxysql
  namespace: demo
  version: "2.0.4"
  replicas: 1
  mode: GroupReplication
      apiGroup: ""
      kind: MySQL
      name: my-group
    replicas: 3
        port: 42004
          k8s-app: prometheus
        interval: 10s


  • .spec.monitor.agent: indicates that we are going to monitor this server using the Prometheus operator.
  • .spec.monitor.prometheus.port specifies the port at which ProxySQL exporter will serve the metrics and from this port, the Prometheus server collects them.
  • .spec.monitor.prometheus.namespace: monitoring specifies that KubeDB should create ServiceMonitor in monitoring namespace.
  • .spec.monitor.prometheus.labels specifies that KubeDB should create ServiceMonitor with these labels.
  • .spec.monitor.prometheus.interval indicates that the Prometheus server should scrape metrics from ProxySQL exporter with 10 seconds interval.

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

$ kubectl create -f created
$ kubectl get proxysql -n demo coreos-prom-proxysql
NAME                   VERSION   STATUS    AGE
coreos-prom-proxysql   2.0.4     Running   14s

KubeDB will create a separate stats service with the name {ProxySQL object name}-stats for monitoring purposes.

$ kubectl get svc -n demo --selector=""
NAME                         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE
coreos-prom-proxysql         ClusterIP   <none>        6033/TCP    73s
coreos-prom-proxysql-stats   ClusterIP   <none>        42004/TCP   68s

Here, coreos-prom-proxysql-stats service has been created for monitoring purposes.

Let’s describe this stats service.

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

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

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

$ kubectl get servicemonitor -n monitoring
NAME                               AGE
kubedb-demo-coreos-prom-proxysql   3m22s

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

$ kubectl get servicemonitor -n monitoring kubedb-demo-coreos-prom-proxysql -o yaml
kind: ServiceMonitor
  creationTimestamp: "2019-11-26T12:46:21Z"
  generation: 1
    k8s-app: prometheus coreos-prom-proxysql-stats.demo
  name: kubedb-demo-coreos-prom-proxysql
  namespace: monitoring
  - apiVersion: v1
    blockOwnerDeletion: true
    kind: Service
    name: coreos-prom-proxysql-stats
    uid: a37585e6-14b3-41b2-a8d2-81763845cb8d
  resourceVersion: "30358"
  selfLink: /apis/
  uid: 09dc865e-73c0-44e1-b972-540b58efc660
  - honorLabels: true
    interval: 10s
    path: /metrics
    port: prom-http
    - demo
    matchLabels: stats GroupReplication coreos-prom-proxysql

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

Also, notice that the ServiceMonitor has a selector that matches the labels we have seen in the coreos-prom-proxysql-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          56m

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

Run the 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-proxysql-stats service as one of the targets.

  Prometheus Target

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

Cleaning up

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

# 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

# cleanup proxysql and mysql resources
$ kubectl delete -n demo proxysql/coreos-prom-proxysql
$ kubectl delete -n demo my/my-group

# delete namespace
$ kubectl delete ns monitoring
$ kubectl delete ns demo

Next Steps