Nuxt 3/Docker/Kubernetes 环境变量

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

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 || &#39;&lt;url&gt;&#39;,
            communityId: process.env.NUXT_ENV_COMMUNITY_ID || &lt;id&gt;,
        }
    },

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: &lt;app name&gt;-deployment
  namespace: default
  labels:
    app: &lt;app name&gt;
spec:
  replicas: 2
  selector:
    matchLabels:
      app: &lt;app name&gt;
  template:
    metadata:
      labels:
        app: &lt;app name&gt;
    spec:
      imagePullSecrets:
        - name: regcred
      containers:
        - name: &lt;container name&gt;
          image: &lt;my-image&gt;
          ports:
            - containerPort: 3000
          envFrom:
            - configMapRef:
                name: &lt;app name&gt;-config

Configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: &lt;app name&gt;-config
  namespace: default
data:
  NUXT_PUBLIC_BASE_URL: &quot;&lt;url&gt;&quot;
  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:

确定