多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
ConfigMap 用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件。 ConfigMap 跟 secret 很类似,但它可以更方便地处理不包含敏感信息的字符串。 # ConfigMap帮助 kubectl的帮助信息很友好,这里通过`kubectl create configmap --help`查看一些基本的用法 ``` $ kubectl create configmap --help Aliases: configmap, cm Examples: # Create a new configmap named my-config based on folder bar kubectl create configmap my-config --from-file=path/to/bar # Create a new configmap named my-config with specified keys instead of file basenames on disk kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt # Create a new configmap named my-config with key1=config1 and key2=config2 kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2 # Create a new configmap named my-config from the key=value pairs in the file kubectl create configmap my-config --from-file=path/to/bar # Create a new configmap named my-config from an env file kubectl create configmap my-config --from-env-file=path/to/bar.env Options: --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. --append-hash=false: Append a hash of the configmap to its name. --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. -o, --output='': Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file. --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. --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 Usage: kubectl create configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run] [options] Use "kubectl options" for a list of global command-line options (applies to all commands). ``` 上面有很多例子,基础的就不说了,来看下最常用的。 # ConfigMap使用 简单创建一个configmap试一下 ``` apiVersion: v1 kind: ConfigMap metadata: name: test-config namespace: default data: user: carey type: debug ``` ``` $ kubectl apply -f configmap.yaml $ kubectl get configmaps test-config -o yaml apiVersion: v1 data: type: debug user: carey kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"type":"debug","user":"carey"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test-config","namespace":"default"}} creationTimestamp: "2019-05-20T03:33:00Z" name: test-config namespace: default resourceVersion: "3864510" selfLink: /api/v1/namespaces/default/configmaps/test-config uid: f786e6f2-7aaf-11e9-b2cf-005056b07581 ``` 可以看到成功创建了一个configmap ## 用作环境变量 ``` apiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: test-container image: busybox command: ["/bin/sh", "-c", "env"] env: - name: USERNAME_KEY valueFrom: configMapKeyRef: name: test-config key: user - name: TYPE_KEY valueFrom: configMapKeyRef: name: test-config key: type envFrom: - configMapRef: name: test-config restartPolicy: Never ``` 可以再容器的日志或者env中发现如下信息 ``` TYPE_KEY=debug type=debug user=carey USERNAME_KEY=carey ``` ## 使用 subpath 将 ConfigMap 作为单独的文件挂载到目录 ``` apiVersion: v1 kind: ConfigMap metadata: name: test-config namespace: default data: my.conf: | enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=noGoodRotten secret.code.passphrase=UUDDLRLRBABAS secret.code.allowed=true secret.code.lives=30 ``` ``` apiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: test-container image: busybox command: ["sleep", "300000"] volumeMounts: - name: config-volume mountPath: /etc/config.cnf subPath: my.conf volumes: - name: config-volume configMap: name: test-config items: - key: my.conf path: my.conf restartPolicy: Never ``` 参考文档:[ConfigMap]([https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/))