An overview of the

MySQL is a common relational database. MariaDB, as a branch version of MySQL, is compatible with MySQL protocol and is becoming more and more popular. How do you use Prometheus to monitor them in a Kubernetes environment? This is usually implemented through the open source mysqlD-Exporter, which I’ll cover in detail in this article.

Mysqld-exporter principle introduction

Mysqld-exporter operates by reading some database status data from MySQL or MariaDB and converting it to Prometheus’ index format and exposing it to an HTTP interface for collection by Prometheus. MySQL and MariaDB, which did not support Prometheus metrics, could be monitored by Prometheus:

steps

Deploy mysqld – exporter

Before deploying mysqlD-EXPORTER, ensure that MySQL or MariaDB is already deployed, either within or outside the cluster, or using an off-the-shelf cloud service. If not, here is an example of deploying MySQL from an application market to a cluster:

  1. Find MySQL in the app marketplace and clickCreate an application - Create an application.

  1. Check whether mysql is running properly.
$ kubectl get pods
NAME                     READY   STATUS        RESTARTS   AGE
mysql-698b898bf7-4dc5k   1/1     Running       0          11s
Copy the code
  1. Obtain the root password:
$ kubectl get secret -o jsonpath={.data.mysql-root-password} mysql
6ZAj33yLBo
Copy the code

With MySQL in place, we are ready to deploy mysqlD-exporter. First, create an account for mysqlD-exporter and log in to MySQL:

$ kubectl exec -it mysql-698b898bf7-4dc5k bash
$ mysql -uroot -p6ZAj33yLBo
Copy the code

Then enter SQL to create an account, for example mysqlD-EXPORTER /123456:

CREATE USER 'mysqld-exporter' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, REPLICATION SLAVE, SELECT ON *.* TO 'mysqld-exporter';
flush privileges;
Copy the code

Then use the following YAML to deploy mysqlD-EXPORTER:

apiVersion: apps/v1 kind: Deployment metadata: name: mysqld-exporter spec: replicas: 1 selector: matchLabels: app: mysqld-exporter template: metadata: labels: app: mysqld-exporter spec: containers: - name: mysqld-exporter image: PROM/mysqld - exporter: v0.12.1 args: - --collect.info_schema.tables - --collect.info_schema.innodb_tablespaces - --collect.info_schema.innodb_metrics - --collect.global_status - --collect.global_variables - --collect.slave_status - --collect.info_schema.processlist - --collect.perf_schema.tablelocks - --collect.perf_schema.eventsstatements - --collect.perf_schema.eventsstatementssum - --collect.perf_schema.eventswaits - --collect.auto_increment.columns - --collect.binlog_size - --collect.perf_schema.tableiowaits - --collect.perf_schema.indexiowaits - --collect.info_schema.userstats - --collect.info_schema.clientstats - --collect.info_schema.tablestats - --collect.info_schema.schemastats - --collect.perf_schema.file_events - --collect.perf_schema.file_instances - --collect.perf_schema.replication_group_member_stats - --collect.perf_schema.replication_applier_status_by_worker - --collect.slave_hosts - --collect.info_schema.innodb_cmp - --collect.info_schema.innodb_cmpmem - --collect.info_schema.query_response_time - --collect.engine_tokudb_status - --collect.engine_innodb_status ports: - containerPort: 9104 protocol: TCP env: - name: DATA_SOURCE_NAME value: "mysqld-exporter:123456@(mysql.default.svc.cluster.local:3306)/" --- apiVersion: v1 kind: Service metadata: name: mysqld-exporter labels: app: mysqld-exporter spec: type: ClusterIP ports: - port: 9104 protocol: TCP name: http selector: app: mysqld-exporterCopy the code

! Replace the password of the account in DATA_SOURCE_NAME and the connection address of MySQL as required

Add monitoring collection configuration

With mysqlD-Exporter, we can configure monitored collection so that mysQLD-exporter’s exposed data is collected, and if you have Promethy-Operator installed in your cluster, Collection rules can be configured by defining ServiceMonitor, as shown in the following example:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: mysqld-exporter
spec:
  endpoints:
    interval: 5s
    targetPort: 9104
  namespaceSelector:
    matchNames:
    - default
  selector:
    matchLabels:
      app: mysqld-exporter
Copy the code

You can configure the collection rules by modifying the Prometheus native configuration file, as shown in the following example:

    - job_name: mysqld-exporter
      scrape_interval: 5s
      kubernetes_sd_configs:
      - role: endpoints
        namespaces:
          names:
          - default
      relabel_configs:
      - action: keep
        source_labels:
        - __meta_kubernetes_service_label_app_kubernetes_io_name
        regex: mysqld-exporter
      - action: keep
        source_labels:
        - __meta_kubernetes_endpoint_port_name
        regex: http
Copy the code

Adding a Monitoring Panel

If you want to see an overview of MySQL or MariaDB, you can import this panel from grafana.com: grafana.com/grafana/das…

If you need a richer palette, you can import some of percona’s open source panels at github.com/percona/gra… Import the contents of the JSON file starting with MySQL_.

summary

This article describes how mysqlD-Exporter can run MySQL or MariaDB, which Prometheus does not support, by allowing Prometheus to run its own database of monitoring metrics and add a Grafana monitor panel. Make monitoring more convenient.

[Tencent cloud native] cloud said new, cloud research new technology, cloud travel new live, cloud appreciation information, scan code to pay attention to the public account of the same name, timely access to more dry goods!!