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 way to deploy and configure Prometheus server. This tutorial will show you how to use Prometheus operator to monitor ProxySQL server 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 database resources isolated, this tutorial uses a separate namespace called demo throughout this tutorial. Run the following command to prepare your cluster:

    $ 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/guides/proxysql/monitoring/prometheus-operator/examples 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 crd. We are going to provide these labels in spec.monitor.prometheus.labels field of ProxySQL 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
default     prometheus             1          2m19s

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 default namespace.

$ kubectl get prometheus -n default prometheus -o yaml
kind: Prometheus
  annotations: |
  creationTimestamp: "2020-08-25T04:02:07Z"
  generation: 1
    prometheus: prometheus
    manager: kubectl
    operation: Update
    time: "2020-08-25T04:02:07Z"
  name: prometheus
  namespace: default
  resourceVersion: "2087"
  selfLink: /apis/
  uid: 972a50cb-b751-418b-b2bc-e0ecc9232730
  replicas: 1
      memory: 400Mi
  serviceAccountName: prometheus
      prometheus: prometheus
      release: prometheus
  • spec.serviceMonitorSelector field specifies which ServiceMonitors should be included. The Above label release: prometheus is used to select ServiceMonitors by its selector. So, we are going to use this label in spec.monitor.prometheus.labels field of ProxySQL crd.
  • spec.serviceMonitorNamespaceSelector field specifies that the ServiceMonitors can be selected outside the Prometheus namespace by Prometheus using namespace selector. The Above label prometheus: prometheus is used to select the namespace where the ServiceMonitor is created.

Add Label to database namespace

KubeDB creates a ServiceMonitor in database namespace demo. We need to add label to demo namespace. Prometheus will select this namespace by using its spec.serviceMonitorNamespaceSelector field.

Let’s add label prometheus: prometheus to demo namespace,

$ kubectl patch namespace demo -p '{"metadata":{"labels": {"prometheus":"prometheus"}}}'
namespace/demo patched

Deploy MySQL as ProxySQL Backend

We need a mysql backend for the proxysql server. So we are creating one with the below yaml.

kind: MySQL
  name: mysql-grp
  namespace: demo
  version: "5.7.36"
  replicas: 3
    mode: GroupReplication
  storageType: Durable
    storageClassName: "standard"
      - ReadWriteOnce
        storage: 1Gi
  terminationPolicy: WipeOut
$ kubectl create -f created 

After applying the above yaml wait for the MySQL to be Ready.

Deploy ProxySQL with Monitoring Enabled

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

kind: ProxySQL
  name: proxy-server
  namespace: demo
  version: "2.4.4-debian"
  replicas: 3
  mode: GroupReplication
    name: mysql-grp
  syncUsers: true
          release: prometheus
          release: prometheus
        interval: 10s
  terminationPolicy: WipeOut
    failureThreshold: 3


  • monitor.agent: indicates that we are going to monitor this server using Prometheus operator.

  • 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 server with 10 seconds interval.

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

$ kubectl create -f created

Now, wait for the server to go into Ready state.

$ kubectl get proxysql -n demo proxy-server
NAME             VERSION       STATUS   AGE
proxy-server   2.4.4-debian    Ready    59s

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

$ $ kubectl get svc -n demo --selector=""
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)            AGE
proxy-server         ClusterIP    <none>        6033/TCP            107s
proxy-server-pods    ClusterIP   None            <none>        6032/TCP,6033/TCP   107s
proxy-server-stats   ClusterIP   <none>        6070/TCP            107s

Here, proxy-server-stats service has been created for monitoring purpose.

Let’s describe this stats service.

$ kubectl describe svc -n demo proxy-server-stats
Name:              proxy-server-stats
Namespace:         demo
Type:              ClusterIP
Port:              metrics  6070/TCP
TargetPort:        metrics/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 demo namespace that select the endpoints of proxy-server-stats service. Verify that the ServiceMonitor crd has been created.

$ kubectl get servicemonitor -n demo
NAME                   AGE
proxy-server-stats     4m8s

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

$ kubectl get servicemonitor -n demo proxy-server-stats -o yaml
kind: ServiceMonitor
  creationTimestamp: "2021-03-19T10:09:03Z"
  generation: 1
  labels: database proxy-server
    release: prometheus
  - apiVersion:
    fieldsType: FieldsV1
          .: {}
          f:release: {}
        f:ownerReferences: {}
        .: {}
        f:endpoints: {}
          .: {}
          f:matchNames: {}
          .: {}
            .: {}
    manager: proxysql-operator
    operation: Update
    time: "2021-03-19T10:09:03Z"
  name: proxy-server-stats
  namespace: demo
  - apiVersion: v1
    blockOwnerDeletion: true
    controller: true
    kind: Service
    name: proxy-server-stats
    uid: 08260a99-0984-4d90-bf68-34080ad0ee5b
  resourceVersion: "241637"
  selfLink: /apis/
  uid: 4f022d98-d2d8-490f-9548-f6367d03ae1f
  - bearerTokenSecret:
      key: ""
    honorLabels: true
    interval: 10s
    path: /metrics
    port: metrics
    - demo
    matchLabels: proxy-server stats

Notice that the ServiceMonitor has label release: prometheus that we had specified in ProxySQL crd.

Also notice that the ServiceMonitor has selector which match the labels we have seen in the proxy-server-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 default -l=app=prometheus
NAME                      READY   STATUS    RESTARTS   AGE
prometheus-prometheus-0   3/3     Running   1          16m

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 default 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 proxy-server-stats service as one of the targets.

  Prometheus Target

Check the endpoint and service labels. 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 proxysql -n demo proxy-server

# cleanup Prometheus resources
kubectl delete -f

kubectl delete -f

# cleanup Prometheus operator resources
kubectl delete -f

# delete namespace
kubectl delete ns demo