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

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

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

问题

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

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-pod-cfg
data:
  test-pod.yaml: |-
    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
        - name: test
          image: ubuntu
          command: ["/busybox/sh", "-c", "echo $VAR"]    

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

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: test-wf-
spec:
  entrypoint: main
  templates:
    - name: main
      container:
        image: "ubuntu"
        command: ["/bin/sh", "-c", "cat /mnt/vc/test"]
        volumeMounts:
          - name: vc
            mountPath: "/mnt/vc"
      volumes:
        - name: vc
          configMap:
            name: test-pod-cfg
            items:
              - key: test-pod.yaml
                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

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-pod-cfg
data:
  test-pod.yaml: |-
    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
        - name: test
          image: ubuntu
          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

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: test-wf-
spec:
  entrypoint: main
  templates:
    - name: main
      container:
        image: "ubuntu"
        command: ["/bin/sh", "-c", "cat /mnt/vc/test"]
        volumeMounts:
          - name: vc
            mountPath: "/mnt/vc"
      volumes:
        - name: vc
          configMap:
            name: test-pod-cfg
            items:
              - key: test-pod.yaml
                path: test

答案1

得分: 1

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

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

spec:
  entrypoint: test-pod
  templates:
  - name: test-pod
    container:
      image: ubuntu
      command: ["/busybox/sh", "-c", "echo $VAR"]
      volumeMounts:
      - name: config
        mountPath: /etc/config
      env:
      - name: VAR
        valueFrom:
          configMapKeyRef:
            name: test-pod-cfg
            key: test-pod.yaml
    volumes:
    - name: config
      configMap:
        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:

spec:
  entrypoint: test-pod
  templates:
  - name: test-pod
    container:
      image: ubuntu
      command: ["/busybox/sh", "-c", "echo $VAR"]
      volumeMounts:
      - name: config
        mountPath: /etc/config
      env:
      - name: VAR
        valueFrom:
          configMapKeyRef:
            name: test-pod-cfg
            key: test-pod.yaml
    volumes:
    - name: config
      configMap:
        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:

确定