Kubernetes management of MySQL application configuration file in container with Configmap

Source: Internet
Author: User
Tags reserved

Understanding of the role of 1.configmap

What role does the Configmap play?
For example, enable a MySQL container. In general, the MySQL container is important for two parts, one for storing data and the other for configuration file MY.CNF.
As previously tested, the storage data can be decoupled with the PV PVC implementation and the separation of the containers.
The configuration file can also be decoupled from the container, which means that the MySQL container can read directly and use preconfigured configuration files (rather than using the default own configuration file in the container), which is very handy. This is the function of Configmap.

Kubernetes uses Configmap to implement configuration file management for applications in the container.

2. Create Configmap

There are two ways to create Configmap, one is created from a Yaml file, and the other is created directly from the command line via Kubectl.

Find a usable configuration of my.cnf file
Test now, as simple as possible.
My See below:

Attention:
Why this name is MYSQLD.CNF, is because the container read the configuration file name is this, the principle of least modification, directly replace the overwrite, but also with the original name.

Create Configmap with this file

[[email protected] wp]# kubectl create configmap mysql-config --from-file=mysqld.cnfconfigmap "mysql-config" created[[email protected] wp]# kubectl describe configmap mysql-configName:? ? ? ?? mysql-configNamespace:? ? defaultLabels:? ? ?? <none>Annotations:? <none>Data====mysqld.cnf:......

3. Mount the MySQL container in volume form and read this file to start

Where is it mounted?
Be sure to mount it to the default storage profile and replace it.
The default read configuration file path for the MySQL container is shown below:

[email protected]:/etc/mysql# pwd/etc/mysql[email protected]:/etc/mysql# lsconf.d? ? ? ? my.cnf? ? ? ? my.cnf.fallback? mysql.cnf? mysql.conf.d[email protected]:/etc/mysql#

The contents of MY.CNF are as follows:

[email protected]:/etc/mysql# cat my.cnf# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; version 2 of the License.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA!includedir /etc/mysql/conf.d/!includedir /etc/mysql/mysql.conf.d/

Look at the contents of these two directories separately

[email protected]:/etc/mysql/mysql.conf.d# Cat mysqld.cnf # Copyright (c), Oracle and/or its affiliates. All rights reserved.## This program is free software; Can redistribute it and/or modify# it under the terms of the GNU general public License as published by# the free Soft Ware Foundation; Version 2 of the license.## This program was distributed in the hope so it would be a useful,# but without any WARRANTY;  Without even the implied warranty of# merchantability or FITNESS for A particular PURPOSE.  See the# GNU General public License-details.## you should has received a copy of the GNU General public license# Along with the program;  If not, write to the free software# Foundation, Inc., Wuyi Franklin St, Fifth Floor, Boston, MA 02110-1301 usa## the MySQL Server configuration file.## for explanations see# http://dev.mysql.com/doc/mysql/en/server-system-variables.html[ Mysqld]pid-file =/var/run/mysqld/mysqld.pidsocket =/var/run/mysqld/mysqld.sockdaTadir =/var/lib/mysql#log-error =/var/log/mysql/error.log# By default we only accept connections from Localhost#bin d-address = 127.0.0.1# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[email&  nbsp;protected]:/etc/mysql/mysql.conf.d#

You can see the most important configuration file is this, configmap mount here, with our pre-set configuration file to replace the mysqld.cnf here
The path here is:

[email protected]:/etc/mysql/mysql.conf.d# pwd/etc/mysql/mysql.conf.d[email protected]:/etc/mysql/mysql.conf.d# lsmysqld.cnf

The following are the service and pod configuration files:

  [[email protected] wp]# cat mysql-svc2.ymlapiversion:v1kind:servicemetadata:? Name:mysql2spec:? Ports:? -port:3306? Selector:?? APP:MYSQL1---apiversion:apps/v1beta1kind:deploymentmetadata:? Name:mysql-t1spec:? Selector:?? Matchlabels:??? App:mysql1? Template:??? Metadata:???? Labels:????? App:mysql1??? Spec:???? Containers:???? -image:mysql:5.7????? Name:mysql-t????? Env:????? -Name:mysql_root_password?????? Valuefrom:??????? Secretkeyref:???????? Name:mysecret???????? Key:password????? Ports:????? -containerport:3306?????? Name:mysql????? Volumemounts:????? -Name:mysql-t1?????? Mountpath:/ETC/MYSQL/MYSQL.CONF.D # #注意路径???? Volumes:????? -Name:mysql-t1?????? Configmap:??????? Name:mysql-config  
[[email protected] wp]# kubectl apply -f mysql-svc2.ymlservice "mysql2" createddeployment.apps "mysql-t1" created

Get up.

[[email protected] wp]# kubectl get pod-owidename ready STATUS restarts age        IP NODEHTTPD-749BF8C6F4-BFJFW 1/1 Running 1 5d 10.244.2.4 KUBERNETES3HTTPD-749BF8C6F4-GHPZL 1/1 Running 1 5d 10.244.2.5 kubernetes3http D-749bf8c6f4-xvrn4 1/1 Running 1 5d 10.244.2.253 KUBERNETES3MYSQL-7DB74785B4-2MK           4r 1/1 Running 0 1h 10.244.1.29 Kubernetes2mysql-t1-6fbf57db97-z5rtl 1/1 Running 0 13s 10.244.1.36 kubernetes2nginx-deployment-6b5c99b6fd-pscr6 1/1 Runnin G 1 5d 10.244.1.27 kubernetes2nginx-deployment-6b5c99b6fd-zr2p7 1/1 Running 1 5 D 10.244.2.254 kubernetes3node-exporter-4gbh9 1/1 Running 41d 192.168 .211.152 kubernetes3noDE-EXPORTER-8H9VP 1/1 Running 41d 192.168.211.151 kubernetes2wordpress-pod-7dd7  659959-HC7MR 1/1 Running 5 8d 10.244.2.2 kubernetes3[[email protected] wp]#

Into the container check is not read to our configuration file

[[email protected] ~]# docker psCONTAINER ID        IMAGE                                                                                       COMMAND                  CREATED              STATUS              PORTS               NAMESc78f97476162        66bc0f66b7af                                                                                "docker-entrypoint..."   About a minute ago   Up About a minute                       k8s_mysql-t_mysql-t1-6fbf57db97-z5rtl_default_72eeb07c-9a27-11e8-8e76-000c292f3b91_0
[email protected]:/etc/mysql/mysql.conf.d# lsmysqld.cnf[email protected]:/etc/mysql/mysql.conf.d# cat mysqld.cnf [client]port = 3306socket = /var/run/mysqld/mysqld.sock[mysql]no-auto-rehash[mysqld]user = mysqlport = 3306socket = /var/run/mysqld/mysqld.sockdatadir = /var/lib/mysql[mysqld_safe]log-error= /var/log/mysql/mysql_oldboy.errpid-file = /var/run/mysqld/mysqld.pid

You can see that the original file has been overwritten.

[email protected]:/var/log/mysql# lserror.log[email protected]:/var/log/mysql# cat error.log 2018-06-26T23:03:23.234767Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).2018-06-26T23:03:23.525795Z 0 [Warning] InnoDB: New log files created, LSN=45790

The log has also been generated based on the configuration file.
The test was successful.

4. Summary of Ideas

In fact, it is not difficult, mainly to find the configuration file directory, and then use Volume mount the configured Configmap file to the directory of the configuration file, instead of the default configuration file.
It should be noted that the file name read by Configmap is the same as the default profile name.

Kubernetes management of MySQL application configuration file in container with Configmap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.