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

     1  ---
     2  title: Advanced logs
     3  description: How to configure advanced log function for a database using KubeBlocks v0.6.0
     4  sidebar_position: 4
     5  sidebar_label: Advanced logs
     6  ---
     7  
     8  # Advanced logs
     9  
    10  Currently, KubeBlocks supports viewing the running logs, error logs, and slow logs of the running database cluster through `kbcli cluster logs`. This method is lightweight and only relies on the log files and tail commands in the target container, without installing any software or plug-ins in the cluster or client.
    11  
    12  Its drawbacks are also very obvious. It strongly depends on the survival of the container and the current status of log files. If the container is already abnormal or the log file is deleted, this method cannot work as expected. The above method is called the `cluster logs` function.
    13  
    14  The `advanced logs` function can better retain log clues at problem sites, support long-term remote backup of log data, and facilitate integration with external systems.
    15  
    16  The advanced logs function supports storing log data to the Loki server in the cluster and the remote AWS S3 service. The data stored in Loki supports `logcli` client and Grafana console access and display. The data stored in S3 supports importing and integrating with other systems for further processing and analysis.
    17  
    18  ## Add-on introduction
    19  
    20  Agamotto, Loki, Prometheus, and other components related to observability are managed by KubeBlocks as add-ons. Each of these add-ons has its specialties.
    21  
    22  * Agamotto is a collection add-on that collects performance and logs.
    23  * Loki is a storage add-on that stores log data.
    24  * Grafana is a front-end display add-on that displays the observed data.
    25  * Logcli is a front-end search add-on that provides the native LogQL query capability. It is installed by `kbcli plugin`.
    26  
    27  ## Before you start
    28  
    29  * [Install kubectl](https://kubernetes.io/docs/tasks/tools/).
    30  * [Install KubeBlocks by kbcli](./../installation/install-with-kbcli/install-kubeblocks-with-kbcli.md) or [install KubeBlocks by Helm](./../installation/install-with-helm/install-kubeblocks-with-helm.md).
    31  
    32  ## Enable advanced logs function
    33  
    34  ### Enable Loki
    35  
    36  The Loki add-on stores the log data and accepts the search request from the front end. Refer to the [Loki official documentation](https://grafana.com/docs/loki/latest/) for details.
    37  
    38  :::caution
    39  
    40  Before enabling the advanced logs function, deploy a Loki node first and then apply Agamotto collection to avoid log data loss.
    41  
    42  :::
    43  
    44  1. View the Loki add-on status to check whether it is enabled.
    45  
    46     ```bash
    47     kbcli addon list | grep loki
    48     ```
    49  
    50  2. If this add-on is not enabled, run the command below to enable this add-on.
    51  
    52     This command runs Loki in the `statefulset` mode. KubeBlocks deploys a single-node Loki of the single binary type, i.e. the `target` in the configuration is `all`, uses a 10-GB PV to store data, and starts a `loki-gateway` service to receive data.
    53  
    54     ```bash
    55     kbcli addon enable loki
    56     >
    57     NAME                           TYPE   PROVIDER    STATUS     AUTO-INSTALL   AUTO-INSTALLABLE-SELECTOR   
    58     loki                           Helm   apecloud    Enabled    false   
    59     ```
    60  
    61     Check whether this Pod is running.
    62  
    63     ```bash
    64     kubectl get pods | grep loki
    65     >
    66     NAME                                            READY   STATUS    RESTARTS   AGE
    67     kb-addon-loki-0                                 1/1     Running   0          47h
    68     loki-gateway-5f4895c7b-5khv7                    1/1     Running   0          47h
    69     ```
    70  
    71  3. Run Grafana to view the status of Loki. For example,
    72  
    73     ![Loki in Grafana]
    74  
    75  4. (Optional) Disable the Agamotto add-on.
    76  
    77     ```bash
    78     kbcli disabled loki
    79     ```
    80  
    81  By default, Loki saves the log data of the last 3 days, and the data older than 72 hours will be automatically deleted. You can use the following commands to adjust the storage policy. Note that when adjusting the policy, you need to pay attention to the host's disk resources and Loki PVC. Currently, Loki does not support size-based retention.
    82  
    83  ```bash
    84  # The new retention_period must be an integer multiple of 24
    85  kbcli addon enable loki --set loki.loki.limits_config.retention_period=168h
    86  ```
    87  
    88  Run `kubectl get pv` to view the PV information of the current Loki instance.
    89  
    90  ```bash
    91  kubectl get pv
    92  >
    93  NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                               STORAGECLASS      REASON   AGE
    94  pvc-ed20ec94-9a58-46e4-9c28-b692cba70e79   8Gi        RWO            Delete           Bound    kb-system/storage-kb-addon-loki-0   csi-hostpath-sc            28h
    95  ```
    96  
    97  If you want to check the disk usage of the current Loki node, you can use the `exec` command to log in to the container and use the `du` command to check the disk usage. For example,
    98  
    99  1. Log in to the Loki container.
   100  
   101     ```bash
   102     kubectl exec -it kb-addon-loki-0 -n kb-system -- /bin/sh
   103     ```
   104  
   105  2. Run the `du` command.
   106  
   107     ```bash
   108     du -sh /var/loki/*
   109     >
   110     152.0K  /var/loki/boltdb-shipper-active
   111     228.0K  /var/loki/boltdb-shipper-cache
   112     7.4M    /var/loki/chunks
   113     4.0K    /var/loki/compactor
   114     64.0K   /var/loki/retention
   115     4.0K    /var/loki/rules
   116     3.5M    /var/loki/wal
   117     ```
   118  
   119  ### Enable Agamotto
   120  
   121  After the log collection function is enabled, Agamotto detects two localhost folders, `/var/log/pods` and `/var/log/kubeblocks`. All files that fit the following patterns will be collected.
   122  
   123  * `/var/log/pods` directory: it stores the standard output of the storage container and the error logs. The file path pattern follows `/var/log/pods/**/**/*.log`.
   124  * `/var/log/kubeblocks` directory: it can customize the storage of log files storage. By default, KubeBlocks stores the logs of database engines, such as error log and slow log. The file path pattern follows `/var/log/kubeblocks/**/**/*.log`.
   125  
   126  1. View the Agamotto add-on status to check whether it is enabled.
   127  
   128     ```bash
   129     kbcli addon list | grep agamotto
   130     ```
   131  
   132  2. If this add-on is not enabled, run the command below to enable this add-on.
   133  
   134     ```bash
   135     kbcli addon enable agamotto
   136     >
   137     NAME                           TYPE   PROVIDER    STATUS     AUTO-INSTALL   AUTO-INSTALLABLE-SELECTOR   
   138     agamotto                       Helm   apecloud    Enabled    false  
   139     ```
   140  
   141     Check whether this Pod is running.
   142  
   143     ```bash
   144     kubectl get pods | grep agamotto
   145     >
   146     NAME                                            READY   STATUS    RESTARTS   AGE
   147     kb-addon-agamotto-kvbr5                         1/1     Running   0          44h
   148     ```
   149  
   150  3. Run the command to upgrade the log function of Agamotto.
   151  
   152     ```bash
   153     kbcli addon enable agamotto --set log.enabled=true
   154     ```
   155  
   156  4. Upload logs to S3.
   157  
   158     By default, the logs function only restores the log data on the Loki server of the current Kubernetes cluster and does not upload the log data to the remote S3 server.
   159  
   160     Fill in the command below with Access Key, Secret Key, region, and bucket, and run this command to enable uploading log data to S3.
   161  
   162     ```bash
   163     kbcli addon enable agamotto --set log.enabled=true,log.s3.enabled=true,log.s3.accessKey=user_ak,log.s3.secretKey=user_sk,log.s3.region=user_region,log.s3.bucket=user_bucket
   164     ```
   165  
   166     The target bucket of the remote S3 creates a directory named after the filename, which stores the real-time data Partition of the corresponding log (partitioned by 5 minutes or 5 MB).
   167  
   168  5. (Optional) Disable the Agamotto add-on.
   169  
   170     ```bash
   171     kbcli addon disable agamotto
   172     ```
   173  
   174  ### Enable Grafana
   175  
   176  :::note
   177  
   178  In the production environment, all monitoring add-ons are disabled by default when installing KubeBlocks and Grafana is also included. You can enable Grafana but it is highly recommended to build your monitoring system or purchase a third-party monitoring service for safety and stability concerns. For integrating a third-party monitoring service, refer to [Integrate a third party monitoring service](./monitor-database.md#for-production-environment).
   179  
   180  :::
   181  
   182  1. Check whether the Loki and Grafana services are enabled.
   183  
   184     ```bash
   185     kbcli dashboard list
   186     >
   187     NAME                 NAMESPACE   PORT    CREATED-TIME                 
   188     kubeblocks-grafana   default     13000   Jul 08,2023 15:37 UTC+0800   
   189     kubeblocks-logs      default     13100   Jul 08,2023 15:36 UTC+0800  
   190     ```
   191  
   192  2. Open the dashboard of the container logs.
   193  
   194     ```bash
   195     kbcli dashboard open kubeblocks-logs
   196     ```
   197  
   198     Or you can go to the **Explore** page and select **Loki** to search the logs.
   199  
   200     ![Loki on Explore page](./../../img/logs-grafana.png)
   201  
   202  ### Enable LogCLI
   203  
   204  kbcli supports the LogCLI plug-in for your convenience of querying logs by command line.
   205  
   206  :::note
   207  
   208  For more LogCLI and LogQL grammar, refer to the [Loki official documentation](https://grafana.com/docs/loki/latest/tools/logcli/).
   209  
   210  :::
   211  
   212  1. Install the LogCLI plug-in.
   213  
   214     1. Set the environment variable.
   215  
   216        ```bash
   217        export PATH="${KBCLI_ROOT:-$HOME/.kbcli}/plugins/bin:$PATH"
   218        ```
   219  
   220     2. Add the `block-index` of ApeCloud as the `krew index`.
   221  
   222        ```bash
   223        kbcli plugin index add default https://github.com/apecloud/block-index.git
   224        ```
   225  
   226     3. View the index status.
   227  
   228        ```bash
   229        kbcli plugin index list
   230        >
   231        INDEX     URL                                           
   232        default   https://github.com/apecloud/block-index.git   
   233        ```
   234  
   235     4. Install the LogCLI plug-in.
   236  
   237        ```bash
   238        kbcli plugin install default/logcli 
   239        ```
   240  
   241     5. View the plug-in status and check whether LogCLI is installed successfully.
   242  
   243        ```bash
   244        kbcli plugin list | grep logcli
   245        ```
   246  
   247  2. Use the LogCLI.
   248  
   249     1. Set the environment variable of LogCLI. For example, set the `LOKI_ADDR` pointing to the localhost 3100 port.
   250  
   251        ```bash
   252        export LOKI_ADDR=http://localhost:3100
   253        ```
   254  
   255     2. Export the Loki service to the localhost. Note that `port/namespace` should be the same as the settings in the system. For example,
   256  
   257        ```bash
   258        kubectl port-forward svc/loki-gateway 3100:80 -n kb-system
   259        ```
   260  
   261     3. Use the LogCLi tool to query log data.
   262  
   263        ```bash
   264        kbcli logcli query '{exporter="OTLP"}'
   265        >
   266        2023/07/10 17:30:18 http://localhost:3100/loki/api/v1/query_range?direction=BACKWARD&end=1688981418606367000&limit=30&query=%7Bexporter%3D%22OTLP%22%7D&start=1688977818606367000
   267        2023/07/10 17:30:18 Common labels: {exporter="OTLP", host_ip="0.0.0.0", host_name="minikube", log_file_name="0.log", restart_num="0"}
   268        2023-07-10T17:30:18+08:00 {container="nginx", log_file_path="/var/log/pods/kb-system_loki-gateway-748cbf767f-6jpdw_54c6db59-10d5-419b-963e-0f116358e14a/nginx/0.log", namespace="kb-system", pod="loki-gateway-748cbf767f-6jpdw", pod_id="54c6db59-10d5-419b-963e-0f116358e14a"}                                                             {"log":"172.17.0.1 - - [10/Jul/2023:09:30:18 +0000]  204 \"POST /loki/api/v1/push HTTP/1.1\" 0 \"-\" \"Go-http-client/1.1\" \"-\"\n","stream":"stderr","time":"2023-07-10T09:30:18.230393596Z"}
   269        2023-07-10T17:30:18+08:00 {container="nginx", log_file_path="/var/log/pods/kb-system_loki-gateway-748cbf767f-6jpdw_54c6db59-10d5-419b-963e-0f116358e14a/nginx/0.log", namespace="kb-system", pod="loki-gateway-748cbf767f-6jpdw", pod_id="54c6db59-10d5-419b-963e-0f116358e14a"}
   270        ```
   271  
   272  ### MySQL Slow logs and error logs
   273  
   274  By default, in the ClusterDefinition of MySQL, there is a sidecar container named `metrics` in the podSpec definition of the `mysql component`.
   275  
   276  This `metrics` container detects the error logs and slow logs output by the MySQL in real-time, and dumps the logs to the host's `/var/log/KubeBlocks` directory. To prevent taking up too much host disk space, the default size of each file is 10 MB, and a maximum of 2 files can be saved.
   277  
   278  In addition, the Agamotto component deployed on the host detects the `/var/log/KubeBlocks` folder in real-time and collects the log data in it to complete the entire log data collection link.
   279  
   280  ```yaml
   281  - name: metrics
   282    image:  {{ .Values.metrics.image.registry | default "docker.io" }}/{{ .Values.metrics.image.repository }}:{{ .Values.metrics.image.tag }}
   283    imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
   284    securityContext:
   285      runAsNonRoot: false
   286      runAsUser: 0
   287    env:
   288      - name: DB_TYPE
   289        value: MySQL
   290      - name: ENDPOINT
   291        value: {{ printf "localhost:3306" }}
   292      - name: MYSQL_USER
   293        valueFrom:
   294          secretKeyRef:
   295            name: $(CONN_CREDENTIAL_SECRET_NAME)
   296            key: username
   297            optional: false
   298      - name: MYSQL_PASSWORD
   299        valueFrom:
   300          secretKeyRef:
   301            name: $(CONN_CREDENTIAL_SECRET_NAME)
   302            key: password
   303            optional: false
   304    command:
   305      - "/bin/agamotto"
   306      - "--config=/opt/agamotto/agamotto-config.yaml"
   307    ports:
   308      - name: http-metrics
   309        containerPort: {{ .Values.metrics.service.port }}
   310    volumeMounts:
   311      - name: agamotto-configuration
   312        mountPath: /opt/agamotto
   313      - name: data
   314        mountPath: /data/mysql
   315      - name: log-data
   316        mountPath: /var/log/kubeblocks
   317        readOnly: false
   318  ```