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

Kubernetes 来自configmap的环境变量在pod内不可用,kubernetes,configuration,kubectl,Kubernetes,Configuration,Kubectl,我正在尝试将env变量从configmap传递到我的pod。我有以下设置 我有一个文件test config.txt,其中包含2个环境变量 a_sample_env=b c_sample_env=d 我创建了一个configmap,如下所示: apiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: mycontainer image: redis envFrom:

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

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

a_sample_env=b
c_sample_env=d
我创建了一个configmap,如下所示:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: mycontainer
    image: redis
    envFrom:
      - configMapRef:
          name: test-config
kubectl创建configmap测试配置——从文件test-config.txt

我的pod定义如下:

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,并获取环境变量的空值

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

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


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

您应该按如下方式创建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

然后,您可以将测试配置作为卷装入容器中。您必须创建一个包装器/启动脚本,以便在启动期间将该文件中的所有k:v对导出为env变量

您只需使用
--from literal
标志而不是
--from file

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

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: {}
执行pod并检查环境

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

命令
kubectl create configmap test config--from env file test config.txt
对我有效。正如您上面提到的,它创建了一个yaml。