概述
主要用于向Pod注入非敏感数据,使用时,用户将数据直接存储在ConfigMap对象当中,然后Pod通过使用ConfigMap卷进行引用
ConfigMap 在容器中使用的场景通常有 3 种,分别如下所示:
- 生成为容器中的环境变量
- 设置容器启动命令的启动参数(需设置为环境变量)
-
限制条件
ConfigMap 必须在 Pod 之前创建
- ConfigMap 受 Namespace 限制,只有处于相同 Namespace 中的 Pod 才可以引用它
- kubelet 只支持可以被 API Server 管理的 Pod 使用 ConfigMap,kubelet 在本 Node 上通过 —manifest-url 或 —config 自动创建的静态 Pod 无法引用 ConfigMap
- 在 Pod 对 ConfigMap 进行挂载(volume)操作时,在容器内部只能挂载为“目录”,不能挂载为“文件”。在挂载到容器内部后,在目录下将包含 ConfigMap 定义的每个 item,如果在该目录下原来还有其它文件,则容器内的该目录将被挂载的 ConfigMap 覆盖。如果想要保存原来的其它文件,可以将 ConfigMap 挂载到容器内部的临时目录,再通过启动脚本将配置文件复制(cp)或链接(link)到应用所在的实际目录
创建
执行 kubectl explain cm
| 参数 | 类型 | 说明 |
|---|---|---|
| apiVersion | string | |
| binaryData | map[string]string | 二进制数据 |
| data | map[string]string | |
| immutable | boolean | 是否可以修改cm |
| kind | string | |
| metadata | Object |
YAML 配置文件
apiVersion: v1kind: ConfigMapmetadata:name: cm-appvars # ConfigMap 的名称data: # 配置信息apploglevel: infoappdatadir: /var/data
执行 配置文件
$ kubectl create -f cm-appvars.yamlconfigmap/cm-appvars created
查看配置信息
$ kubectl get configmapNAME DATA AGEcm-appvars 2 37s
查看配置详细
$ kubectl describe configmap cm-appvarsName: cm-appvarsNamespace: defaultLabels: <none>Annotations: <none>Data====appdatadir:----/var/dataapploglevel:----infoEvents: <none>
kubectl 命令行
kubectl create configmap <map-name> <data-source>
—from-literal
eg:
✗ kubectl create cm myconfig --from-literal=k=v -n testconfigmap/myconfig created
命令解释:
- cm: ConfigMap 的简写
- myconfig: 要创建的 ConfigMap 的名称,可以自己定义
- —from-literal=k=v:表示直接从命令行指定 key-value 值,k=v 的格式是
<key>=<value>,这个参数也可以指定多次 - -n test : 在 test 这个 namespace 下面创建 ConfigMap
查看myconfig 信息
kubectl get cm myconfig -n test -o yamlapiVersion: v1data:k: vkind: ConfigMapmetadata:creationTimestamp: "2020-07-12T16:08:14Z"managedFields:- apiVersion: v1fieldsType: FieldsV1fieldsV1:f:data:.: {}f:k: {}manager: kubectloperation: Updatetime: "2020-07-12T16:08:14Z"name: myconfignamespace: testresourceVersion: "217482"selfLink: /api/v1/namespaces/test/configmaps/myconfiguid: 8a8169bd-e605-4f3b-8b3f-c9148abf54c6
—from-file
[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
从文件创建名为 mysql-config 的 ConfigMap:
$ kubectl create configmap mysql-config --from-file=mysqld.cnfconfigmap/mysql-config created
使用 key-value 键值对创建名为 myconfig 的 ConfigMap:
$ kubectl create configmap myconfig --from-literal=root_passwd=123456configmap/myconfig created
创建mysql
apiVersion: apps/v1kind: Deploymentmetadata:name: mysqlspec:selector:matchLabels:app: mysqltemplate:metadata:labels:app: mysqlspec:containers:- image: mysql:5.7name: mysqlports:- containerPort: 3306env:- name: MYSQL_ROOT_PASSWORDvalueFrom:configMapKeyRef:name: myconfigkey: root_passwdvolumeMounts:- name: mysqlmountPath: /etc/mysql/mysql.conf.dvolumes:- name: mysqlconfigMap:name: mysql-config
执行
$ kubectl create -f mysql-config.yaml
查看
$ kubectl get podsNAME READY STATUS RESTARTS AGEmysql-598f68bc4f-hkz62 0/1 ContainerCreating 0 90s
进入容器
$ kubectl exec -it mysql-598f68bc4f-hkz62 /bin/bash
执行mysql客户端
# mysql -uroot -p123456mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.30 MySQL Community Server (GPL)Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
