1. 从文件创建
1.1 --from-file
从文件创建,且挂载后文件名和源文件一致。
- 语法
| kubectl create configmap Configmap_Name --from-file=File_Name -n Namespace_Name |
说明:
这种方法创建的configmap,默认以File_Name作为key。
而configmap挂载到容器中之后将以这个key作为容器中文件的名字。
因此创建configmap的源文件必须与容器内要挂载的文件名一致。
如果要修改key名,看1.2节
- 示例
需求:从.env 文件创建一个名为 web-env的congfigmap,之后需要挂载到pod中/etc/.env
| IOT_WEB_API_HOST=10.252.xxx.xxx |
| IOT_WEB_API_PORT=30103 |
| IOT_WEB_PROJECT_NAME=xxxxxxx |
| kubectl create configmap web-env --from-file=.env -n xinfa |
- 查看
说明: configmap中 data下 .env为key。之后文件内容为该key的值
| # kubectl edit -n xinfa configmaps web-env |
| apiVersion: v1 |
| data: |
| .env: | |
| IOT_WEB_API_HOST=10.252.xxx.xxx |
| IOT_WEB_API_PORT=30103 |
| IOT_WEB_PROJECT_NAME=xxxxxxx |
| kind: ConfigMap |
| metadata: |
| creationTimestamp: "2019-11-29T05:33:36Z" |
| name: web-env |
| namespace: xinfa |
| resourceVersion: "9432225" |
| selfLink: /api/v1/namespaces/xinfa/configmaps/web-env |
| uid: ca830a23-1269-11ea-82c2-005056a7d888 |
1.2 --from-literal
从文件创建,挂载后的文件名可自定义(和源文件不同)
- 语法
| kubectl create configmap Configmap_Name --from-file=KEY_Name=File_Name --from-file=KEY_Nname=File_Name -n Namespace_Name |
- 示例
需求:从env 文件创建一个名为 web-env的congfigmap,之后需要挂载到pod中/etc/.env
| IOT_WEB_API_HOST=10.252.xxx.xxx |
| IOT_WEB_API_PORT=30103 |
| IOT_WEB_PROJECT_NAME=xxxxxxx |
| [root@DoM01 yml]# kubectl create configmap web-env --from-file=.env=env |
| configmap/config-env created |
| # kubectl edit configmaps web-env |
可见configmap内容如下:
| |
| |
| |
| |
| apiVersion: v1 |
| data: |
| .env: | |
| IOT_WEB_API_HOST=10.252.xxx.xxx |
| IOT_WEB_API_PORT=30103 |
| IOT_WEB_PROJECT_NAME=xxxxxxx |
| kind: ConfigMap |
| metadata: |
| creationTimestamp: "2021-02-03T09:47:36Z" |
| name: web-env |
| namespace: default |
| resourceVersion: "15083053" |
| selfLink: /api/v1/namespaces/default/configmaps/web-env |
| uid: 45cde575-d55b-4242-8b6d-ef0303196c2b |
上文可见,key变成了.env 而并不是1.1中使用原文件名作为key了。
2. 从yaml文件创建
文件叫什么名字无所谓,就叫configmap.yml吧,内容如下:
| apiVersion: v1 |
| kind: ConfigMap |
| metadata: |
| name: mysqlcnf |
| namespace: mysql-test |
| data: |
| my.cnf: | |
| # 这里是mysql的配置文件 |
| # 虽然主公这个懒家伙什么也没有留下,但是不影响mysql启动 |
| !includedir /etc/mysql/conf.d/ |
| !includedir /etc/mysql/mysql.conf.d/ |
| |
| kubectl create -f configmap.yml |
3. configMap使用
3.1 key 是目标文件名
- 使用方法
key名是挂载到容器中的文件名,如果不是使用3.2中的方法。
| spec: |
| containers: |
| - name: xinfa01-web |
| image: 10.252.xxx.xxx/xinfa/xxxx |
| imagePullPolicy: IfNotPresent |
| env: |
| - name: TZ |
| value: "Asia/Shanghai" |
| ports: |
| - containerPort: 3080 |
| protocol: TCP |
| name: http |
| volumeMounts: |
| - mountPath: /build |
| name: web-server |
| |
| - mountPath: /etc/.env |
| name: web-env |
| subPath: .env |
| volumes: |
| - name: web-server |
| persistentVolumeClaim: |
| claimName: pv-xf01-web |
| |
| - name: web-env |
| configMap: |
| name: web-env |
| imagePullSecrets: |
| - name: my-harbor |
- 完整的示例
| --- |
| apiVersion: extensions/v1beta1 |
| kind: Deployment |
| metadata: |
| name: mysql |
| namespace: mysql-test |
| spec: |
| replicas: 1 |
| template: |
| metadata: |
| labels: |
| app: mysql |
| spec: |
| containers: |
| - name: mysql |
| image: harbocto.xxx.com.cn/public/mysql:5.7 |
| imagePullPolicy: IfNotPresent |
| env: |
| - name: MYSQL_ROOT_PASSWORD |
| value: yunweizhidao |
| - name: MYSQL_REPLICATION_USER |
| value: "liuwei" |
| - name: MYSQLREPLICAITONPASSWO |
| value: "1qaz@WSX" |
| ports: |
| - containerPort: 3306 |
| volumeMounts: |
| - name: mysql-data |
| mountPath: /var/lib/mysql |
| - mountPath: /etc/mysql/my.cnf |
| name: mysqlcnf |
| subPath: my.cnf |
| volumes: |
| - name: mysql-data |
| persistentVolumeClaim: |
| claimName: mysql |
| - name: mysqlcnf |
| configMap: |
| name: mysqlcnf |
| |
| --- |
| apiVersion: v1 |
| kind: ConfigMap |
| metadata: |
| name: mysqlcnf |
| namespace: mysql-test |
| data: |
| my.cnf: | |
| # 这里是mysql的配置文件 |
| # 虽然鸿渐这个懒家伙什么也没有留下,但是不影响mysql启动 |
| !includedir /etc/mysql/conf.d/ |
| !includedir /etc/mysql/mysql.conf.d/ |
| --- |
| apiVersion: v1 |
| kind: Service |
| metadata: |
| name: mysql |
| namespace: mysql-test |
| spec: |
| type: NodePort |
| ports: |
| - port: 3306 |
| targetPort: 3306 |
| nodePort: 30200 |
| selector: |
| app: mysql |
| selector: |
| app: mysql |
| |
| --- |
| kind: PersistentVolumeClaim |
| apiVersion: v1 |
| metadata: |
| name: mysql |
| namespace: mysql-test |
| spec: |
| accessModes: |
| - ReadWriteOnce |
| resources: |
| requests: |
| storage: 5Gi |
| |
3.2 key名不是目标文件名
需求:将名为mysqlcnf 的configmap中,key为mycnf的内容,挂载到容器中为/etc/mysql/my.cnf
| ports: |
| - containerPort: 3306 |
| volumeMounts: |
| - name: mysql-data |
| mountPath: /var/lib/mysql |
| - name: mysqlcnf |
| mountPath: /etc/mysql/my.cnf |
| subPath: my.cnf |
| volumes: |
| - name: mysql-data |
| persistentVolumeClaim: |
| claimName: mysql |
| - name: mysqlcnf |
| configMap: |
| name: mysqlcnf |
| |
| items: |
| - key: mycnf |
| path: my.cnf |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?