环境变量返回不确定的Kubernetes部署 [英] Environmental variables returning undefined for Kubernetes deployment
问题描述
我发布了类似的问题为此,并尝试实施该问题的答案所说的:
I posted a question similar to this and tried to implement what the answer for this question said: How to access Kubernetes container environment variables from Next.js application?
但是,当我仍然将环境变量称为process.env.USERNAME
时,我仍然没有得到定义...我在部署文件中做错了吗?这是我的deployment.yaml
的副本:
However, when I still call my environment variables doing process.env.USERNAME
, I'm still getting undefined back... Am I doing something wrong in my deployment file? Here is a copy 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: /
- 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>
我也创建了一个configMap.yaml
文件,尽管我不确定这是否是正确的方法.这是我的configMap.yaml
文件:
I also created a configMap.yaml
file, although I'm not sure if that's the right way to do this. Here is my configMap.yaml
file:
apiVersion: v1
kind: ConfigMap
metadata:
name: environmental-variables
data:
.env: |
USERNAME: <username>
PASSWORD: <password>
HOST: <host>
任何帮助将不胜感激!另外,由于我不想公开任何变量,因为它包含敏感信息,因此我试图将其环境变量设置为Secrets.我正在尝试使用Express在Node.js应用程序上执行此操作.谢谢!
Any help will be greatly appreciated! Also I'm trying to make my environment variable as Secrets since I don't want to expose any of my variables because it contains sensitive information. I am trying to do this on a Node.js application using Express. Thank you!
这是我的Yaml文件中的秘密"部分的样子
Here is how the Secrets part looks like in my yaml file
secrets:
- name: environmental-variables
key: USERNAME
- name: environmental-variables
key: PASSWORD
我的秘密" yaml文件的外观如下:
How my Secrets yaml file looks like:
kind: Secret
apiVersion: v1
metadata:
name: environmental-variables
namespace: tda-dev-duck-dev
data:
USERNAME: <username>
PASSWORD: <password>
推荐答案
经过数天的研究,我发现如何将Secrets用作环境变量,然后我想出了如何在我的nodejs应用程序中引用它!
After days of figuring out how to use Secrets as an environmental variable, I figured out how to reference it in my nodejs application!
在使用通常的方式调用环境变量process.env.VARIABLE_NAME
之前,但是当我将Secrets作为环境变量使用时,这对我不起作用.为了获取变量的值,我必须在Javascript文件中执行process.env.ENVIRONMENTAL_VARIABLES_USERNAME
,这对我有用!其中ENVIRONMENTAL_VARIABLES
是name
,而USERNAME
是密钥!
Before I was doing the normal way of calling environmental variables, process.env.VARIABLE_NAME
, but that did not work for me when I had Secrets as an environment variable. In order to get the value of the variable, I had to do process.env.ENVIRONMENTAL_VARIABLES_USERNAME
in my Javascript file and that worked for me! Where ENVIRONMENTAL_VARIABLES
is the name
and USERNAME
is the key!
不确定这是否对其他人有帮助,但这是我设法在nodejs应用程序中访问我的Secrets的方法!
Not sure if this will help anyone else but this is how I managed to access my Secrets in my nodejs application!
这篇关于环境变量返回不确定的Kubernetes部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!