如何将环境变量传递给在Kubernetes中作为卷访问的ConfigMap中的脚本

huangapple go评论90阅读模式
英文:

How to pass environment variables to a script present in ConfigMap while accessing it as a volume in Kubernetes

问题

我有以下的ConfigMap,其中包含一个名为VAR的变量。在访问该变量作为一个卷时,这个变量应该从工作流中获取值。

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: test-pod-cfg
  5. data:
  6. test-pod.yaml: |-
  7. apiVersion: v1
  8. kind: Pod
  9. metadata:
  10. name: test-pod
  11. spec:
  12. containers:
  13. - name: test
  14. image: ubuntu
  15. command: ["/busybox/sh", "-c", "echo $VAR"]

以下是一个 Argo 工作流,它正在从 ConfigMap 中提取脚本 test-pod.yaml 并将其添加为容器的一个卷。在这里,如何动态传递环境变量 VAR 给 ConfigMap 以进行替换呢?

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: Workflow
  3. metadata:
  4. name: test-wf-
  5. spec:
  6. entrypoint: main
  7. templates:
  8. - name: main
  9. container:
  10. image: "ubuntu"
  11. command: ["/bin/sh", "-c", "cat /mnt/vc/test"]
  12. volumeMounts:
  13. - name: vc
  14. mountPath: "/mnt/vc"
  15. volumes:
  16. - name: vc
  17. configMap:
  18. name: test-pod-cfg
  19. items:
  20. - key: test-pod.yaml
  21. path: test
英文:

I have the following ConfigMap which is having a variable called VAR. This variable should get the value from the workflow while accessing it as a volume

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: test-pod-cfg
  5. data:
  6. test-pod.yaml: |-
  7. apiVersion: v1
  8. kind: Pod
  9. metadata:
  10. name: test-pod
  11. spec:
  12. containers:
  13. - name: test
  14. image: ubuntu
  15. command: ["/busybox/sh", "-c", "echo $VAR"]

Here is the argo workflow which is fetching script test-pod.yaml in ConfigMap and adding it as a volume to container. In this how to pass Environment variable VAR to the ConfigMap for replacing it dynamically

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: Workflow
  3. metadata:
  4. name: test-wf-
  5. spec:
  6. entrypoint: main
  7. templates:
  8. - name: main
  9. container:
  10. image: "ubuntu"
  11. command: ["/bin/sh", "-c", "cat /mnt/vc/test"]
  12. volumeMounts:
  13. - name: vc
  14. mountPath: "/mnt/vc"
  15. volumes:
  16. - name: vc
  17. configMap:
  18. name: test-pod-cfg
  19. items:
  20. - key: test-pod.yaml
  21. path: test

答案1

得分: 1

要将ConfigMap挂载为一个卷并使环境变量VAR在容器中可用,您需要在Pod的规范中添加一个卷,并在容器的规范中设置环境变量。

在卷规范中,您需要将ConfigMap添加为卷源,并设置包含环境变量的文件的路径。例如:

  1. spec:
  2. entrypoint: test-pod
  3. templates:
  4. - name: test-pod
  5. container:
  6. image: ubuntu
  7. command: ["/busybox/sh", "-c", "echo $VAR"]
  8. volumeMounts:
  9. - name: config
  10. mountPath: /etc/config
  11. env:
  12. - name: VAR
  13. valueFrom:
  14. configMapKeyRef:
  15. name: test-pod-cfg
  16. key: test-pod.yaml
  17. volumes:
  18. - name: config
  19. configMap:
  20. name: test-pod-cfg

环境变量VAR然后将在容器中以ConfigMap中指定的值可用。

要了解更多信息,请查看官方文档

英文:

To mount the ConfigMap as a volume and make the environment variable VAR available to the container, you will need to add a volume to the pod's spec and set the environment variable in the container's spec.

In the volume spec, you will need to add the ConfigMap as a volume source and set the path to the file containing the environment variable. For example:

  1. spec:
  2. entrypoint: test-pod
  3. templates:
  4. - name: test-pod
  5. container:
  6. image: ubuntu
  7. command: ["/busybox/sh", "-c", "echo $VAR"]
  8. volumeMounts:
  9. - name: config
  10. mountPath: /etc/config
  11. env:
  12. - name: VAR
  13. valueFrom:
  14. configMapKeyRef:
  15. name: test-pod-cfg
  16. key: test-pod.yaml
  17. volumes:
  18. - name: config
  19. configMap:
  20. name: test-pod-cfg

The environment variable VAR will then be available in the container with the value specified in the ConfigMap.

For more information follow this official doc.

huangapple
  • 本文由 发表于 2023年2月18日 01:37:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定