ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中。使用时,Pod可以将其用作环境变量、命令行参数或者存储卷中的配置文件。
创建ConfigMap
创建configMap可以基于目录,文件或者字面值。
基于目录创建ConfigMap
# 拉取配置文件,将其保存在本地目录
wget https://kubernetes.io/examples/configmap/game.properties
wget https://kubernetes.io/examples/configmap/ui.properties
# 创建configMap
kubectl create configmap game-config --from-file=./
可以通过kubectl get configmaps game-config
查看创建的configMap实例
NAME DATA AGE
game-config 2 3s
可以通过如下命令,查看configMap中的具体信息
kubectl get configmaps game-config -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
基于文件创建
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties定义从文件创建ConfgiMap时使用的健
kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>其中,my-key-name
是要在ConfigMap中使用的键名,path-to-file
是你想要健表示数据源文件的位置。
kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties根据字面值创建ConfigMap
kubectl create configmap hello-config --from-literal=test=helloconfigMap在Pod中的映射
映射环境变量
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: registry.aliyuncs.com/google_containers/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# 指定configMap的名称
name: hello-config
# 这是configMap中data的key
key: test
restartPolicy: Never
映射文件路径
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: registry.aliyuncs.com/google_containers/busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
# Provide the name of the ConfigMap containing the files you want
# to add to the container
name: hello-config
restartPolicy: Never
挂载的 ConfigMap 将自动更新
更新已经在数据卷中使用的 ConfigMap 时,已映射的键最终也会被更新。kubelet
在每次定期同步时都会检查已挂载的 ConfigMap 是否是最新的。更新 ConfigMap 到将新键映射到 Pod 的总延迟可能与 kubelet 同步周期 + ConfigMap 在 kubelet 中缓存的 TTL 一样长。
文章转载自慢行的蜗牛,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




