2017-11-08 16:21:18 +08:00
|
|
|
|
## ConfigMap
|
2017-05-20 21:03:25 +08:00
|
|
|
|
|
|
|
|
|
其实ConfigMap功能在Kubernetes1.2版本的时候就有了,许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息。这些配置信息需要与docker image解耦,你总不能每修改一个配置就重做一个image吧?ConfigMap API给我们提供了向容器中注入配置信息的机制,ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制大对象。
|
|
|
|
|
|
|
|
|
|
## ConfigMap概览
|
|
|
|
|
|
|
|
|
|
**ConfigMap API**资源用来保存**key-value pair**配置数据,这个数据可以在**pods**里使用,或者被用来为像**controller**一样的系统组件存储配置数据。虽然ConfigMap跟[Secrets](https://kubernetes.io/docs/user-guide/secrets/)类似,但是ConfigMap更方便的处理不含敏感信息的字符串。 注意:ConfigMaps不是属性配置文件的替代品。ConfigMaps只是作为多个properties文件的引用。你可以把它理解为Linux系统中的`/etc`目录,专门用来存储配置文件的目录。下面举个例子,使用ConfigMap配置来创建Kuberntes Volumes,ConfigMap中的每个data项都会成为一个新文件。
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
kind: ConfigMap
|
|
|
|
|
apiVersion: v1
|
|
|
|
|
metadata:
|
|
|
|
|
creationTimestamp: 2016-02-18T19:14:38Z
|
|
|
|
|
name: example-config
|
|
|
|
|
namespace: default
|
|
|
|
|
data:
|
|
|
|
|
example.property.1: hello
|
|
|
|
|
example.property.2: world
|
|
|
|
|
example.property.file: |-
|
|
|
|
|
property.1=value-1
|
|
|
|
|
property.2=value-2
|
|
|
|
|
property.3=value-3
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`data`一栏包括了配置数据,ConfigMap可以被用来保存单个属性,也可以用来保存一个配置文件。 配置数据可以通过很多种方式在Pods里被使用。ConfigMaps可以被用来:
|
|
|
|
|
|
|
|
|
|
1. 设置环境变量的值
|
|
|
|
|
2. 在容器里设置命令行参数
|
|
|
|
|
3. 在数据卷里面创建config文件
|
|
|
|
|
|
|
|
|
|
用户和系统组件两者都可以在ConfigMap里面存储配置数据。
|
|
|
|
|
|
|
|
|
|
其实不用看下面的文章,直接从`kubectl create configmap -h`的帮助信息中就可以对ConfigMap究竟如何创建略知一二了。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 创建ConfigMaps
|
|
|
|
|
|
|
|
|
|
可以使用该命令,用给定值、文件或目录来创建ConfigMap。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
kubectl create configmap
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 使用目录创建
|
|
|
|
|
|
|
|
|
|
比如我们已经有个了包含一些配置文件,其中包含了我们想要设置的ConfigMap的值:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ 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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
使用下面的命令可以创建一个包含目录中所有文件的ConfigMap。
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`—from-file`指定在目录下的所有文件都会被用在ConfigMap里面创建一个键值对,键的名字就是文件名,值就是文件的内容。
|
|
|
|
|
|
|
|
|
|
让我们来看一下这个命令创建的ConfigMap:
|
|
|
|
|
|
2018-02-24 15:17:33 +08:00
|
|
|
|
```yaml
|
2017-05-20 21:03:25 +08:00
|
|
|
|
$ kubectl describe configmaps game-config
|
|
|
|
|
Name: game-config
|
|
|
|
|
Namespace: default
|
|
|
|
|
Labels: <none>
|
|
|
|
|
Annotations: <none>
|
|
|
|
|
|
|
|
|
|
Data
|
|
|
|
|
====
|
|
|
|
|
game.properties: 158 bytes
|
|
|
|
|
ui.properties: 83 bytes
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
我们可以看到那两个key是从kubectl指定的目录中的文件名。这些key的内容可能会很大,所以在kubectl describe的输出中,只能够看到键的名字和他们的大小。 如果想要看到键的值的话,可以使用`kubectl get`:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ kubectl get configmaps game-config -o yaml
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
我们以`yaml`格式输出配置。
|
|
|
|
|
|
|
|
|
|
```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: 2016-02-18T18:34:05Z
|
|
|
|
|
name: game-config
|
|
|
|
|
namespace: default
|
|
|
|
|
resourceVersion: "407"
|
|
|
|
|
selfLink: /api/v1/namespaces/default/configmaps/game-config
|
|
|
|
|
uid: 30944725-d66e-11e5-8cd0-68f728db1985
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 使用文件创建
|
|
|
|
|
|
|
|
|
|
刚才**使用目录创建**的时候我们`—from-file`指定的是一个目录,只要指定为一个文件就可以从单个文件中创建ConfigMap。
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ kubectl create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties
|
|
|
|
|
|
|
|
|
|
$ kubectl get configmaps game-config-2 -o yaml
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-24 15:17:33 +08:00
|
|
|
|
```yaml
|
2017-05-20 21:03:25 +08:00
|
|
|
|
apiVersion: v1
|
|
|
|
|
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
|
|
|
|
|
kind: ConfigMap
|
|
|
|
|
metadata:
|
|
|
|
|
creationTimestamp: 2016-02-18T18:54:22Z
|
|
|
|
|
name: game-config-3
|
|
|
|
|
namespace: default
|
|
|
|
|
resourceVersion: "530"
|
|
|
|
|
selfLink: /api/v1/namespaces/default/configmaps/game-config-3
|
|
|
|
|
uid: 05f8da22-d671-11e5-8cd0-68f728db1985
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`—from-file`这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个目录是一样的。
|
|
|
|
|
|
2017-07-23 15:42:12 +08:00
|
|
|
|
### 使用字面值创建
|
2017-05-20 21:03:25 +08:00
|
|
|
|
|
|
|
|
|
使用文字值创建,利用`—from-literal`参数传递配置信息,该参数可以使用多次,格式如下;
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
|
|
|
|
|
|
|
|
|
|
$ kubectl get configmaps special-config -o yaml
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
apiVersion: v1
|
|
|
|
|
data:
|
|
|
|
|
special.how: very
|
|
|
|
|
special.type: charm
|
|
|
|
|
kind: ConfigMap
|
|
|
|
|
metadata:
|
|
|
|
|
creationTimestamp: 2016-02-18T19:14:38Z
|
|
|
|
|
name: special-config
|
|
|
|
|
namespace: default
|
|
|
|
|
resourceVersion: "651"
|
|
|
|
|
selfLink: /api/v1/namespaces/default/configmaps/special-config
|
|
|
|
|
uid: dadce046-d673-11e5-8cd0-68f728db1985
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Pod中使用ConfigMap
|
|
|
|
|
|
|
|
|
|
**使用ConfigMap来替代环境变量**
|
|
|
|
|
|
|
|
|
|
ConfigMap可以被用来填入环境变量。看下下面的ConfigMap。
|
|
|
|
|
|
2018-02-24 15:17:33 +08:00
|
|
|
|
```yaml
|
2017-05-20 21:03:25 +08:00
|
|
|
|
apiVersion: v1
|
|
|
|
|
kind: ConfigMap
|
|
|
|
|
metadata:
|
|
|
|
|
name: special-config
|
|
|
|
|
namespace: default
|
|
|
|
|
data:
|
|
|
|
|
special.how: very
|
|
|
|
|
special.type: charm
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
apiVersion: v1
|
|
|
|
|
kind: ConfigMap
|
|
|
|
|
metadata:
|
|
|
|
|
name: env-config
|
|
|
|
|
namespace: default
|
|
|
|
|
data:
|
|
|
|
|
log_level: INFO
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
我们可以在Pod中这样使用ConfigMap:
|
|
|
|
|
|
2018-02-24 15:17:33 +08:00
|
|
|
|
```yaml
|
2017-05-20 21:03:25 +08:00
|
|
|
|
apiVersion: v1
|
|
|
|
|
kind: Pod
|
|
|
|
|
metadata:
|
|
|
|
|
name: dapi-test-pod
|
|
|
|
|
spec:
|
|
|
|
|
containers:
|
|
|
|
|
- name: test-container
|
|
|
|
|
image: gcr.io/google_containers/busybox
|
|
|
|
|
command: [ "/bin/sh", "-c", "env" ]
|
|
|
|
|
env:
|
|
|
|
|
- name: SPECIAL_LEVEL_KEY
|
|
|
|
|
valueFrom:
|
|
|
|
|
configMapKeyRef:
|
|
|
|
|
name: special-config
|
|
|
|
|
key: special.how
|
|
|
|
|
- name: SPECIAL_TYPE_KEY
|
|
|
|
|
valueFrom:
|
|
|
|
|
configMapKeyRef:
|
|
|
|
|
name: special-config
|
|
|
|
|
key: special.type
|
|
|
|
|
envFrom:
|
|
|
|
|
- configMapRef:
|
|
|
|
|
name: env-config
|
|
|
|
|
restartPolicy: Never
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这个Pod运行后会输出如下几行:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
SPECIAL_LEVEL_KEY=very
|
|
|
|
|
SPECIAL_TYPE_KEY=charm
|
|
|
|
|
log_level=INFO
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**用ConfigMap设置命令行参数**
|
|
|
|
|
|
|
|
|
|
ConfigMap也可以被使用来设置容器中的命令或者参数值。它使用的是Kubernetes的$(VAR_NAME)替换语法。我们看下下面这个ConfigMap。
|
|
|
|
|
|
2018-02-24 15:17:33 +08:00
|
|
|
|
```yaml
|
2017-05-20 21:03:25 +08:00
|
|
|
|
apiVersion: v1
|
|
|
|
|
kind: ConfigMap
|
|
|
|
|
metadata:
|
|
|
|
|
name: special-config
|
|
|
|
|
namespace: default
|
|
|
|
|
data:
|
|
|
|
|
special.how: very
|
|
|
|
|
special.type: charm
|
|
|
|
|
```
|
|
|
|
|
|
2017-08-28 19:01:52 +08:00
|
|
|
|
为了将ConfigMap中的值注入到命令行的参数里面,我们还要像前面那个例子一样使用环境变量替换语法`${VAR_NAME)`。(其实这个东西就是给Docker容器设置环境变量,以前我创建镜像的时候经常这么玩,通过docker run的时候指定-e参数修改镜像里的环境变量,然后docker的CMD命令再利用该$(VAR_NAME)通过sed来修改配置文件或者作为命令行启动参数。)
|
2017-05-20 21:03:25 +08:00
|
|
|
|
|
2018-02-24 15:17:33 +08:00
|
|
|
|
```yaml
|
2017-05-20 21:03:25 +08:00
|
|
|
|
apiVersion: v1
|
|
|
|
|
kind: Pod
|
|
|
|
|
metadata:
|
|
|
|
|
name: dapi-test-pod
|
|
|
|
|
spec:
|
|
|
|
|
containers:
|
|
|
|
|
- name: test-container
|
|
|
|
|
image: gcr.io/google_containers/busybox
|
|
|
|
|
command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
|
|
|
|
|
env:
|
|
|
|
|
- name: SPECIAL_LEVEL_KEY
|
|
|
|
|
valueFrom:
|
|
|
|
|
configMapKeyRef:
|
|
|
|
|
name: special-config
|
|
|
|
|
key: special.how
|
|
|
|
|
- name: SPECIAL_TYPE_KEY
|
|
|
|
|
valueFrom:
|
|
|
|
|
configMapKeyRef:
|
|
|
|
|
name: special-config
|
|
|
|
|
key: special.type
|
|
|
|
|
restartPolicy: Never
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
运行这个Pod后会输出:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
very charm
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**通过数据卷插件使用ConfigMap**
|
|
|
|
|
|
|
|
|
|
ConfigMap也可以在数据卷里面被使用。还是这个ConfigMap。
|
|
|
|
|
|
2018-02-24 15:17:33 +08:00
|
|
|
|
```yaml
|
2017-05-20 21:03:25 +08:00
|
|
|
|
apiVersion: v1
|
|
|
|
|
kind: ConfigMap
|
|
|
|
|
metadata:
|
|
|
|
|
name: special-config
|
|
|
|
|
namespace: default
|
|
|
|
|
data:
|
|
|
|
|
special.how: very
|
|
|
|
|
special.type: charm
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在数据卷里面使用这个ConfigMap,有不同的选项。最基本的就是将文件填入数据卷,在这个文件中,键就是文件名,键值就是文件内容:
|
|
|
|
|
|
2018-02-24 15:17:33 +08:00
|
|
|
|
```yaml
|
2017-05-20 21:03:25 +08:00
|
|
|
|
apiVersion: v1
|
|
|
|
|
kind: Pod
|
|
|
|
|
metadata:
|
|
|
|
|
name: dapi-test-pod
|
|
|
|
|
spec:
|
|
|
|
|
containers:
|
|
|
|
|
- name: test-container
|
|
|
|
|
image: gcr.io/google_containers/busybox
|
|
|
|
|
command: [ "/bin/sh", "-c", "cat /etc/config/special.how" ]
|
|
|
|
|
volumeMounts:
|
|
|
|
|
- name: config-volume
|
|
|
|
|
mountPath: /etc/config
|
|
|
|
|
volumes:
|
|
|
|
|
- name: config-volume
|
|
|
|
|
configMap:
|
|
|
|
|
name: special-config
|
|
|
|
|
restartPolicy: Never
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
运行这个Pod的输出是`very`。
|
|
|
|
|
|
|
|
|
|
我们也可以在ConfigMap值被映射的数据卷里控制路径。
|
|
|
|
|
|
2018-02-24 15:17:33 +08:00
|
|
|
|
```yaml
|
2017-05-20 21:03:25 +08:00
|
|
|
|
apiVersion: v1
|
|
|
|
|
kind: Pod
|
|
|
|
|
metadata:
|
|
|
|
|
name: dapi-test-pod
|
|
|
|
|
spec:
|
|
|
|
|
containers:
|
|
|
|
|
- name: test-container
|
|
|
|
|
image: gcr.io/google_containers/busybox
|
|
|
|
|
command: [ "/bin/sh","-c","cat /etc/config/path/to/special-key" ]
|
|
|
|
|
volumeMounts:
|
|
|
|
|
- name: config-volume
|
|
|
|
|
mountPath: /etc/config
|
|
|
|
|
volumes:
|
|
|
|
|
- name: config-volume
|
|
|
|
|
configMap:
|
|
|
|
|
name: special-config
|
|
|
|
|
items:
|
|
|
|
|
- key: special.how
|
|
|
|
|
path: path/to/special-key
|
|
|
|
|
restartPolicy: Never
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
运行这个Pod后的结果是`very`。
|