我发布了一个与此类似的question,并尝试实现该问题的答案: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应用程序上执行此操作。谢谢!
编辑:这是我的Yaml文件中的“秘密”部分的样子
secrets:
- name: environmental-variables
key: USERNAME
- name: environmental-variables
key: PASSWORD
“我的秘密” yaml文件的外观如下:
kind: Secret
apiVersion: v1
metadata:
name: environmental-variables
namespace: tda-dev-duck-dev
data:
USERNAME: <username>
PASSWORD: <password>
答案 0 :(得分:0)
您创建了ConfigMap
,并试图从秘密中获取价值。如果您想从configmap中设置值,请按照以下说明更新环境
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
答案 1 :(得分:0)
经过数天的思考,如何将Secrets用作环境变量,我想出了如何在我的nodejs应用程序中引用它!
在我以通常的方式调用环境变量process.env.VARIABLE_NAME
之前,但是当我将Secrets作为环境变量使用时,这对我不起作用。为了获取变量的值,我必须在Javascript文件中执行process.env.ENVIRONMENTAL_VARIABLES_USERNAME
,这对我来说很有效!其中ENVIRONMENTAL_VARIABLES
是name
,而USERNAME
是密钥!
不确定这是否对其他人有帮助,但这是我设法在nodejs应用程序中访问我的Secrets的方法!