標籤:tar apply redist inno defaults please general local 功能
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 = 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[[email protected] wp]#
注意:
這個名字為什麼是mysqld.cnf,是因為容器裡讀取的設定檔名字就是這個,最少修改的原則,直接取代覆蓋,還用原名字。
用這個檔案建立configmap
[[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.以volume形式掛載進mysql容器,並讀取這個檔案啟動
掛載在哪裡呢?
肯定是掛載到預設的存放設定檔處,並取代它。
mysql容器預設讀取的設定檔路徑見下:
[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#
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.pidsocket = /var/run/mysqld/mysqld.sockdatadir = /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 riskssymbolic-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# lsmysqld.cnf
以下是service和pod的設定檔:
[[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
起來了
[[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 kubernetes3httpd-749bf8c6f4-xvrn4 1/1 Running 1 5d 10.244.2.253 kubernetes3mysql-7db74785b4-2mk4r 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 Running 1 5d 10.244.1.27 kubernetes2nginx-deployment-6b5c99b6fd-zr2p7 1/1 Running 1 5d 10.244.2.254 kubernetes3node-exporter-4gbh9 1/1 Running 25 41d 192.168.211.152 kubernetes3node-exporter-8h9vp 1/1 Running 26 41d 192.168.211.151 kubernetes2wordpress-pod-7dd7659959-hc7mr 1/1 Running 5 8d 10.244.2.2 kubernetes3[[email protected] wp]#
進入容器檢查是不是讀取到了我們的設定檔
[[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
可以看到已經覆蓋原檔案。
[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
日誌也已經根據設定檔產生。
測試成功。
4.思路總結
其實並不難,主要是要找設定檔的存放目錄,然後用volume掛載配置好的configmap檔案到設定檔的目錄,取代預設的設定檔即可。
需要注意的是configmap讀取的檔案名稱需和預設的設定檔名相同。
kubernetes用configmap實現容器中mysql應用設定檔的管理