简体   繁体   中英

Use an environment variable as integer in yaml file

I have an application in a container which reads a YAML file which contains data like

              initializationCount=0
              port=980

Now that I want to remove those hard coded values inside the application and get them out of the container. Hence I created a configMap with all configuration values. I used the config map keys as environmental variables while deploying the pod.

My issue is that, If I want to use these environment variables in my yaml file like

            initializationCount=${iCount}
            port=${port}

The API which reads this YAML file throws number format Exception since the env variables are always strings. I do not have control over the API which reads my yaml file.

I have tried

            initializationCount=!!int ${iCount} 

but it does not work.

Rather than pulling in the configmap values as environment variables, try mounting the configmap as a volume at runtime.

The configmap should have one key which is the name of your YAML file. the value for that key should be the contents of the file.

This data will be mounted to the container's filesystem when the pod initializes. That way your app will read the config YAML the same way it has been, but the values will be externalized in the configmap.

Something like this:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-app
      image: my-app:latest
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: app-config
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  config.yaml: |
    initializationCount=0
    port=980

Kubernetes docs here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM