英文:
Nuxt 3/Docker/Kubernetes Environment Variables
问题
我有一个新的Nuxt 3应用程序。在我的config.ts文件中,我定义了以下变量:
runtimeConfig: {
// 仅服务器上有的私钥
// 提供给客户端的公钥
public: {
baseUrl: process.env.NUXT_PUBLIC_BASE_URL || '<url>',
communityId: process.env.NUXT_ENV_COMMUNITY_ID || <id>,
}
},
我将此应用程序构建成Docker镜像,然后部署到Kubernetes作为一个Pod/Deployment,同时还有一个ConfigMap,其中包含我之前定义的这两个变量。
当我SSH进入Pod并运行echo $NUXT_ENV_COMMUNITY_ID
时,我可以看到我在ConfigMap中设置的ID,基本URL也是如此,但是当我尝试在Nuxt中访问这些变量时,它们仍然使用它们的默认选项。如果我移除默认值,应用程序会崩溃/返回500错误,因为它们未定义。
有什么建议吗?
--编辑的YAML--
Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: <app name>-deployment
namespace: default
labels:
app: <app name>
spec:
replicas: 2
selector:
matchLabels:
app: <app name>
template:
metadata:
labels:
app: <app name>
spec:
imagePullSecrets:
- name: regcred
containers:
- name: <container name>
image: <my-image>
ports:
- containerPort: 3000
envFrom:
- configMapRef:
name: <app name>-config
ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: <app name>-config
namespace: default
data:
NUXT_PUBLIC_BASE_URL: "<url>"
NUXT_ENV_COMMUNITY_ID: "2"
英文:
I have a new Nuxt 3 application. In my config.ts, I have the following variables defined:
runtimeConfig: {
// Private keys are only available on the server
// Public keys that are exposed to the client
public: {
baseUrl: process.env.NUXT_PUBLIC_BASE_URL || '<url>',
communityId: process.env.NUXT_ENV_COMMUNITY_ID || <id>,
}
},
I have this application built to a docker image, then deployed to Kubernetes as a pod/deployment, alongside a configmap which holds the 2 variables I defined earlier.
When I SSH into the pod, and run echo $NUXT_ENV_COMMUNITY_ID
, I can see the ID I set in the configmap, and same story with the base url, however when I try to access the variables in Nuxt, they default to their default options. If I remove the defaults, the app crashes/returns a 500 error since they are not defined.
Any suggestions?
--EDIT YAML--
Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: <app name>-deployment
namespace: default
labels:
app: <app name>
spec:
replicas: 2
selector:
matchLabels:
app: <app name>
template:
metadata:
labels:
app: <app name>
spec:
imagePullSecrets:
- name: regcred
containers:
- name: <container name>
image: <my-image>
ports:
- containerPort: 3000
envFrom:
- configMapRef:
name: <app name>-config
Configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: <app name>-config
namespace: default
data:
NUXT_PUBLIC_BASE_URL: "<url>"
NUXT_ENV_COMMUNITY_ID: "2"
答案1
得分: 1
请查看 https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables。
您可以将您的变量设置为空字符串在 runtimeConfig.public
中。
我还注意到第二个中没有 _PUBLIC_
,可能是问题所在。
英文:
See https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables.
You can set your variables as empty strings in runtimeConfig.public
.
I also notice there's no _PUBLIC_
on the second one, might be the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论