github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/user_docs/observability/alert.md (about)

     1  ---
     2  title: Configure alert
     3  description: How to enable alert
     4  keywords: [mysql, alert, alert message, email alert]
     5  sidebar_position: 2
     6  ---
     7  
     8  # Configure alert
     9  
    10  ## Configure IM alert
    11  
    12  Alerts are mainly used for daily error response to improve system availability. KubeBlocks has a built-in set of common alert rules and integrates multiple notification channels. The alert capability of KubeBlocks can meet the operation and maintenance requirements of production-level online clusters.
    13  
    14  :::note
    15  
    16  The alert function is the same for all.
    17  
    18  :::
    19  
    20  ### Alert rules
    21  
    22  The built-in generic alert rules of KubeBlocks meet the needs of various data products and provide an out-of-the-box experience without further configurations. These alert rules provide the best practice for cluster operation and maintenance, which further improve alert accuracy and reduce the probability of false negatives and false positives by experience-based smoothing windows, alert thresholds, alert levels, and alert indicators.
    23  
    24  Taking PostgreSQL as an example, the alert rules have built-in common abnormal events, such as instance down, instance restart, slow query, connection amount, deadlock, and cache hit rate.
    25  
    26  The following example shows PostgreSQL alert rules (refer to [Prometheus](https://prometheus.io/docs/prometheus/latest/querying/basics/) for syntax). When the amount of active connections exceeds 80% of the threshold and lasts for 2 minutes, Prometheus triggers a warning alert and sends it to AlertManager.
    27  
    28  ```bash
    29  alert: PostgreSQLTooManyConnections
    30    expr: |
    31      sum by (namespace,app_kubernetes_io_instance,pod) (pg_stat_activity_count{datname!~"template.*|postgres"})
    32      > on(namespace,app_kubernetes_io_instance,pod)
    33      (pg_settings_max_connections - pg_settings_superuser_reserved_connections) * 0.8
    34    for: 2m
    35    labels:
    36      severity: warning
    37    annotations:
    38      summary: "PostgreSQL too many connections (> 80%)"
    39      description: "PostgreSQL has too many connections and the value is {{ $value }}. (instance: {{ $labels.pod }})"
    40  ```
    41  
    42  You can view all the built-in alert rules in **Alerts Tab** of **Prometheus Dashboards**. Run the commands below to open Prometheus Dashboards.
    43  
    44  ```bash
    45  # View dashboards list
    46  kbcli dashboard list
    47  
    48  # Open Prometheus Dashboards
    49  kbcli dashboard open kubeblocks-prometheus-server # Here is an example and fill in the actual name based on the above dashboard list
    50  ```
    51  
    52  ### Configure IM alert
    53  
    54  The alert message notification of KubeBlocks mainly adopts the AlertManager native capability. After receiving the Prometheus alert, KubeBlocks performs steps including deduplication, grouping, silence, suppression, and routing, and finally sends it to the corresponding notification channel.
    55  
    56  AlertManager integrates a set of notification channels, such as Email and Slack. KubeBlocks extends new IM class notification channels with AlertManger Webhook.
    57  
    58  This tutorial takes configuring Feishu as the notification channel as an example.
    59  
    60  **Before you start**
    61  
    62  To receive alerts, you need to deploy monitoring add-ons and enable cluster monitoring first. Refer to [Monitor database](monitor-database.md) for details.
    63  
    64  ***Steps:***
    65  
    66  1. Configure alert channels.
    67  
    68     Refer to the following guides to configure your alert channels.
    69  
    70     * [Feishu custom bot](https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN)
    71     * [DingTalk custom bot](https://open.dingtalk.com/document/orgapp/custom-robot-access)
    72     * [WeChat Enterprise custom bot](https://developer.work.weixin.qq.com/document/path/91770)
    73     * [Slack](https://api.slack.com/messaging/webhooks)
    74  
    75    :::note
    76  
    77    * Each notification channel has its interface call volume and frequency limits and when the limits are reached, the channel will limit traffic and you cannot receive alerts.
    78    * The SLA of the service provided by a single channel cannot guarantee the alerts are sent successfully. Therefore, it is recommended to configure multiple notification channels to ensure availability.
    79  
    80    :::
    81  
    82  2. Configure the receiver.
    83  
    84     To improve ease of use, `kbcli` develops the `alert` subcommand to simplify the receiver configuration. You can set the notification channels and receivers by the `alert` subcommand. This subcommand also supports condition filters, such as cluster names and severity levels. After the configuration succeeds, it takes effect dynamically without the service restarting.
    85  
    86     1. Add an alert receiver.
    87  
    88        ```bash
    89        kbcli alert add-receiver --webhook='xxx' --cluster=xx --severity=xx
    90        ```
    91  
    92        ***Example***
    93  
    94        The following commands show how to add a receiver to Feishu based on different requirements.
    95        The webhook address below is an example and you need to replace it with the actual address before running the command.
    96  
    97        ```bash
    98        # Signature authentication is disabled
    99        kbcli alert add-receiver \
   100        --webhook='url=https://open.feishu.cn/open-apis/bot/v2/hook/foo'
   101  
   102        # Signature authentication is enabled and sign is used as the value of token
   103        kbcli alert add-receiver \
   104        --webhook='url=https://open.feishu.cn/open-apis/bot/v2/hook/foo,token=sign'
   105  
   106        # Only receive the alerts from a cluster named mysql-cluster
   107        kbcli alert add-receiver \
   108        --webhook='url=https://open.feishu.cn/open-apis/bot/v2/hook/foo' --cluster=mysql-cluster
   109  
   110        # Only receive the critical alerts from a cluster named mysql-cluster
   111        kbcli alert add-receiver \
   112        --webhook='url=https://open.feishu.cn/open-apis/bot/v2/hook/foo' --cluster=mysql-cluster --severity=critical
   113        ```
   114  
   115    :::note
   116  
   117    For the detailed command description, run `kbcli alert add-receiver -h`.
   118  
   119    :::
   120  
   121     2. View the receiver list to verify whether the new receiver is added. 
   122  
   123        You can also view the notification configurations by this command.
   124  
   125        ```bash
   126        kbcli alert list-receivers
   127        ```
   128  
   129     3. (Optional) Delete the notification channel and receiver if you want to disable the alert function.
   130  
   131        ```bash
   132        kbcli alert delete-receiver <receiver-name>
   133        ```
   134  
   135  ### IM alert troubleshooting
   136  
   137  If you cannot receive alert notices, run the commands below to troubleshoot the logs of AlertManager and AlertManager-Webhook-Adaptor add-ons.
   138  
   139  ```bash
   140  # Find the corresponding Pod of AlertManager and get Pod name
   141  kubectl get pods -n kb-system -l 'app=prometheus,component=alertmanager'
   142  
   143  # Search AlertManeger logs
   144  kubectl logs <pod-name> -n kb-system -c prometheus-alertmanager
   145  
   146  # Find the corresponding Pod of AlertManager-Webhook-Adaptor and get Pod name
   147  kubectl get pods -n kb-system -l 'app.kubernetes.io/name=alertmanager-webhook-adaptor'
   148  
   149  # Search AlertManager-Webhook-Adaptor logs
   150  kubectl logs <pod-name> -n kb-system -c alertmanager-webhook-adaptor
   151  ```
   152  
   153  ## Configure email alert
   154  
   155  KubeBlocks also supports email alert.
   156  
   157  1. Configure SMTP server.
   158  
   159     ```bash
   160     kbcli alert config-smtpserver 
   161     --smtp-from alert-test@apecloud.com \
   162     --smtp-smarthost smtp.feishu.cn:587 \
   163     --smtp-auth-username alert-test@apecloud.com \
   164     --smtp-auth-password 123456abc \
   165     --smtp-auth-identity alert-test@apecloud.com
   166     ```
   167  
   168  2. View the SMTP server list to verify whether the above server is added successfully.
   169  
   170     You can also view the configuration details by this command.
   171  
   172     ```bash
   173     kbcli alert list-smtpserver
   174     ```
   175  
   176  3. Add email receiver.
   177  
   178     ```bash
   179     kbcli alert add-receiver --email='user1@kubeblocks.io'
   180     ```
   181  
   182     KubeBlocks email alert now supports receiving emails from a specified cluster and of a certain severity. You can set this function by using `--cluster` and `--severity` flags.
   183  
   184     * `--cluster`: means only receiving emails from a specified cluster.
   185  
   186         ```bash
   187         kbcli alert add-receiver --email='user1@kubeblocks.io,user2@kubeblocks.io' --cluster=mycluster
   188         ```
   189  
   190     * `--severity`: means only receiving emails from a specified cluster and the alert severity is `warning`.
   191  
   192         ```bash
   193         kbcli alert add-receiver --email='user1@kubeblocks.io,user2@kubeblocks.io' --cluster=mycluster --severity=warning
   194         ```