理解 ConfigMaps 和 Pods
1、我们必须在 Pod 使用 ConfigMap 之前,创建好 ConfigMap(除非您把 ConfigMap 标志成”optional”)。如果您引用了一个不存在的 ConfigMap, 那这个Pod是无法启动的。就像引用了不存在的 Key 会导致 Pod 无法启动一样。
2、如果您使用envFrom来从 ConfigMap 定义环境变量,无效的 Key 会被忽略。Pod可以启动,但是无效的名字将会被记录在事件日志里。
3、ConfigMaps 存在于指定的命名空间.则这个 ConfigMap 只能被同一个命名空间里的 Pod 所引用。
一、使用 ConfigMap 数据来定义 Pod 环境变量
1、从单个 ConfigMap 提取数据定义 Pod 的环境变量
# 在 ConfigMap 里将环境变量定义为键值对
kubectl create configmap special-config --from-literal=special.how=very
# 将 ConfigMap 里的special.how值赋给 Pod 的环境变量SPECIAL_LEVEL_KEY
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:
#ConfigMap 中包含了期望赋予 SPECIAL_LEVEL_KEY 的值
name: special-config
# 指定ConfigMap的key
key: special.how
restartPolicy: Never
2、从多个 ConfigMap 读取数据来定义 Pod 环境
# 先创建 ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: default
data:
log_level: INFO
# 在 Pod 配置里定义环境变量.
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: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: env-config
key: special.type
restartPolicy: Never
二、将所有 ConfigMap 的键值对都设置为Pod的环境变量
# 创建一个包含多个键值对的 ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
# 使用env-from来读取 ConfigMap 的所有数据作为Pod的环境变量。ConfigMap 的键名作为环境变量的变量名
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" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
# 现在 Pod 的输出会包含SPECIAL_LEVEL=very和SPECIAL_TYPE=charm
三、 在 Pod 命令里使用 ConfigMap 定义的环境变量
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.level
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
restartPolicy: Never
四、 添加ConfigMap数据到卷
前提: 创建CongfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.level: very
special.type: charm
1、 从 ConfigMap 里的数据生成一个卷
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/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: special-config
restartPolicy: Never
#ls /etc/config/
special.level
special.type
2、添加 ConfigMap 数据到卷里指定路径
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/keys" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: special.level
path: keys
restartPolicy: Never
# cat /etc/config/keys
very
网友评论