|NO.Z.00170|——————————|CloudNative|——|KuberNetes&配置管理.V01|——|configmap.v01|从目录创建configmap|
一、ConfigMap概述
### --- ConfigMap概述
~~~ # 一般用ConfigMap去管理一些配置文件、或者一些大量的环境变量信息。
~~~ ConfigMap将配置和Pod分开,有一个nginx,nginx.conf -> configmap,nginx
~~~ 更易于配置文件的更改和管理。
~~~ # Secret:
~~~ Secret更倾向于存储和共享敏感、加密的配置信息。
### --- ConfigMap中文地址:
~~~ https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-pod-configmap/

二、从目录创建configmap
### --- 从目录创建configmap
~~~ # 创建configmap文件夹
~~~ 可以用来kubectl create configmap从同一目录中的多个文件创建ConfigMap。
~~~ 当您基于目录创建ConfigMap时,kubectl会识别基本名是目录中有效密钥的文件,
~~~ 并将每个文件打包到新的ConfigMap中。除常规文件外,
~~~ 所有目录条目都将被忽略(例如,子目录,符号链接,设备,管道等)
[root@k8s-master01 ~]# mkdir -p configure-pod-container/configmap/
### --- 下载模板文件
[root@k8s-master01 ~]# wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
[root@k8s-master01 ~]# wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties
[root@k8s-master01 ~]# cd configure-pod-container/configmap/
game.properties ui.properties
### --- 这种是kv的形式
[root@k8s-master01 configmap]# cat game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
[root@k8s-master01 configmap]# cat ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
三、从一个目录创建多个文件的configmap
### --- 从一个目录创建多个文件的configmap
[root@k8s-master01 configmap]# kubectl create configmap game-config --from-file=configure-pod-container/configmap/
configmap/game-config created
### --- 在这种情况下,以上命令将每个文件打包,
~~~ game.properties并将目录ui.properties中的每个文件打包configure-pod-container/configmap/到game-config ConfigMap中。
~~~ 您可以使用以下命令显示ConfigMap的详细信息:
~~~ 注:目录中的game.properties和ui.properties
~~~ 文件在ConfigMapconfigure-pod-container/configmap/的data部分中表示。
[root@k8s-master01 configmap]# kubectl describe configmaps game-config
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.properties: // 将这两个文件内容读取到configmap中
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties: // 将这两个文件内容读取到configmap中
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
四、查看创建的configmap的yaml配置文件
### --- 查看创建的configmap的yaml配置文件
[root@k8s-master01 configmap]# kubectl get configmap game-config -oyaml
apiVersion: v1
data:
game.properties: |-
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: "2021-04-23T15:34:24Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {}
f:game.properties: {}
f:ui.properties: {}
manager: kubectl-create
operation: Update
time: "2021-04-23T15:34:24Z"
name: game-config
namespace: default
resourceVersion: "597716"
uid: c4e4a7b3-a717-4b8a-98e1-b415e0fd533c
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了