作者:半身风雪
上一节:配置 Java 微服务
内容简介:上一节主要学习创建ConfigMap 的九种方式,那么本节内容主要讲解 ConfigMap 的使用。
本节学习目标:
提示:以下是本篇文章正文内容,下面案例可供参考
$ kubectl create configmap special-config --from-literal=special.how=very
special.how
赋值给 Pod 规约中的 SPECIAL_LEVEL_KEY
环境变量。在 pods/pod-single-configmap-env-variable.yaml
文件目录中添加如下代码:apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
# 定义环境变量
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# ConfigMap 包含你要赋给 SPECIAL_LEVEL_KEY 的值
name: special-config
# 指定与取值相关的键名
key: special.how
restartPolicy: Never
kubectl create -f
命令,创建pod:$ kubectl create -f https://kubernetes.io/examples/pods/pod-single-configmap-env-variable.yaml
Pod 的输出包含环境变量 SPECIAL_LEVEL_KEY=very。
configmap/configmaps.yaml
添加如下代码: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
$ kubectl create -f https://kubernetes.io/examples/configmap/configmaps.yaml
pods/pod-multiple-configmap-env-variable.yaml
中定义环境变量:apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/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: log_level
restartPolicy: Never
$ kubectl create -f https://kubernetes.io/examples/pods/pod-multiple-configmap-env-variable.yaml
Pod 的输出包含环境变量 SPECIAL_LEVEL_KEY=very 和 LOG_LEVEL=INFO。
configmap/configmaps.yaml
添加如下代码: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
$ kubectl create -f https://kubernetes.io/examples/configmap/configmaps.yaml
pods/pod-multiple-configmap-env-variable.yaml
中定义环境变量:apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/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: log_level
restartPolicy: Never
$ kubectl create -f https://kubernetes.io/examples/pods/pod-multiple-configmap-env-variable.yaml
Pod 的输出包含环境变量 SPECIAL_LEVEL_KEY=very 和 LOG_LEVEL=INFO。
configmap/configmap-multikeys.yaml
中创建一个包含多个键值对的 ConfigMap。apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
$ kubectl create -f https://kubernetes.io/examples/configmap/configmap-multikeys.yaml
pods/pod-configmap-envFrom.yaml
目录中,使用 envFrom 将所有 ConfigMap 的数据定义为容器环境变量,ConfigMap 中的键成为 Pod 中的环境变量名称。apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
$ kubectl create -f https://kubernetes.io/examples/pods/pod-configmap-envFrom.yaml
Pod 的输出包含环境变量 SPECIAL_LEVEL=very 和 SPECIAL_TYPE=charm。
pods/pod-configmap-env-var-valueFrom.yaml
中使用**$(VAR_NAME)** Kubernetes 替换语法在容器的 command 和 args 属性中使用 ConfigMap 定义的环境变量。apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/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
$ kubectl create -f https://kubernetes.io/examples/pods/pod-configmap-env-var-valueFrom.yaml
very charm
使用 --from-file
创建 ConfigMap 时,文件名成为存储在 ConfigMap 的 data 部分中的键, 文件内容成为键对应的值。
下面看一个示例configmap/configmap-multikeys.yaml
:
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
创建 ConfigMap:
$ kubectl create -f https://kubernetes.io/examples/configmap/configmap-multikeys.yaml
在 Pod 规约的 volumes 部分下添加 ConfigMap 名称。 会将 ConfigMap 数据添加到 volumeMounts.mountPath
所指定的目录 (在本例中为 /etc/config)。 command 部分列出了名称与 ConfigMap 中的键匹配的目录文件。
pods/pod-configmap-volume.yaml
:apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
# 提供包含要添加到容器中的文件的 ConfigMap 的名称
name: special-config
restartPolicy: Never
$ kubectl create -f https://kubernetes.io/examples/pods/pod-configmap-volume.yaml
ls /etc/config/
产生下面的输出:SPECIAL_LEVEL
SPECIAL_TYPE
使用 path 字段为特定的 ConfigMap 项目指定预期的文件路径。 在这里,ConfigMap 中键 SPECIAL_LEVEL
的内容将挂载在 config-volume 卷中 /etc/config/keys
文件中。
pods/pod-configmap-volume-specific-key.yaml
示例代码:apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/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
$ kubectl create -f https://kubernetes.io/examples/pods/pod-configmap-volume-specific-key.yaml
cat /etc/config/keys
产生以下输出:very
ConfigMap API 资源将配置数据存储为键值对。 数据可以在 Pod 中使用,也可以用来提供系统组件(如控制器)的配置。 ConfigMap 与 Secret 类似, 但是提供的是一种处理不含敏感信息的字符串的方法。 用户和系统组件都可以在 ConfigMap 中存储配置数据。
ConfigMap 的 data 字段包含配置数据。如下例所示,它可以简单 (如用 --from-literal
的单个属性定义)或复杂 (如用 --from-file
的配置文件或 JSON blob定义)。
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: 2016-02-18T19:14:38Z
name: example-config
namespace: default
data:
# 使用 --from-literal 定义的简单属性
example.property.1: hello
example.property.2: world
# 使用 --from-file 定义复杂属性的例子
example.property.file: |-
property.1=value-1
property.2=value-2
property.3=value-3
$ kubectl get events
输出与此类似:
LASTSEEN FIRSTSEEN COUNT NAME KIND SUBOBJECT TYPE REASON SOURCE MESSAGE
0s 0s 1 dapi-test-pod Pod Warning InvalidEnvironmentVariableNames {kubelet, 127.0.0.1} Keys [1badkey, 2alsobad] from the EnvFrom configMap default/myconfig were skipped since they are considered invalid environment variable names.
文章写到这里,我想大家已经ConfigMap 有了一定的了解,对于 ConfigMap 的讲解,到这里就结束了,大家快动手试试吧!!!