pod中mysql配置文件修改_kubernetes用configmap实现容器中mysql应用配置文件的管理

1.configmap的作用理解

configMap起什么作用的呢?

举个例子,启用一个mysql容器。一般来说,mysql容器重要的有两部分,一部分为存储数据,一部分为配置文件my.cnf。

前面有测试过,存储数据可以用pv pvc实现和容器的分离解耦。

配置文件也能够实现和容器的分离解耦,也就是说mysql容器能够直接读取并使用预先配置好的配置文件(而不是使用容器中默认自带的配置文件),非常方便。这就是configMap的功能。

kubernetes使用configMap来实现对容器中应用的配置文件管理。

2.创建configMap

创建ConfigMap的方式有两种,一种是通过yaml文件来创建,另一种是通过kubectl直接在命令行下创建。

随便找一个可用的配置好的my.cnf文件

现在测试,尽量简单。

我的见下:

[root@kubernetes1 wp]# cat mysqld.cnf

[client]

port = 3306

socket = /var/run/mysqld/mysqld.sock

[mysql]

no-auto-rehash

[mysqld]

user = mysql

port = 3306

socket = /var/run/mysqld/mysqld.sock

datadir = /var/lib/mysql

[mysqld_safe]

log-error= /var/log/mysql/mysql_oldboy.err

pid-file = /var/run/mysqld/mysqld.pid

[root@kubernetes1 wp]#

注意:

这个名字为什么是mysqld.cnf,是因为容器里读取的配置文件名字就是这个,最少修改的原则,直接取代覆盖,还用原名字。

用这个文件创建configmap

[root@kubernetes1 wp]# kubectl create configmap mysql-config --from-file=mysqld.cnf

configmap "mysql-config" created

[root@kubernetes1 wp]# kubectl describe configmap mysql-config

Name:         mysql-config

Namespace:    default

Labels:      

Annotations: 

Data

====

mysqld.cnf:

......

3.以volume形式挂载进mysql容器,并读取这个文件启动

挂载在哪里呢?

肯定是挂载到默认的存放配置文件处,并取代它。

mysql容器默认读取的配置文件路径见下:

root@mysql-5bbbf49b4f-wjw47:/etc/mysql# pwd

/etc/mysql

root@mysql-5bbbf49b4f-wjw47:/etc/mysql# ls

conf.d        my.cnf        my.cnf.fallback  mysql.cnf  mysql.conf.d

root@mysql-5bbbf49b4f-wjw47:/etc/mysql#

my.cnf的内容见下:

root@mysql-7db74785b4-2mk4r:/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/

分别看下这两个目录的内容

root@mysql-7db74785b4-2mk4r:/etc/mysql/mysql.conf.d# cat mysqld.cnf

# Copyright (c) 2014, 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

#

# 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.pid

socket = /var/run/mysqld/mysqld.sock

datadir = /var/lib/mysql

#log-error = /var/log/mysql/error.log

# By default we only accept connections from localhost

#bind-address = 127.0.0.1

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

root@mysql-7db74785b4-2mk4r:/etc/mysql/mysql.conf.d#

可以看到最重要的配置文件就是这个,configmap挂载到这里,用我们预设的配置文件取代这里的mysqld.cnf

这里的路径是:

root@mysql-7db74785b4-2mk4r:/etc/mysql/mysql.conf.d# pwd

/etc/mysql/mysql.conf.d

root@mysql-7db74785b4-2mk4r:/etc/mysql/mysql.conf.d# ls

mysqld.cnf

以下是service和pod的配置文件:

[root@kubernetes1 wp]# cat mysql-svc2.yml

apiVersion: v1

kind: Service

metadata:

name: mysql2

spec:

ports:

- port: 3306

selector:

app: mysql1

---

apiVersion: apps/v1beta1

kind: Deployment

metadata:

name: mysql-t1

spec:

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

[root@kubernetes1 wp]# kubectl apply -f mysql-svc2.yml

service "mysql2" created

deployment.apps "mysql-t1" created

起来了

[root@kubernetes1 wp]# kubectl get pod -owide

NAME READY STATUS RESTARTS AGE IP NODE

httpd-749bf8c6f4-bfjfw 1/1 Running 1 5d 10.244.2.4 kubernetes3

httpd-749bf8c6f4-ghpzl 1/1 Running 1 5d 10.244.2.5 kubernetes3

httpd-749bf8c6f4-xvrn4 1/1 Running 1 5d 10.244.2.253 kubernetes3

mysql-7db74785b4-2mk4r 1/1 Running 0 1h 10.244.1.29 kubernetes2

mysql-t1-6fbf57db97-z5rtl 1/1 Running 0 13s 10.244.1.36 kubernetes2

nginx-deployment-6b5c99b6fd-pscr6 1/1 Running 1 5d 10.244.1.27 kubernetes2

nginx-deployment-6b5c99b6fd-zr2p7 1/1 Running 1 5d 10.244.2.254 kubernetes3

node-exporter-4gbh9 1/1 Running 25 41d 192.168.211.152 kubernetes3

node-exporter-8h9vp 1/1 Running 26 41d 192.168.211.151 kubernetes2

wordpress-pod-7dd7659959-hc7mr 1/1 Running 5 8d 10.244.2.2 kubernetes3

[root@kubernetes1 wp]#

进入容器检查是不是读取到了我们的配置文件

[root@kubernetes2 ~]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

c78f97476162 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

root@mysql-t1-6fbf57db97-z5rtl:/etc/mysql/mysql.conf.d# ls

mysqld.cnf

root@mysql-t1-6fbf57db97-z5rtl:/etc/mysql/mysql.conf.d# cat mysqld.cnf

[client]

port = 3306

socket = /var/run/mysqld/mysqld.sock

[mysql]

no-auto-rehash

[mysqld]

user = mysql

port = 3306

socket = /var/run/mysqld/mysqld.sock

datadir = /var/lib/mysql

[mysqld_safe]

log-error= /var/log/mysql/mysql_oldboy.err

pid-file = /var/run/mysqld/mysqld.pid

可以看到已经覆盖原文件。

root@mysql-t1-6fbf57db97-z5rtl:/var/log/mysql# ls

error.log

root@mysql-t1-6fbf57db97-z5rtl:/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

日志也已经根据配置文件生成。

测试成功。

4.思路总结

其实并不难,主要是要找配置文件的存放目录,然后用volume挂载配置好的configmap文件到配置文件的目录,取代默认的配置文件即可。需要注意的是configmap读取的文件名需和默认的配置文件名相同。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值