secret 用于存放敏感数据,configmap 用于存放普通数据,用法与 secret 类似。
创建
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
启动:
kubectl apply -f configmap1.yml
pod 中使用 configmap(env 方式)
配置文件 pod-configmap1.yml:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ] # 启动时输出环境变量
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
启动:
kubectl apply -f pod-configmap1.yml
查看 pod 日志:
kubectl logs dapi-test-pod | grep SPECIAL
SPECIAL_LEVEL=very
SPECIAL_TYPE=charm
正常读取到了 configmap。
pod 中使用 configmap(volume 方式)
配置文件 pod-configmap2.yml:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod2
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
启动:
kubectl apply -f pod-configmap2.yml
查看日志:
kubectl logs dapi-test-pod2
SPECIAL_LEVEL
SPECIAL_TYPE
成功拿到了 configmap。