Open vislee opened 2 years ago
k8s通过ConfigMap支持了pods统一的配置方案。
ConfigMap
先通过yaml文件创建ConfigMap资源。
[root@master k8s]# cat cm-appvars-err.yaml
apiVersion: v1 kind: ConfigMap metadata: name: cm-appvars-err data: apploglevel: debug appdatadir: /var/app/data 00app-log-dir: /var/app/log
通过命令kubectl create -f cm-appvars-err.yaml创建ConfigMap资源。
kubectl create -f cm-appvars-err.yaml
通过命令获取和查看ConfigMap
[root@master k8s]# kubectl get cm NAME DATA AGE cm-appvars 2 3d cm-appvars-err 3 11m [root@master k8s]# kubectl describe cm cm-appvars-err Name: cm-appvars-err Namespace: default Labels: <none> Annotations: <none> Data ==== 00app-log-dir: ---- /var/app/log appdatadir: ---- /var/app/data apploglevel: ---- debug BinaryData ==== Events: <none>
apiVersion: v1 kind: Pod metadata: name: cm-test-pod spec: containers: - name: cm-test image: busybox command: ["/bin/sh", "-c", "env | grep APP"] env: - name: APPLOGLEVEL valueFrom: configMapKeyRef: name: cm-appvars key: apploglevel - name: APPDATADIR valueFrom: configMapKeyRef: name: cm-appvars key: appdatadir restartPolicy: Never
apiVersion: v1 kind: Pod metadata: name: cm-test-envfrom-pod spec: containers: - name: cm-envfrom-test image: busybox command: ["/bin/sh", "-c", "env"] envFrom: - configMapRef: name: cm-appvars-err restartPolicy: Never
最后,我们通过命令kubectl create -f cm-test-envfrom-pod.yaml 创建Pod。
kubectl create -f cm-test-envfrom-pod.yaml
查看创建的pod
[root@master k8s]# kubectl get pods NAME READY STATUS RESTARTS AGE cm-test-envfrom-pod 0/1 Completed 0 17m
kubectl logs cm-test-envfrom-pod 查看pod的日志,少了00app-log-dir 这个环境变量。
kubectl logs cm-test-envfrom-pod
通过命令查看kubectl describe pods cm-test-envfrom-pod,在Events显示如下信息:
kubectl describe pods cm-test-envfrom-pod
Warning InvalidEnvironmentVariableNames 93s kubelet Keys [00app-log-dir] from the EnvFrom configMap default/cm-appvars-err were skipped since they are considered invalid environment variable names.
Pod作为环境变量引用时符合Posix定义的变量命名规范,否则会在创建环境变量的时候跳过,并记录一个event来提示。
概述
k8s通过
ConfigMap
支持了pods统一的配置方案。使用
创建ConfigMap资源
先通过yaml文件创建ConfigMap资源。
[root@master k8s]# cat cm-appvars-err.yaml
通过命令
kubectl create -f cm-appvars-err.yaml
创建ConfigMap资源。通过命令获取和查看ConfigMap
在Pod中使用ConfigMap
[root@master k8s]# cat cm-test-envfrom-pod.yaml
最后,我们通过命令
kubectl create -f cm-test-envfrom-pod.yaml
创建Pod。查看创建的pod
kubectl logs cm-test-envfrom-pod
查看pod的日志,少了00app-log-dir 这个环境变量。通过命令查看
kubectl describe pods cm-test-envfrom-pod
,在Events显示如下信息:Pod作为环境变量引用时符合Posix定义的变量命名规范,否则会在创建环境变量的时候跳过,并记录一个event来提示。