1.configmap的作用理解
configMap起什么作用的呢?
举个例子,启用一个mysql容器。一般来说,mysql容器重要的有两部分,一部分为存储数据,一部分为配置文件my.cnf。
前面有测试过,存储数据可以用pv pvc实现和容器的分离解耦。
配置文件也能够实现和容器的分离解耦,也就是说mysql容器能够直接读取并使用预先配置好的配置文件(而不是使用容器中默认自带的配置文件),非常方便。这就是configMap的功能。
kubernetes使用configMap来实现对容器中应用的配置文件管理。
2.创建configMap
创建ConfigMap的方式有两种,一种是通过yaml文件来创建,另一种是通过kubectl直接在命令行下创建。
随便找一个可用的配置好的my.cnf文件
现在测试,尽量简单。
我的见下:
[[email protected] 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
[[email protected] wp]#
注意:
这个名字为什么是mysqld.cnf,是因为容器里读取的配置文件名字就是这个,最少修改的原则,直接取代覆盖,还用原名字。
用这个文件创建configmap
[[email protected] wp]# kubectl create configmap mysql-config --from-file=mysqld.cnf
configmap "mysql-config" created
[[email protected] wp]# kubectl describe configmap mysql-config
Name:? ? ? ?? mysql-config
Namespace:? ? default
Labels:? ? ?? <none>
Annotations:? <none>
Data
====
mysqld.cnf:
......
3.以volume形式挂载进mysql容器,并读取这个文件启动
挂载在哪里呢?
肯定是挂载到默认的存放配置文件处,并取代它。
mysql容器默认读取的配置文件路径见下:
[email protected]:/etc/mysql# pwd
/etc/mysql
[email protected]:/etc/mysql# ls
conf.d? ? ? ? my.cnf? ? ? ? my.cnf.fallback? mysql.cnf? mysql.conf.d
[email protected]:/etc/mysql#
my.cnf的内容见下:
[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/
分别看下这两个目录的内容
[email protected]:/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
[email protected]:/etc/mysql/mysql.conf.d#
可以看到最重要的配置文件就是这个,configmap挂载到这里,用我们预设的配置文件取代这里的mysqld.cnf
这里的路径是:
[email protected]:/etc/mysql/mysql.conf.d# pwd
/etc/mysql/mysql.conf.d
[email protected]:/etc/mysql/mysql.conf.d# ls
mysqld.cnf
以下是service和pod的配置文件:
[[email protected] 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
[[email protected] wp]# kubectl apply -f mysql-svc2.yml
service "mysql2" created
deployment.apps "mysql-t1" created
起来了
[[email protected] 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
[[email protected] wp]#
进入容器检查是不是读取到了我们的配置文件
[[email protected] ~]# 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
[email protected]:/etc/mysql/mysql.conf.d# ls
mysqld.cnf
[email protected]:/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
可以看到已经覆盖原文件。
[email protected]:/var/log/mysql# ls
error.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
日志也已经根据配置文件生成。
测试成功。
4.思路总结
其实并不难,主要是要找配置文件的存放目录,然后用volume挂载配置好的configmap文件到配置文件的目录,取代默认的配置文件即可。
需要注意的是configmap读取的文件名需和默认的配置文件名相同。
原文地址:http://blog.51cto.com/goome/2155871