为 Kubernetes 部署返回未定义的环境变量
Environmental variables returning undefined for Kubernetes deployment
我发了一个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
:
的副本
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
文件:
apiVersion: v1
kind: ConfigMap
metadata:
name: environmental-variables
data:
.env: |
USERNAME: <username>
PASSWORD: <password>
HOST: <host>
任何帮助将不胜感激!此外,我正在尝试将我的环境变量设置为 Secrets,因为我不想公开我的任何变量,因为它包含敏感信息。我正在尝试使用 Express 在 Node.js 应用程序上执行此操作。谢谢!
编辑:这是 Secrets 部分在我的 yaml 文件中的样子
secrets:
- name: environmental-variables
key: USERNAME
- name: environmental-variables
key: PASSWORD
我的 Secrets yaml 文件的样子:
kind: Secret
apiVersion: v1
metadata:
name: environmental-variables
namespace: tda-dev-duck-dev
data:
USERNAME: <username>
PASSWORD: <password>
您创建了 ConfigMap
并试图从秘密中获取价值。如果你想从 configmap 中设置值,那么像下面这样更新 env
env:
- name: USERNAME
valueFrom:
configMapKeyRef:
name: environmental-variables # this is ConfigMap Name
key: USERNAME # this is key in ConfigMap
- name: PASSWORD
valueFrom:
configMapKeyRef:
name: environmental-variables
key: PASSWORD
- name: HOST
valueFrom:
configMapKeyRef:
name: environmental-variables
key: HOST
并像下面这样更新 configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: environmental-variables
data:
USERNAME: <username>
PASSWORD: <password>
HOST: <host>
要了解如何使用 ConfigMap 数据定义容器环境变量,请单击 here
如果您想将机密用作环境变量,请检查here
经过几天弄清楚如何将 Secrets 用作环境变量后,我弄清楚了如何在我的 nodejs 应用程序中引用它!
在我以正常方式调用环境变量之前,process.env.VARIABLE_NAME
,但是当我将 Secrets 作为环境变量时,这对我不起作用。为了获得变量的值,我必须在 Javascript 文件中执行 process.env.ENVIRONMENTAL_VARIABLES_USERNAME
,这对我有用!其中 ENVIRONMENTAL_VARIABLES
是 name
而 USERNAME
是关键!
不确定这是否对其他人有帮助,但这就是我设法在我的 nodejs 应用程序中访问我的 Secrets 的方式!
我发了一个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
:
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
文件:
apiVersion: v1
kind: ConfigMap
metadata:
name: environmental-variables
data:
.env: |
USERNAME: <username>
PASSWORD: <password>
HOST: <host>
任何帮助将不胜感激!此外,我正在尝试将我的环境变量设置为 Secrets,因为我不想公开我的任何变量,因为它包含敏感信息。我正在尝试使用 Express 在 Node.js 应用程序上执行此操作。谢谢!
编辑:这是 Secrets 部分在我的 yaml 文件中的样子
secrets:
- name: environmental-variables
key: USERNAME
- name: environmental-variables
key: PASSWORD
我的 Secrets yaml 文件的样子:
kind: Secret
apiVersion: v1
metadata:
name: environmental-variables
namespace: tda-dev-duck-dev
data:
USERNAME: <username>
PASSWORD: <password>
您创建了 ConfigMap
并试图从秘密中获取价值。如果你想从 configmap 中设置值,那么像下面这样更新 env
env:
- name: USERNAME
valueFrom:
configMapKeyRef:
name: environmental-variables # this is ConfigMap Name
key: USERNAME # this is key in ConfigMap
- name: PASSWORD
valueFrom:
configMapKeyRef:
name: environmental-variables
key: PASSWORD
- name: HOST
valueFrom:
configMapKeyRef:
name: environmental-variables
key: HOST
并像下面这样更新 configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: environmental-variables
data:
USERNAME: <username>
PASSWORD: <password>
HOST: <host>
要了解如何使用 ConfigMap 数据定义容器环境变量,请单击 here
如果您想将机密用作环境变量,请检查here
经过几天弄清楚如何将 Secrets 用作环境变量后,我弄清楚了如何在我的 nodejs 应用程序中引用它!
在我以正常方式调用环境变量之前,process.env.VARIABLE_NAME
,但是当我将 Secrets 作为环境变量时,这对我不起作用。为了获得变量的值,我必须在 Javascript 文件中执行 process.env.ENVIRONMENTAL_VARIABLES_USERNAME
,这对我有用!其中 ENVIRONMENTAL_VARIABLES
是 name
而 USERNAME
是关键!
不确定这是否对其他人有帮助,但这就是我设法在我的 nodejs 应用程序中访问我的 Secrets 的方式!