Nuxt 3/Docker/Kubernetes 环境变量

huangapple go评论98阅读模式
英文:

Nuxt 3/Docker/Kubernetes Environment Variables

问题

我有一个新的Nuxt 3应用程序。在我的config.ts文件中,我定义了以下变量:

  1. runtimeConfig: {
  2. // 仅服务器上有的私钥
  3. // 提供给客户端的公钥
  4. public: {
  5. baseUrl: process.env.NUXT_PUBLIC_BASE_URL || '<url>',
  6. communityId: process.env.NUXT_ENV_COMMUNITY_ID || <id>,
  7. }
  8. },

我将此应用程序构建成Docker镜像,然后部署到Kubernetes作为一个Pod/Deployment,同时还有一个ConfigMap,其中包含我之前定义的这两个变量。

当我SSH进入Pod并运行echo $NUXT_ENV_COMMUNITY_ID时,我可以看到我在ConfigMap中设置的ID,基本URL也是如此,但是当我尝试在Nuxt中访问这些变量时,它们仍然使用它们的默认选项。如果我移除默认值,应用程序会崩溃/返回500错误,因为它们未定义。

有什么建议吗?

--编辑的YAML--

Deployment

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: <app name>-deployment
  5. namespace: default
  6. labels:
  7. app: <app name>
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. app: <app name>
  13. template:
  14. metadata:
  15. labels:
  16. app: <app name>
  17. spec:
  18. imagePullSecrets:
  19. - name: regcred
  20. containers:
  21. - name: <container name>
  22. image: <my-image>
  23. ports:
  24. - containerPort: 3000
  25. envFrom:
  26. - configMapRef:
  27. name: <app name>-config

ConfigMap

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: <app name>-config
  5. namespace: default
  6. data:
  7. NUXT_PUBLIC_BASE_URL: "<url>"
  8. NUXT_ENV_COMMUNITY_ID: "2"
英文:

I have a new Nuxt 3 application. In my config.ts, I have the following variables defined:

  1. runtimeConfig: {
  2. // Private keys are only available on the server
  3. // Public keys that are exposed to the client
  4. public: {
  5. baseUrl: process.env.NUXT_PUBLIC_BASE_URL || &#39;&lt;url&gt;&#39;,
  6. communityId: process.env.NUXT_ENV_COMMUNITY_ID || &lt;id&gt;,
  7. }
  8. },

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

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: &lt;app name&gt;-deployment
  5. namespace: default
  6. labels:
  7. app: &lt;app name&gt;
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. app: &lt;app name&gt;
  13. template:
  14. metadata:
  15. labels:
  16. app: &lt;app name&gt;
  17. spec:
  18. imagePullSecrets:
  19. - name: regcred
  20. containers:
  21. - name: &lt;container name&gt;
  22. image: &lt;my-image&gt;
  23. ports:
  24. - containerPort: 3000
  25. envFrom:
  26. - configMapRef:
  27. name: &lt;app name&gt;-config

Configmap

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: &lt;app name&gt;-config
  5. namespace: default
  6. data:
  7. NUXT_PUBLIC_BASE_URL: &quot;&lt;url&gt;&quot;
  8. NUXT_ENV_COMMUNITY_ID: &quot;2&quot;

答案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.

huangapple
  • 本文由 发表于 2023年3月3日 22:10:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定