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:       <none>
Annotations:  <none>

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讀取的文件名需和默認的配置文件名相同。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章