Component Config-File Practice

This article will introduce the configuration files in Kato, and will take mariadb as an example to demonstrate the use of configuration files.

Introduction to Configuration Files

Kato combines the configuration file implemented by Kubernetes' ConfigMap, which is a special storage type that allows users to directly define the content of the file, usually referring to the configuration file.

The configuration file has two major features: dynamic rendering environment variables and configuration file sharing.

Dynamically Render Environment Variables

The syntax of dynamic rendering configuration file parsing environment variables:

${environment variable name}
${Environment variable name: default value}

If the specified environment variable exists in the component, then Kato will resolve the value of the environment variable to the configuration file; if the environment variable does not exist in the component, then Kato will resolve the default? value to the configuration file in.

If the specified environment variable does not exist and the default value is not set, then Kato will not parse

Configure File Dharing

The configuration file can be shared through the storage sharing mechanism. If you have multiple components using the same configuration file, you can share it directly without editing the settings multiple times. The shared configuration file will only parse the environment variables of the current component.

Configuration File Components in Mariadb

Mariadb, the official image of Docker, gives two ways to customize MySQL configuration files.

Mariadb Official Configuration Method

The first method is to create a configuration file on the host and mount this configuration file to the container’s /etc/mysql/conf.d directory?. The configuration under /etc/mysql/conf.d The file will overwrite the default configuration file /etc/mysql/my.cnf. This method is not flexible enough, and it is impossible to confirm when the component is created on which node the Pod will be scheduled (the data center is usually a cluster ), the configuration file can be mounted after the component is created, and then restart the component to make it take effect.

The second method is to pass in the mysqld? parameter in the docker run command, for example, through the character-set-server and collation-server two parameters to modify the default encoding and collation rules:

docker run --name some-mariadb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

This method is also not flexible enough. If there are too many parameters to be set, the docker run command will be very long.

Kato Configuration Method

The third method is a configuration file implemented by Kato combined with k8s ConfigMap. This method is more flexible, and can be dynamically rendered environment variables, and can also be shared with other components.

Create a Configuration File

When creating mariadb, check the build and start button, as shown in the figure:

Add a configuration file in the application page environment configuration –> configuration file settings, and the mount path is /etc/mysql/mariadb.cnf; as shown in the figure:

The complete content of the configuration file is as follows:

# MariaDB-specific config file.
# Read by /etc/mysql/my.cnf

[client]
# Default is Latin1, if you need UTF-8 set this (also in server section)
default-character-set = ${DEFAULT_CHARACTER_SET:utf8}

[mysqld]
#
# * Character sets
#
# Default is Latin1, if you need UTF-8 set all this (also in client section)
#
character-set-server  = ${CHARACTER_SET_SERVER:utf8}
collation-server      = ${COLLATION_SERVER:utf8_general_ci}
character_set_server  = ${CHARACTER_SET_SERVER:utf8}
collation_server      = ${COLLATION_SERVER:utf8_general_ci}

default_storage_engine = ${DEFAULT_STORAGE_ENGINE:innodb}

# Import all .cnf files from configuration directory
!includedir /etc/mysql/mariadb.conf.d/

In this configuration file, we set DEFAULT_CHARACTER_SET, CHARACTER_SET_SERVER, DEFAULT_STORAGE_ENGINE and other variables that can parse environment variables, and set default values ​​for them.

Add Environment Variables

Add environment variables DEFAULT_STORAGE_ENGINE=myisam and MYSQL_ROOT_PASSWORD=kato to the component. After mariadb is created successfully, enter the container and check the contents of /etc/mysql/mariadb:

root@gr52b3ee-0:/# cat /etc/mysql/mariadb.cnf 
# MariaDB-specific config file.
# Read by /etc/mysql/my.cnf

[client]
# Default is Latin1, if you need UTF-8 set this (also in server section)
default-character-set = utf8

[mysqld]
#
# * Character sets
#
# Default is Latin1, if you need UTF-8 set all this (also in client section)
#
character-set-server  = utf8
collation-server      = utf8_general_ci
character_set_server  = utf8
collation_server      = utf8_general_ci

default_storage_engine = myisam

# Import all .cnf files from configuration directory
!includedir /etc/mysql/mariadb.conf.d/

As you can see, Kato did not find DEFAULT_CHARACTER_SET, CHARACTER_SET_SERVER, etc. There are no variables corresponding to the environment variables, so they are analyzed with their corresponding default values; when the environment variables corresponding to DEFAULT_STORAGE_ENGINE are found, the environment variables are used. DEFAULT_STORAGE_ENGINE`’s? Value myisam is analyzed.

Let’s log in to MySQL again to see if these configurations take effect:

# Login to MySQL
root@gr52b3ee-0:/# mysql -uroot -pkato

# Check coding and proofreading rules
MariaDB [(none)]> show variables like "%character%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

# View storage engine
MariaDB [(none)]> SHOW STORAGE ENGINES;
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                                          | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| MRG_MyISAM         | YES     | Collection of identical MyISAM tables                                            | NO           | NO   | NO         |
| CSV                | YES     | Stores tables as CSV files                                                       | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables                        | NO           | NO   | NO         |
| MyISAM             | DEFAULT | Non-transactional engine with good performance and small data footprint          | NO           | NO   | NO         |
| Aria               | YES     | Crash-safe tables with MyISAM heritage                                           | NO           | NO   | NO         |
| InnoDB             | YES     | Supports transactions, row-level locking, foreign keys and encryption for tables | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                                               | NO           | NO   | NO         |
| SEQUENCE           | YES     | Generated tables filled with sequential values                                   | YES          | NO   | YES        |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+

It can be seen that the encoding, proofreading rules, and storage engine settings have taken effect.

Sharing Configuration Files

Next, we create another Mariadb, in the application interface Environment Configuration –>Shared Configuration File–> Mount Shared Configuration File to mount the mariadb configuration file created above. As shown in the figure:

After the creation is complete, you can see the fragment of the file /etc/mysql/mariadb.cnf as follows:

default_storage_engine = innodb

As you can see, because the current component does not set the DEFAULT_STORAGE_ENGINE variable, Kato uses its default? value innodb for analysis.

Summary

This article uses mariadb as an example to demonstrate the use of Kato configuration files, including rendering of environment variables and sharing configuration files. I hope that after reading this article, you can configure components flexibly.