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

     1  ---
     2  title: Parameter configuration
     3  description: How to configure parameter templates and update parameters in KubeBlocks 
     4  keywords: [parameter configuration]
     5  sidebar_position: 5
     6  sidebar_label: Parameter configuration
     7  ---
     8  
     9  # Parameter configuration
    10  
    11  This tutorial takes Oracle MySQL as an example and explains how to configure parameter templates and parameters in KubeBlocks. You can find [the full PR here](https://github.com/apecloud/learn-kubeblocks-addon/tree/main/tutorial-3-config-and-reconfig/).
    12  
    13  ## Before you start
    14  
    15  1. Grasp basic concepts of Kubernetes, such as Pod and ConfigMap.
    16  2. Finish configurations in [Configure parameter template](./parameter-template.md).
    17  3. (Optional) Know something about Go Template.
    18  4. (Optional)Know something about CUE Lang.
    19  
    20  ## Introduction
    21  
    22  KubeBlocks adds configurations by mounting the ConfigMap to the volume. With a Kubernetes-Native concept that `ConfigMap is the only source of truth`, it centralizes entry for parameter changes in the ConfigMap to prevent configuration drifting. Therefore, the order below illustrates how KubeBlocks performs parameter reconfiguration:
    23  
    24  1. Configure parameter values in the ConfigMap.
    25  2. Derive parameter configurations (add/delete/update) based on ConfigMap modifications.
    26  3. Apply the parameter configurations to the engine.
    27  
    28  Different parameters require different configuration methods:
    29  
    30  - Static parameters require a cluster restart (cold update).
    31  - Dynamic parameters require a parameter refresh (hot update).
    32  
    33  Table 1 lists four common hot update methods, including UNIX Signal, SQL, Auto, etc. Currently, engines in KubeBlocks can implement one or more of these methods. For example, to apply dynamic configuration in PostgreSQL, you can use:
    34  
    35  - UNIX Signal: Send a `SIGHUP` signal.
    36  - Tools: Call `pg_ctl` command.
    37  - SQL: Execute SQL statements to directly update parameters.
    38  
    39  :paperclip: Table 1. Summary of Parameter Hot Updates
    40  
    41  | Methods     | Descriptions | Applicability |
    42  | :---------- | :----------- | :------------ |
    43  | Unix Signal | For example, PostgreSQL. <br /> If you need to reload the configuration file after parameter configuration, send a `SIGHUP` signal to PG. | Applicable to engines that support Unix Signal updates. |
    44  | SQL         | For example, MySQL. <br /> Perform parameter configurations through the SQL statement `SET GLOBAL <var> =<value>`. | Applicable to most RDBMS engines. <br /> **Note**: The `execSQL` interface is required. Currently, KubeBlocks only supports MySQL and PostgreSQL. |
    45  | Tools       | For example, Redis or MongoDB. <br /> Related tools are provided for configuring parameters. | Implemented via custom scripts or local tools, highly versatile. |
    46  | Auto        | The engine itself watches for changes in configuration files, and updates automatically when a change is detected. | Dependent on whether the engine supports automatic loading. |
    47  
    48  As mentioned in [Parameter template](./parameter-template.md), Kubernetes does not synchronously update ConfigMap changes to the Pod. For KubeBlocks, it not only needs to distinguish the way parameters are configured but also needs to watch whether the corresponding configurations are synchronized to the Pod.
    49  
    50  Now take a look at how KubeBlocks manages parameter configurations through the `ConfigConstraint` API.
    51  
    52  ## ConfigConstraint
    53  
    54  As a multi-engine platform, KubeBlocks needs to get the following information to better support parameter configuration:
    55  
    56  1. Format of configuration files:
    57  
    58     Different configuration files have different structures. KubeBlocks parses files based on their structure to deduce the information about each configuration (add/delete/update).
    59  
    60  2. Effect scope of parameters:
    61  
    62     Be clear which parameters are dynamic, which are static, and which are immutable. KubeBlocks specifies the effect scope of parameters to determine how they quickly take effect.
    63  
    64  3. Methods for dynamic parameter changes:
    65  
    66     As shown in Table 1., parameters can be dynamically configured in various ways. Therefore, specify different dynamic configuration methods for different engines.
    67  
    68  4. Definition of parameter validation rules:
    69  
    70     It's important to define validation rules for parameters. In a production environment, developers often fail to start databases due to typos in parameter values. Parameter validation therefore adds a layer of protection by performing checks in advance to prevent such mistakes.
    71  
    72  :::note
    73  
    74  KubeBlocks creates a config-manager sidecar for components that have configured ConfigConstraint. It is used to detect file updates, send signals, and execute updated scripts.
    75  
    76  :::
    77  
    78  The information is included in `ConfigConstraint` (parameter constraints) as shown below. The four sections correspond to the four key configuration details mentioned above.
    79  
    80  ```yaml
    81  apiVersion: apps.kubeblocks.io/v1alpha1
    82  kind: ConfigConstraint
    83  metadata:
    84    name: oracle-mysql-config-constraints
    85  spec:
    86    #1. Specify the file format as INI and only focus on the `mysqld` section
    87    formatterConfig:
    88      format: ini
    89      iniConfig:
    90        sectionName: mysqld
    91  
    92    #2. Specify the dynamic parameter configuration method for MySQL, using `reload-script` to execute SQL statements
    93    reloadOptions:
    94      tplScriptTrigger:
    95        sync: true
    96        # Specify which script file to use for configuration
    97        scriptConfigMapRef: oracle-mysql-reload-script
    98        namespace: {{ .Release.Namespace }}
    99  
   100    ##3.1 Configure static parameters
   101    staticParameters:
   102      - open_files_limit
   103      - performance_schema
   104      - enforce_gtid_consistency
   105  
   106    ##3.2 Configure dynamic parameters
   107    dynamicParameters:
   108      - innodb_buffer_pool_size
   109      - max_connections
   110      - gtid_mode
   111  
   112    ##4. Define parameter validation rules with a CUE template
   113    cfgSchemaTopLevelName: MysqlParameter
   114    configurationSchema:
   115      cue: |-
   116        {{- .Files.Get "config/oracle-mysql-config-constraint.cue" | nindent 6 }}
   117  ```
   118  
   119  Each API is to be explained in the following tutorial.
   120  
   121  ### FormatterConfig
   122  
   123  FormatterConfig describes the configuration file format, such as `ini`, `yaml`, `json`, `xml`, `properties`.
   124  
   125  The file itself is just a text and requires different parsers.
   126  
   127  When KubeBlocks detects a configuration file change, it deduces the parameter configuration (add/delete/update) based on the format and notifies the Pod to update.
   128  
   129  For example, MySQL's adjustable parameters take the `ini` format and only parse the `mysqld` information.
   130  
   131  ```bash
   132    formatterConfig:
   133      format: ini               # Format of the configuration file and ini, xml, yaml, json and hcl are supported
   134      iniConfig:
   135        sectionName: mysqld     # If the ini format is adopted, there might be multiple sections and sectionName is required
   136  ```
   137  
   138  ### ReloadOptions
   139  
   140  ReloadOptions describes the method of dynamic parameter configuration.
   141  
   142  Table 1 above summarizes 4 common methods of dynamic parameter configuration. KubeBlocks, accordingly, supports multiple configuration methods.
   143  
   144  - tplScriptTrigger: Configures parameter by template files.
   145  - shellTrigger: Configures parameters by executing scripts.
   146  - unixSignalTrigger: Configures parameters through UNIX Signal.
   147  - None: AutoLoad mode, which is automatically configured by the database engine.
   148  
   149  ***Example***
   150  
   151  - tplScriptTrigger
   152  
   153    This example chooses `tplScriptTrigger` to configure parameters by defining the content in the template file.
   154  
   155    ```bash
   156      reloadOptions:
   157        tplScriptTrigger:                                 # Configure parameters by template file
   158          sync: true                                      # Synchronou reloading
   159          scriptConfigMapRef: oracle-mysql-reload-script  # The referenced template file 
   160          namespace: {{ .Release.Namespace }}
   161    ```
   162  
   163  - shellTrigger
   164  
   165    `shellTrigger` performs dynamic parameter configuration by shell scripts, which is a general method since most databases support configuring parameters through clients.
   166  
   167    ```yaml
   168      reloadOptions:
   169        shellTrigger:
   170          sync: true
   171          command:
   172            - "update-dynamic-config.sh" 
   173    ```
   174  
   175  :::note
   176  
   177  The scripts in Reloadptions will be loaded to the Pod and executed by the config-manager sidecar mentioned before.
   178  
   179  :::
   180  
   181  ### Static/Dynamic Parameters
   182  
   183  KubeBlocks supports configuring dynamic, static and immutable parameters and such effect scope is used to identify the parameter type and to determine how the parameter reconfiguration takes effect.
   184  
   185  KubeBlocks includes multiple parameter reloading strategies and applies the appropriate strategy based on the reconfiguration contents.
   186  
   187  This example lists some common MySQL parameters, such as the static parameter `performance_schema` and the dynamic parameter `max_connection`.
   188  
   189  If the parameter list is too long, it is recommended to use the `.Files.Get` function.
   190  
   191  ```yaml
   192    ##3.1 Configure static parameter list
   193    staticParameters:
   194      - open_files_limit
   195      - performance_schema
   196      - enforce_gtid_consistency
   197  
   198    ##3.2 Configure dynamic parameter list
   199    dynamicParameters:
   200      - innodb_buffer_pool_size
   201      - max_connections
   202      - gtid_mode
   203  ```
   204  
   205  ### ConfigurationSchema
   206  
   207  During the configuration process, starting a cluster may fail due to entering an invalid parameter value.
   208  
   209  KubeBlocks provides ConfigurationSchema for validating parameter effectiveness. KubeBlocks uses CUE for verification. It works by describing the type, default value and range of each parameter to prevent problems caused by an invalid parameter value.
   210  
   211  This example illustrates the configuration for verifying MySQL parameter values.
   212  
   213  ```yaml
   214  #MysqlParameter: {
   215  
   216      // Sets the autocommit mode
   217      autocommit?: string & "0" | "1" | "OFF" | "ON"
   218  
   219      open_files_limit: int | *5000
   220  
   221      // Enables or disables the Performance Schema
   222      performance_schema: string & "0" | "1" | "OFF" | "ON" | *"0"
   223      
   224      // The number of simultaneous client connections allowed.
   225      max_connections?: int & >=1 & <=100000    
   226      ...
   227   }
   228  ```
   229  
   230  For example, the example above defines some constraints for the parameter `performance_schem` in MySQL.
   231  
   232  - Type: string
   233  - Available values: ON, OFF, 0, 1
   234  - Default value: 0
   235  
   236  ```yaml
   237      // Enables or disables the Performance Schema
   238      performance_schema: string & "0" | "1" | "OFF" | "ON" | *"0"
   239  ```
   240  
   241  ## How to configure parameters
   242  
   243  Better user experience, KubeBlocks offers kbcli for your convenient parameter management.
   244  
   245  ### Create a cluster
   246  
   247  Both kbcli and Helm are supported.
   248  
   249  <Tabs>
   250  
   251  <TabItem value="kbcli" label="kbcli" default>
   252  
   253  ```bash
   254  kbcli cluster create mycluster --cluster-definition='oracle-mysql' --cluster-version oracle-mysql-8.0.32
   255  ```
   256  
   257  </TabItem>
   258  
   259  <TabItem value="Helm" label="Helm">
   260  
   261  ```bash
   262  helm install oracle-mysql path-to-your-helm-chart/oracle-mysql
   263  ```
   264  
   265  </TabItem>
   266  
   267  </Tabs>
   268  
   269  ### View parameter configuration
   270  
   271  View the detailed configuration of a cluster, including the configuration template name and constraint name.
   272  
   273  ```bash
   274  kbcli cluster describe-config mycluster
   275  >
   276  ConfigSpecs Meta:
   277  CONFIG-SPEC-NAME   FILE     ENABLED   TEMPLATE                       CONSTRAINT                        RENDERED                            COMPONENT    CLUSTER
   278  mysql-config       my.cnf   true      oracle-mysql-config-template   oracle-mysql-config-constraints   mycluster-mysql-comp-mysql-config   mysql-comp   mycluster
   279  
   280  History modifications:
   281  OPS-NAME   CLUSTER   COMPONENT   CONFIG-SPEC-NAME   FILE   STATUS   POLICY   PROGRESS   CREATED-TIME   VALID-UPDATED
   282  ```
   283  
   284  ### Configure parameters
   285  
   286  For example, configure the max_connection of MySQL.
   287  
   288  Based on the above configuration,
   289  
   290  - max_connection is a dynamic parameter.
   291  - The value range is [1, 10000].
   292  
   293  :::note
   294  
   295  For KubeBlocks v0.6.0 and above, run `kbcli cluster edit-config` to configure the parameter.
   296  
   297  :::
   298  
   299  ```bash
   300  kbcli cluster edit-config mycluster
   301  ```
   302  
   303  In the interactive editing interface, edit max_connection as 1000.
   304  
   305  ![Interactive Config Editor](./../../img/addon-interactive-config-editor.png)
   306  
   307  Save the changes and confirm the information to realize the parameter reconfiguration.
   308  
   309  ![Confirm Config Changes](./../../img/addon-confirm-config-changes.png)
   310  
   311  ### View the change history
   312  
   313  View the parameter configurations again. Besides the parameter template, the history and detailed information are also recorded.
   314  
   315  ```bash
   316  kbcli cluster describe-config mycluster
   317  >
   318  ConfigSpecs Meta:
   319  CONFIG-SPEC-NAME   FILE     ENABLED   TEMPLATE                       CONSTRAINT                        RENDERED                            COMPONENT    CLUSTER
   320  mysql-config       my.cnf   true      oracle-mysql-config-template   oracle-mysql-config-constraints   mycluster-mysql-comp-mysql-config   mysql-comp   mycluster
   321  
   322  History modifications:
   323  OPS-NAME                        CLUSTER     COMPONENT    CONFIG-SPEC-NAME   FILE     STATUS    POLICY   PROGRESS   CREATED-TIME                 VALID-UPDATED
   324  mycluster-reconfiguring-7p442   mycluster   mysql-comp   mysql-config       my.cnf   Succeed            1/1        Aug 25,2023 18:27 UTC+0800   {"my.cnf":"{\"mysqld\":{\"max_connections\":\"1000\"}}"}
   325  ```
   326  
   327  ## Reference
   328  
   329  - [Configure a Pod to Use a ConfigMap](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/)
   330  - [CUE Lang Overview](https://cuetorials.com/zh/overview/)
   331  - [KubeBlocks ApeCloud MySQL Configuration](https://kubeblocks.io/docs/preview/user_docs/kubeblocks-for-mysql/configuration)
   332  
   333  ## Appendix
   334  
   335  ### A.1 How to view the reconfiguration process
   336  
   337  Parameter configuration is a type of KubeBlocks operations, shorten as ops.
   338  
   339  After the kbcli reconfiguration command is performed, a Configuration ops is generated in KubeBlocks.
   340  
   341  As shown in Section 3.5, an ops named `mycluster-reconfiguring-7p442` is generated and you can run the command below to view the process, including the changes, policy and time.
   342  
   343  ```bash
   344  kbcli cluster describe-op <your-reconfig-ops-name>
   345  ```
   346  
   347  ### A.2 Compare the difference between two changes
   348  
   349  Run `diff-config` to view the difference between two changes
   350  
   351  ```bash
   352  kbcli cluster diff-config <your-reconfig-ops1> <your-reconfig-ops2>
   353  ```