github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/developer_docs/integration/monitoring.md (about)

     1  ---
     2  title: Monitoring
     3  description: How to configure monitoring function in KubeBlocks 
     4  keywords: [monitoring]
     5  sidebar_position: 6
     6  sidebar_label: Monitoring
     7  ---
     8  
     9  # Configure monitoring
    10  
    11  This tutorial takes Oracle MySQL as an example and explains how to configure monitoring in KubeBlocks. You can refer to [the full PR](https://github.com/apecloud/learn-kubeblocks-addon/tree/main/tutorial-4-monitor-cluster/oracle-mysql).
    12  
    13  ## Before you start
    14  
    15  1. Knowledge about basic Kubernetes concepts, such as Pod and Sidecar.
    16  2. Finish [Tutorial 1](./how-to-add-an-add-on.md).
    17  3. Knowledge about basic monitoring system concepts, such as Prometheus and Grafana.
    18  
    19  ## Introduction
    20  
    21  Monitoring is an essential part of Kubernetes observability. It helps developers check the system's operational status to quickly identify issues.
    22  
    23  Kubeblocks currently integrates Prometheus and Grafana as add-ons. In this tutorial, you will learn how to integrate the Prometheus/Grafana solution.
    24  
    25  ### Prometheus Overview
    26  
    27  Prometheus provides an open-source monitoring solution that integrates metric collection, metric storage, and alert capabilities.
    28  
    29  It is widely used in cloud-native, containerized, and microservices architectures. With Prometheus, developers and operations teams can monitor the performance and health status of applications in real-time, so as to quickly identify and resolve issues to ensure application reliability and availability. Prometheus is usually used with Grafana to create powerful monitoring and observability solutions.
    30  
    31  ### Grafana Overview
    32  
    33  Grafana is an open-source analytics and monitoring platform widely used for visualizing time series data. It allows users to create interactive and customizable dashboards to monitor and analyze data from various sources.
    34  
    35  :paperclip: Table 1. Terminology
    36  
    37  | Term | Description |
    38  | :--  | :---------- |
    39  | Prometheus Exporter | Prometheus Exporter is a component that collects monitoring data and provides data to external entities using the Prometheus monitoring specification. <br /> For more details, refer to [Prometheus  Exporter List](https://prometheus.io/docs/instrumenting/exporters/). |
    40  | Prometheus Metrics | Prometheus Metrics are data points used for monitoring and performance analysis. They typically include request counts, response times, CPU usage, and memory usage. |
    41  | Grafana Dashboard | Grafana Dashboard is a visualization interface used to present data. It is commonly used for monitoring and analyzing various time series data. |
    42  
    43  Prometheus in KubeBlocks has already been configured with scraping jobs, so developers only need to configure the Exporter. In KubeBlocks, the Exporter is deployed as a sidecar alongside the main container of the database engine in the same Pod.
    44  
    45  ## Configure Exporter
    46  
    47  First, choose an Exporter. This tutorial is based on Oracle MySQL, so a MySQL Exporter is needed.
    48  
    49  ### Configure Exporter version
    50  
    51  Modify ClusterVersion (`clusterversion.yaml`).
    52  
    53  You can find an appropriate Exporter from open-source communities(e.g., [Prometheus in Docker](https://hub.docker.com/u/prom)).
    54  
    55  ```yaml
    56  componentVersions:
    57  - componentDefRef: mysql-compdef
    58    versionsContext:
    59      containers:
    60      - name: mysql-container
    61        image: ...
    62        imagePullPolicy: ..
    63      - name: mysql-exporter               
    64        image: prom/mysqld-exporter:v0.14.0
    65  ```
    66  
    67  Specify the image of mysql-exporter as prom/mysqld-exporter with the version 0.14.0.
    68  
    69  ### Add an Exporter container
    70  
    71  Modify `clusterdefinition.yaml` and configure mysql-exporter in Sidecar form.
    72  
    73  ```yaml
    74  podSpec:
    75    containers:
    76      # mysql container and other containers ->
    77      - name: mysql-exporter
    78        ports:
    79          - name: metrics
    80            containerPort: 9104
    81            protocol: TCP
    82        env:
    83          - name: "MYSQL_MONITOR_USER"
    84            valueFrom:
    85              secretKeyRef:
    86                name: $(CONN_CREDENTIAL_SECRET_NAME)
    87                key: username
    88          - name: "MYSQL_MONITOR_PASSWORD"
    89            valueFrom:
    90              secretKeyRef:
    91                name: $(CONN_CREDENTIAL_SECRET_NAME)
    92                key: password          
    93          - name: "DATA_SOURCE_NAME"
    94            value: "$(MYSQL_MONITOR_USER):$(MYSQL_MONITOR_PASSWORD)@(localhost:3306)/"
    95  ```
    96  
    97  As shown from Line 4 to 21, a new container has been added to the original PodSpec. `DATA_SOURCE_NAME` is an environment variable specific to the mysql-exporter.
    98  
    99  :::caution
   100  
   101  Different Exporters require different environment variables, and they need to be configured based on specific features of each Exporter.
   102  
   103  As mentioned, this tutorial uses mysql exporter 0.14.0. In the latest mysql exporter version 0.15.0, the variable `DATA_SOURCE_NAME` is no longer supported.
   104  
   105  :::
   106  
   107  ### Configure monitor parameters
   108  
   109  Modify `clusterdefinition.yaml` and configure `monitor` parameters.
   110  
   111  ```yaml
   112  componentDefs:
   113    - name: mysql-compdef
   114      characterType: mysql
   115      service: ..
   116      monitor:
   117        exporterConfig:
   118          scrapePort: 9104        # Listening port of the Exporter, used by Prometheus to pull data
   119          scrapePath: "/metrics"  # Path of the Exporter path, used by Prometheus to pull data
   120  ```
   121  
   122  KubeBlocks supports multiple monitoring solutions. To use the open-source Prometheus/Grafana solution, configure the listening port and metrics path in `monitor`, which should correspond to the container-port specified in [2. Add an Exporter container](#2-add-an-exporter-container).
   123  
   124  ## Configure Grafana Dashboard
   125  
   126  ### Obtain Grafana Dashboard configurations
   127  
   128  Grafana Dashboard can help users monitor, analyze, and understand data in real-time. For popular databases, various dashboard configuration files (in JSON format) can be easily found.
   129  
   130  - [Official Website of Grafana](https://grafana.com/grafana/dashboards).
   131  - [KubeBlocks Dashboard](https://github.com/apecloud/kubeblocks-mixin).
   132  
   133  ### Add to your cluster
   134  
   135  Import the downloaded JSON files on the Grafana Dashboard page or configure them in your cluster template.
   136  
   137  The latter option is more versatile, as the same configuration can be reused for any cluster generated through the template.
   138  
   139  Therefore, two files are added to the existing Helm chart.
   140  
   141  - dashboards: Save dashboard JSON files.
   142  - grafana: Create a ConfigMap to store the contents of dashboard JSON files.
   143  
   144  ```yaml
   145  tree oracle-mysql
   146  .
   147  ├── Chart.yaml
   148  ├── dashboards
   149  │   └── mysql.json
   150  ├── templates
   151  │   ├── NOTES.txt   
   152  │   ├── _helpers.tpl
   153  │   ├── clusterdefinition.yaml
   154  │   └── clusterversion.yaml
   155  │   └── grafana
   156  │       └── configmap-dashboards.yaml
   157  └── values.yaml
   158  
   159  4 directories, 8 files
   160  ```
   161  
   162  ## Monitor cluster data
   163  
   164  ### Prepare the environment and enable Prometheus monitoring components
   165  
   166  Run `kbcli addon list` to check if the following add-ons are enabled (status: Enabled):
   167  
   168  ```bash
   169  kbcli addon list
   170  >
   171  ...
   172  grafana                        Helm   Enabled                   true  
   173  alertmanager-webhook-adaptor   Helm   Enabled                   true    
   174  prometheus                     Helm   Enabled    alertmanager   true
   175  ...
   176  ```
   177  
   178  If not (status: `Disabled`), enable them one by one.
   179  
   180  ```bash
   181  kbcli addon enable prometheus
   182  kbcli addon enable alertmanager-webhook-adaptor
   183  kbcli addon enable grafana
   184  ```
   185  
   186  Then you can have access to the integrated three dashboards:
   187  
   188  ```bash
   189  kbcli dashboard list
   190  >
   191  NAME                                 NAMESPACE   PORT    CREATED-TIME
   192  kubeblocks-grafana                   kb-system   13000   Jul 24,2023 11:38 UTC+0800
   193  kubeblocks-prometheus-alertmanager   kb-system   19093   Jul 24,2023 11:38 UTC+0800
   194  kubeblocks-prometheus-server         kb-system   19090   Jul 24,2023 11:38 UTC+0800
   195  ```
   196  
   197  ### Create a database cluster
   198  
   199  1. Install a cluster template.
   200  
   201     ```bash
   202     helm install oracle-mysql ./path-to-your-helm-chart/oracle-mysql
   203     ```
   204  
   205  2. Enable monitoring function.
   206  
   207     Opt 1. Enable when creating a cluster
   208  
   209     ```bash
   210     kbcli cluster create mycluster --cluster-definition='oracle-mysql' --monitor='true'
   211     ```
   212  
   213     Opt 2. Enable in an existing cluster
   214  
   215     ```bash
   216     kbcli cluster update mycluster --monitor='true'
   217     ```
   218  
   219  3. Open the dashboard
   220  
   221     ```bash
   222     # View available dashboards
   223     kbcli dashboard list
   224     >
   225     NAME                                      NAMESPACE        PORT        CREATED-TIME
   226     kubeblocks-grafana                        default          3000        Jan 13,2023 10:53 UTC+0800
   227     kubeblocks-prometheus-alertmanager        default          9093        Jan 13,2023 10:53 UTC+0800
   228     kubeblocks-prometheus-server              default          9090        Jan 13,2023 10:53 UTC+0800
   229  
   230     # Select Grafana and open the web console in the default browser
   231     kbcli dashboard open kubeblocks-grafana
   232     ```
   233  
   234  4. Sign in to the dashboard
   235  
   236  ![Grafana Homepage](./../../img/addon-monitoring-signin.png)
   237  
   238  :::note
   239  
   240  If the dashboard requires a login, use the following username and password.
   241  
   242  ```bash
   243  Username: admin 
   244  Password: kubeblocks
   245  ```
   246  
   247  :::
   248  
   249  ## Summary
   250  
   251  This tutorial explains how to quickly adapt the Prometheus/Grafana solution to monitor your database cluster. KubeBlocks will also introduce a monitoring solution based on OpenTelemetry in the future. Stay tuned for updates.
   252  
   253  ## References
   254  
   255  1. [Prometheus](https://prometheus.io/).
   256  2. [Grafana Dashboard](https://grafana.com/grafana/dashboards/).
   257  3. [Create a dashboard](https://grafana.com/docs/grafana/latest/dashboards/build-dashboards/create-dashboard/).
   258  
   259  ## Appendix
   260  
   261  ### A.1 Disable cluster monitoring
   262  
   263  In KubeBlocks, you can enable or disable monitoring for a specific cluster using kbcli.
   264  
   265  ```bash
   266  kbcli cluster update mycluster --monitor='false'
   267  ```