来自configmap的环境变量在pod内不可用

时间:2019-06-13 09:59:53

标签: kubernetes configuration kubectl

我正在尝试从configmap将env变量传递给我的pod。我有以下设置。

我有一个文件 test-config.txt ,其中包含2个环境变量

a_sample_env=b
c_sample_env=d

我创建一个如下的configmap:

  

kubectl create configmap test-config --from-file test-config.txt

我的广告连播定义如下:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: mycontainer
    image: redis
    envFrom:
      - configMapRef:
          name: test-config

但是我的应用程序在test-config.txt文件中没有收到2个env变量。我使用kubectl exec登录到pod并获取了env变量的空值。

root@test-pod:/data# echo $c_sample_env

root@test-pod:/data# echo $a_sample_env

有人能指出为什么豆荚中没有环境变量吗?

2 个答案:

答案 0 :(得分:2)

您应按以下方式创建configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  a_sample_env: b
  c_sample_env: d

如果使用以下命令创建configmap,则

kubectl create configmap test-config --from-file test-config.txt

然后您可以将test-config作为卷安装在容器中。您将必须创建一个包装器/启动脚本,以在启动过程中将该文件中的所有k:v对导出为env变量

答案 1 :(得分:0)

您可以简单地使用--from-literal标志而不是--from-file来创建秘密

kubectl create cm test-config --from-literal=a_sample_env=b --from-literal=c_sample_env=d

创建广告连播

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: test-pod
  name: test-pod
spec:
  containers:
  - image: redis
    imagePullPolicy: IfNotPresent
    name: test-pod
    resources: {}
    envFrom:
    - configMapRef:
        name: test-config
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

执行到吊舱并检查环境

root@test-pod:/data# env | grep sample
c_sample_env=d
a_sample_env=b