# kubectl create configmap 命令详解

根据配置文件、目录或指定的 literal-value 创建 configmap。

configmap 用来保存一个或多个 key/value 信息。

当基于配置文件创建 configmap 时,key 将默认为文件的基础名称,value 默认为文件文本内容。如果基本名称的 key 无效,则可以指定另一个 key。

当基于目录创建 configmap 时,key 还是文件的基础名称,目录中每个配置文件名都被设置为 key,文件内容设置为 value。

# 语法

$ configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run]

# 示例

根据文件创建一个名为 my-config 的 configmap

kubectl create configmap my-config --from-file=path/to/bar

使用指定的 keys 创建一个名为 my-config 的 configmap

kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt

使用 key1 = config1 和 key2 = config2 创建一个名为 my-config 的 configmap

kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

从文件中的 key = value 对创建一个名为 my-config 的 configmap

kubectl create configmap my-config --from-file=path/to/bar

从 env 文件创建一个名为 my-config 的 configmap

kubectl create configmap my-config --from-env-file=path/to/bar.env

# Flags

Name Shorthand Default Usage
allow-missing-template-keys true If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
dry-run false If true, only print the object that would be sent, without sending it.
from-env-file Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).
from-file [] Key file can be specified using its file path, in which case file basename will be used as configmap key, or optionally with a key and file path, in which case the given key will be used. Specifying a directory will iterate each named file in the directory whose basename is a valid configmap key.
from-literal [] Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)
generator configmap/v1 The name of the API generator to use.
no-headers false When using the default or custom-column output format, don't print headers (default print headers).
output o Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
output-version DEPRECATED: To use a specific API version, fully-qualify the resource, version, and group (for example: 'jobs.v1.batch/myjob').
save-config false If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
schema-cache-dir ~/.kube/schema If non-empty, load/store cached API schemas in this directory, default is '$HOME/.kube/schema'
show-all a false When printing, show all resources (default hide terminated pods.)
show-labels false When printing, show all labels as the last column (default hide labels column)
sort-by If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. '{.metadata.name}'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
template Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
validate true If true, use a schema to validate the input before sending it
Last Updated: 6/17/2023, 6:57:19 PM