reactjsnode.jskubernetes

K8s node.js pod setup env.js from .yaml deployment


I have a container where i released a node.js frontend, some of my envs are stored in an env.js, how can i configure this envs from the yaml file of the deployment (using the env in deployment is not working).

In alternative i tried setupping a pvc to mount in the pod the env.js that i deposit in the pv but it is not working as the file is copied as a directory and idk why.

env.js:

window.env = { "API_URL": "http://ip:port" }

Solution

  • I ended up finding that what David said was on the right track, apparently the file env.js cant be in the same folder as the application, but if you set it in a subfolder for example env/env.js and configuring the ConfigMap to write the file actually works.

    ConfigMap:

    apiVersion: v1
    kind: ConfigMap
    metadata:    
      name: cfg-map
    data:
      env.js: |
        window.env = {
          "API_URL": "http://ip:port"
        }
    

    Deployment:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deployment
    spec:
      ...
      selector:
        spec:
          ...
          volumeMounts:
          - name: storage
            mountPath: /usr/share/nginx/html/env
        volumes:
        - name: storage
          configMap:
            name: cfg-map
            items:
            - key: "env.js"
              path: "env.js"