Unveiling the enigma: Kubernetes deployment rendered with undefined environmental

I recently posted a question on Stack Overflow having a similar issue. I followed the instructions provided in the answer of this post: How to get access to Kubernetes container environment variables from Next.js application?. However, even after calling my environment variables using process.env.USERNAME, I am still getting an undefined value. Is there something wrong with my deployment file? Below is a snippet of my deployment.yaml:

metadata:
  namespace: <namespace>
  releaseName: <release name>
  releaseVersion: 1.0.0
  target: <target>
auth:
replicaCount: 1
image:
  repository: '<name of repository is here>'
  pullPolicy: <always>
container:
  multiPorts:
  - containerPort: 443
    name: HTTPS
    protocol: TCP
  - containerPort: 80
    name: HTTP
    protocol: TCP
  env:
  - name: USERNAME
    valueFrom:
      secretKeyRef:
        name: my-username
        key: username
  - name: PASSWORD
    valueFrom:
      secretKeyRef:
        name: my-password
        key: password
  - name: HOST
    valueFrom:
      secretKeyRef:
        name: my-host
        key: host
  volumeMounts:
  - name: config
    mountPath: "/configMap"
    readOnly: true
volume:
  - name: config
    configMap:
      name: environmental-variables
resources:
  requests:
    cpu: 0.25
    memory: 256Mi
  limits:
    cpu: 1
    memory: 1024Mi
variables:
- name: NODE_ENV
  value: <node env value here>
ingress:
  enabled: true
  ingressType: <ingressType>
  applicationType: <application type>
  serviceEndpoint: <endpoint>
  multiPaths:
    - path: /
      name: HTTPS
tls: 
  enabled: true
  secretName: <name>
autoscale:
  enabled: false
  minReplicas: 1
  maxReplicas: 5
  cpuAverageUtilization: 50
  memoryUtilizationValue: 50
annotations:
  ingress:
    nginx.ingress.kubernetes.io/affinity: <affinity>
    nginx.ingress.kubernetes.io/session-cookie-name: <cookie-name>
    nginx.ingress.kubernetes.io/session-cookie-expires: <number>
    nginx.ingress.kubernetes.io/session-cookie-max-age: <number>

To address my concern, I also created a separate configMap.yaml file. Though I'm unsure if this is the correct approach. Here is how my configMap.yaml file looks like:

apiVersion: v1
kind: ConfigMap
metadata:
  name: environmental-variables
data:
  .env: |
    USERNAME: <username>
    PASSWORD: <password>
    HOST: <host>

I would greatly appreciate any assistance in resolving this issue! Also, for security reasons, I am looking to store my environment variables as secrets without exposing them. The application I am working on is built using Node.js and Express framework. Thank you!

EDIT: Adding the Secrets part of my yaml file for reference:

secrets:
  - name: environmental-variables
    key: USERNAME
  - name: environmental-variables
    key: PASSWORD

This is how my Secrets yaml file appears:

kind: Secret
apiVersion: v1
metadata:
  name: environmental-variables
  namespace: tda-dev-duck-dev
data:
  USERNAME: <username>
  PASSWORD: <password>

Answer №1

After spending a considerable amount of time figuring out the best way to utilize Secrets as an environmental variable, I finally discovered the solution for referencing it within my nodejs application!

Prior to this realization, I was using the standard method of accessing environmental variables through process.env.VARIABLE_NAME. However, this approach failed to work when dealing with Secrets set as environment variables. To obtain the value of the variable, I had to modify my JavaScript file by utilizing

process.env.ENVIRONMENTAL_VARIABLES_USERNAME
, where ENVIRONMENTAL_VARIABLES represents the name and USERNAME acts as the key.

I'm not entirely sure if this information will be beneficial to others, but it certainly helped me overcome the challenge of accessing Secrets in my nodejs application!

Answer №2

You have successfully created a ConfigMap and are now attempting to retrieve values from a secret. To set the values from the configmap, please update the environment as shown below:

env:
  - name: USERNAME
    valueFrom:
      configMapKeyRef:
        name: environmental-variables_xyz # This is the name of the ConfigMap
        key: USERNAME_xyz # This is the specific key in the ConfigMap
  - name: PASSWORD
    valueFrom:
      configMapKeyRef:
        name: environmental-variables_xyz
        key: PASSWORD_xyz
  - name: HOST
    valueFrom:
      configMapKeyRef:
        name: environmental-variables_xyz
        key: HOST_xyz

Additionally, make sure to update the configmap with the appropriate values:

apiVersion: v1
kind: ConfigMap
metadata:
  name: environmental-variables_xyz
data:
  USERNAME: <username>
  PASSWORD: <password>
  HOST: <host>

If you would like to learn more about defining container environment variables using ConfigMap data, please click here.

To use secrets as environment variables, please refer to the information provided here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The installation of robotjs via npm failed due to issues encountered while trying to build the binaries

After attempting to execute the command "npm install robotjs -g," an error is thrown back at me. [email protected] install C:\Users\Ehsan\AppData\Roaming\npm\node_modules\robotjs prebuild-install || node-gyp reb ...

The most effective method for dynamically incorporating tables into a Node.js and Express application using Sequelize

After conducting some research, I have hit a roadblock in finding the right solution. My project involves creating a personal budgeting application using node, Express, and Sequelize. The goal is to allow users maximum flexibility by dynamically generating ...

The Maven build encountered an error while trying to execute a goal, resulting in the inability to

I'm currently working on a Windows 7 computer and I've received some code that requires me to execute "mvn install" in order to build the application. However, when I try to run this command, I encounter the following error: Failed to execute ...

Can you guide me on how to display data in expressjs using mongoose?

Recently I started working with mongoose and I'm attempting to retrieve data from my mongodb using mongoose. Below is the code snippet I am currently using: mongoose.connect('mongodb://localhost/dbname'); var Schema = mongoose.Schema; var d ...

Encountering issues transmitting information to MongoDB via NodeJs

I'm completely new to NodeJs, MongoDB, and web development in general. Recently, I came across a tutorial from about 2 years ago on creating a registration system. The author was able to successfully send a post request using Postman and save the data ...

Guide to hosting Word, Excel, PowerPoint, and PDF files on a Node.js server

I recently managed to transfer a file from my MongoDB database to my node.js server Below is a screenshot for reference: View the Screenshot here However, I am unsure of how to serve these files from the node.js server to the client side. Any help in a ...

ERROR: The secure connection certificate for localhost is self-signed

While working on localhost, I encountered an error with nodemailer that resulted in an internal server error (500): ERROR: SELF_SIGNED_CERT_IN_CHAIN A thread discussing this issue can be found here: Npm SELF_SIGNED_CERT_IN_CHAIN on Azure. It suggested the ...

Error Encountered - Node.js application experiencing issues in passport login functionality

I'm in the process of developing a login application using nodejs and incorporating passport js for authentication. The app is connected to a local MySql database and utilizes sequelize as its ORM library. Within my user model, I've implemented ...

Deploying Docker Compose in a Production Environment

Currently attempting to utilize docker-compose in order to build and start a basic Node.js application. I encountered a similar issue when working with a Django application, so it seems like there may be a crucial step that I am overlooking. Below is the c ...

Encountering numerous issues during my attempt to perform an npm install command

After cloning a git repository, I encountered an issue when trying to run the app in the browser. Despite running "npm install," some dependencies were not fully installed. Upon attempting to run "npm install" again, the following errors were displayed: np ...

Having trouble with eslint in create-react-app because of a parent folder that also has another app with its own node_modules folder?

I have a git repository with two projects inside: a loopback app (named app) and a create-react-app react app (named client). Here is the directory structure: ├─┬app │ ├──node_modules │ ├─┬client ├─node_modules The loopback ...

The specified path is not found within the JsonFilter

Something seems off. I'm using Prisma with a MongoDB connection and attempting to search the JSON tree for specific values that match the [key, value] from the loop. However, I haven't made much progress due to an error with the path property. Be ...

The attempt to establish a connection with localhost at port 1433 was unsuccessful due to a connection refusal on 127.0.0.1:1433

Currently, I am attempting to establish a connection to my local MySql server from a node.js/express application using mssql with the following configuration: // Database connection var sql = require('mssql'); var config = { user: 'db_u ...

Loopback issue: Access Denied

I'm facing an issue with my loopback app that uses mongoDB. When logging in as Admin, I'm unable to use the post method on dishes and receive an authorization required error. The only way to make it work is by changing the dishes role to ALLOW ev ...

Max Savings Seeker API Version Four

Currently, I am working on incorporating the Bargain Finder Max Rest API into my Node.js application. However, the data being returned is not as complete or useful as I had hoped. Does anyone have any suggestions on how to obtain more comprehensive data? D ...

Unable to decipher the mysterious map of nothingness

I am currently working on a GET method in Node.js. My goal is to fetch data using the GET request and then use the MAP function to gather all the names into an array. However, I encountered the following error: /root/server.js:21 ...

Utilize node.js to extract parameters from various parts of the chain and maintain a linear workflow

Is it possible to maintain clean code while fetching -paramFromA and paramFromB in this way? All of the functions included here return a new Promise. var a = helper.getAccountPromise(tokens); var b = a.then(helper.getFundingPromise) var c = b.then(h ...

Choosing the Right SQLite Library for Node.js

Currently, I am in the process of developing an application using node.js. For the embedded database functionality, I have decided to use SQLite. After exploring various npm modules for SQLite, I came across a few options: https://github.com/grumdrig/nod ...

What is the best method for storing a third-party image in cache?

Running my website, I aim to achieve top-notch performance scores using LightHouse. I have successfully cached all the images I created (Cache-Control: public, max-age=31536000). Unfortunately, third-party website images are not cached. How can I cache t ...

boost the amount of responses received per second

I have developed an android game with a user base of 40,000 online users. Each user is sending a request to the server every 5 seconds. Below is the code I have written to test these requests: const express = require('express') const app = ex ...