configmap 是 kubernetes 中与 pod、service 同一级别的组件,它里面以键值对的形式存放着创建pod 所需的变量。configmap 存在的意义就是当pod创建时将其所需要的变量都”注入“ pod 中,这里的”注入“刚开始听到的时候着实有点困惑,现在觉得这个”注入“也是蛮有道理的。所谓”注入“,就是:当 pod 创建时,需要什么变量,就从存放这个变量的 configmap 中拿。对比想想 Spring Boot 中的 application.properties 文件,它里面存放着各种配置,甚至是各种环境下的配置。等代码中实际使用某个变量时,只需要通过 @Value
这个注解,就可以将配置文件中的变量值注入到代码的实际变量中。kubernetes 集群中所有的 configmap 发挥着与 Spring Boot 中的 application.properties 文件相同的作用。
本文参考官方资料:
Configure a Pod to Use a ConfigMap
创建comfigMap
在注入之前必须先创建configmap,并且将相关变量存入其中。创建configmap有三种方式:从目录创建、从文件创建、从字面量创建。
从字面量创建(literal values)
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
查看结果:kl describe configmaps special-config
Name: special-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
special.how:
----
very
special.type:
----
charm
Events: <none>
special-config 中有两个 item(条目),key 分别为:special.how
和 special.type
,他们各自各自的 value 分别为:very
和 charm
。
从目录创建
创建目录并下载两个文件
# Create the local directory
mkdir -p configure-pod-container/configmap/
# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties
从目录创建 configmap
kubectl create configmap game-config --from-file=configure-pod-container/configmap/
查看结果:kubectl describe configmaps 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
configure-pod-container/configmap/
路径下的文件名为key
,文件中的内容为value
从文件创建
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
查看结果:kubectl describe configmaps game-config-2
Name: game-config-2
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
从文件创建并自定义key
在用文件创建configmap时,默认文件名做为key,文件内容作为value。但我们可以选择不用文件名做为key,而是自己指定,这样做的目的就是为了“注入”时用起来方便。
创建configmap:
kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties
查看结果:kubectl get configmaps game-config-3 -o yaml
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: 2016-02-18T18:54:22Z
name: game-config-3
namespace: default
resourceVersion: "530"
uid: 05f8da22-d671-11e5-8cd0-68f728db1985
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
这块留心看下:data
中的 key 变成了 game-special-key
而不是之前的文件名。
这就是三种创建 configmap 的方式,这是命令行的方式演示的。
注入
创建 configmap 的目的是要将它里面的值注入到 pod 中去,最后再将这些值“弄到” container ,应为应用程序是跑在容器中的。将configmap 中的键值对“弄到” container 中有两种方式:
作为 container 的环境变量
先创建configmap:
kubectl create configmap special-config --from-literal=special.how=very
在上面我们创建了 special-config
这个 configmap。这块我们将special-config
中的变量作为container 的环境变量,从而使得 container 中的应用程序就可以使用这些配置变量了。
创建pod:dapi-test-pod
:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
restartPolicy: Never
稍微解释下: 在上面的 yaml 中 创建了pod:dapi-test-pod
,pod 中用镜像 :busybox
创建了 容器:test-container
。在test-container
中定义了一个环境变量:SPECIAL_LEVEL_KEY
,这个环境变量的值是从名字叫:special-config
的configmap 中的 key:special.how 的值获取到的。这块可能有点绕口,理解万岁!!!
那怎么确认有没有注入成功呢?其实很简单,我们只需要看下container中是不是有环境变量:SPECIAL_LEVEL_KEY
,并且它的值是不是跟 special-config
中的一样就可以了。
pod:dapi-test-pod
中执行命令:command: [ "/bin/sh", "-c", "env" ]
这样会把环境变量打到标准输出中,我们只需要查看容器的日志就可以了。
查看日志:kubectl logs dapi-test-pod
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
SPECIAL_LEVEL_KEY=very
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
至于configmap中的配置变量作为container的命令行参数,这个用法大家自己查吧,很简单的。
作为 container 中路径下的文件
这种用法是 configmap 中的配置作为 container 中指定路径下的文件,configmap 中键值对的 key 作为文件名,value 作为文件的内容。
这里我们看个栗子:
先创建configmap,这次用yaml文件的方式,内容如下:
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
special-config 中有两个键值对 SPECIAL_LEVEL: very
和 SPECIAL_TYPE: charm
创建configmap:
kubectl create -f configmap-multikeys.yaml
创建 pod:
kubectl create -f pod-configmap-volume.yaml
pod-configmap-volume.yaml:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
解释一下:在上面的yaml中,pod:dapi-test-pod
挂载了一个名字叫 special-config
的 configMap
类型的卷 (volume)
,并且给 special-config
取了一个别名叫 config-volume
,这个别名只能被容器 test-container
使用。test-container
将卷:config-volume
挂载到了 /etc/config
路径下。因为 special-config
中的配置变量是两个键值对,所以在容器的 /etc/config
路径下会出来呢两个文件。
查看日志:kubectl logs dapi-test-pod
SPECIAL_LEVEL
SPECIAL_TYPE
可以看到日志中有两个文件名,但这样看不是很明显,为了看得更清楚些,我们可以选择进入容器的方式进行查看。
这里我们得首先修改下yaml文件,因为上面的 command: [ "/bin/sh", "-c", "ls /etc/config/" ]
命令执行一下就完事了,pod会处于 completed
状态。只有 running
状态的 pod 才能进入它里面的容器。所以,将上面yaml中的command 改为:
command: ["/bin/sh","-c","while true; do echo 'hello diego'; sleep 3; done "]
进入容器:
kubectl exec -it dapi-test-pod /bin/sh
查看挂载的目录:
/ # ls /etc/config/
SPECIAL_LEVEL SPECIAL_TYPE
查看文件内容:
cat /etc/config/SPECIAL_TYPE
charm
很明显,将configmap中的键值对挂载了容器的 /etc/config/
路径下,key是文件名,value是文件内容。
关于 Secret
secret 和 configmap 的存在的意义是一样的,不同点是 secret 中存放的都是些安全敏感的配置信息,具体内容,后续再整理吧。