ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
命令形式如下: ~~~bash kubectl create configmap <map-name> <data-source> ~~~ 其中,`<map-name>`是设置的 configmap 的名字,`<data-source>`可以是目录、文件、或是 key-value 键值对。 `<data-source>`中的可选参数为:`--from-file`/`--from-env-file`(从目录或文件创建) 和`--from-literal`(直接指定键值对创建)。`configmap`也可以缩写为`cm`。 可以在命令行中执行`kubectl create configmap -h`查看具体参数和使用方法。 #### 从目录中创建 ConfigMap 这种方式适用于在一个目录下有多个配置文件。 在`/home/shiyanlou`目录下新建`configmap`文件夹,并在`configmap`文件夹下新建两个文件,分别名为:`game.properties`和`ui.properties`。 `game.properties`中的文件内容为: ~~~text 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`中的文件内容为: ~~~text color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice ~~~ 执行创建: ~~~bash # 这里设置 configmap 的名称为 game-config $ kubectl create configmap game-config --from-file=/home/shiyanlou/configmap/ configmap/game-config created $ kubectl get configmap game-config -o yaml 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: "2019-09-23T08:37:09Z" name: game-config namespace: default resourceVersion: "11509" selfLink: /api/v1/namespaces/default/configmaps/game-config uid: 212cd3eb-fd6d-43a4-8aed-d33968da8e1c $ kubectl describe configmap game-config Name: game-config Namespace: default Labels: <none> Annotations: <none> 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 Events: <none> ~~~ #### 从文件中创建 ConfigMap 可以从单独的文件或是多个文件中进行创建。 比如: ~~~bash # 使用单个文件创建 kubectl create configmap game-config-2 --from-file=/home/shiyanlou/configmap/game.properties # 使用多个文件创建 kubectl create configmap game-config-3 --from-file=/home/shiyanlou/configmap/game.properties --from-file=/home/shiyanlou/configmap/ui.properties ~~~ 使用参数`--from-env-file`从 env-file 中创建 ConfigMap。env-file 是包含了环境变量的列表。 在`/home/shiyanlou/configmap`目录下新建`game-env-file.properties`文件,并写入如下内容: ~~~text enemies=aliens lives=3 allowed="true" ~~~ 执行创建: ~~~bash $ kubectl create configmap game-config-env-file --from-env-file=/home/shiyanlou/configmap/game-env-file.properties configmap/game-config-env-file created $ kubectl get configmap game-config-env-file -o yaml apiVersion: v1 data: allowed: '"true"' enemies: aliens lives: "3" kind: ConfigMap metadata: creationTimestamp: "2019-09-23T09:00:46Z" name: game-config-env-file namespace: default resourceVersion: "13919" selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file uid: 790cbece-1144-4ebe-9ea1-1e151f210ef7 ~~~ 另外使用文件创建的话,可以使用自定义的名称替代默认使用的文件名,比如: ~~~bash # 使用 game.properties 文件创建,但是将其重命名为 game-special-key $ kubectl create configmap game-config-4 --from-file=game-special-key=/home/shiyanlou/configmap/game.properties configmap/game-config-4 created # 可以看到这里显示的是 game-special-key,而不再是之前的 game.properties $ kubectl describe configmap game-config-4 Name: game-config-4 Namespace: default Labels: <none> Annotations: <none> Data ==== game-special-key: ---- enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=noGoodRotten secret.code.passphrase=UUDDLRLRBABAS secret.code.allowed=true secret.code.lives=30 Events: <none> ~~~ #### 使用 key-value 键值对创建 ConfigMap 使用参数`--from-literal`直接在命令中设置 key-value 键值对。 比如: ~~~bash # 传递多组键值对 $ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm configmap/special-config created $ kubectl get configmaps special-config -o yaml apiVersion: v1 data: special.how: very special.type: charm kind: ConfigMap metadata: creationTimestamp: "2019-09-23T09:16:09Z" name: special-config namespace: default resourceVersion: "15490" selfLink: /api/v1/namespaces/default/configmaps/special-config uid: 231c59c3-dc4d-4650-a04f-af51debc94fa ~~~