ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
ConfigMap可以采用下面几种方式创建。 <br/> **1. 使用目录创建** ```shell # ls docs/user-guide/configmap/kubectl/ game.properties ui.properties # cat docs/user-guide/configmap/kubectl/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 # cat docs/user-guide/configmap/kubectl/ui.properties color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice # kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl configmap/game-config created # kubectl get configmap NAME DATA AGE game-config 2 82s # kubectl get configmap game-config -o go-template='{{.data}}' map[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 ``` `--from-file`:指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对,键的名字就是文件名,值就是文件的内容。 <br/> **2. 使用文件创建** 只要指定为一个文件就可以从单个文件中创建 ConfigMap。 ```shell # kubectl create configmap game-config-2 --from-file=game.properties --from-file=ui.properties configmap/game-config-2 created # kubectl get configmap NAME DATA AGE game-config-2 2 22s # kubectl get configmaps game-config-2 -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: "2022-02-21T11:31:55Z" managedFields: - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:data: .: {} f:game.properties: {} f:ui.properties: {} manager: kubectl operation: Update time: "2022-02-21T11:31:55Z" name: game-config-2 namespace: default resourceVersion: "123146" selfLink: /api/v1/namespaces/default/configmaps/game-config-2 uid: d311a5f5-1b16-44ac-a863-240297b630ad ``` `--from-file`:这个参数可以使用多次,你可以使用两次分別指定上个实例中的那两个配置文 件,效果就跟指定整个目录是一样的。 <br/> **3. 使用字面值创建** 使用文字值创建,利用`--from-literal`参数传递配置信息,该参数可以使用多次。 ```shell # 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: "2022-02-21T11:37:43Z" managedFields: - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:data: .: {} f:special.how: {} f:special.type: {} manager: kubectl operation: Update time: "2022-02-21T11:37:43Z" name: special-config namespace: default resourceVersion: "123966" selfLink: /api/v1/namespaces/default/configmaps/special-config uid: e4ae0c2f-6dc8-4f26-a58c-eb36ec0cea49 ``` **** 参考文档:https://feisky.xyz/kubernetes-handbook/concepts/configmap.html