gpt4 book ai didi

kubernetes - Next.js环境变量在本地工作,但在Kubernetes上托管时不工作

转载 作者:行者123 更新时间:2023-12-02 11:36:50 27 4
gpt4 key购买 nike

有一个Next.js项目。

这是我的next.config.js文件,我在本指南中进行了后续操作:https://dev.to/tesh254/environment-variables-from-env-file-in-nextjs-570b

module.exports = withCSS(withSass({
webpack: (config) => {
config.plugins = config.plugins || []
config.module.rules.push({
test: /\.svg$/,
use: ['@svgr/webpack', {
loader: 'url-loader',
options: {
limit: 100000,
name: '[name].[ext]'
}}],
});

config.plugins = [
...config.plugins,

// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true
})
]

const env = Object.keys(process.env).reduce((acc, curr) => {
acc[`process.env.${curr}`] = JSON.stringify(process.env[curr]);
return acc;
}, {});
// Fixes npm packages that depend on `fs` module
config.node = {
fs: 'empty'
}

/** Allows you to create global constants which can be configured
* at compile time, which in our case is our environment variables
*/
config.plugins.push(new webpack.DefinePlugin(env));

return config
}
}),

);

我有一个.env文件,其中保存我需要的值。它在本地主机上运行时有效。

在我的Kubernetes环境中,在可以修改的部署文件中,我设置了相同的环境变量。但是,当我尝试识别它们时,它们以未定义的形式出现,因此我的应用程序无法运行。

我这样称呼它:
process.env.SOME_VARIABLE

在本地工作。

部署后,有没有人有过使环境变量在Next.js上起作用的经验?不像后端服务那样简单。 :(

编辑:
这就是环境变量部分的外观。

编辑2:
完整部署文件,已编辑以删除一些详细信息
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "38"
creationTimestamp: xx
generation: 40
labels:
app: appname
name: appname
namespace: development
resourceVersion: xx
selfLink: /apis/extensions/v1beta1/namespaces/development/deployments/appname
uid: xxx
spec:
progressDeadlineSeconds: xx
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: appname
tier: sometier
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: appname
tier: sometier
spec:
containers:
- env:
- name: NODE_ENV
value: development
- name: PORT
value: "3000"
- name: SOME_VAR
value: xxx
- name: SOME_VAR
value: xxxx
image: someimage
imagePullPolicy: Always
name: appname
readinessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 3000
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
resources:
requests:
cpu: 100m
memory: 100Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 1
conditions:
- lastTransitionTime: xxx
lastUpdateTime: xxxx
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
observedGeneration: 40
readyReplicas: 1
replicas: 1
updatedReplicas: 1

最佳答案

.env适用于docker或docker-compose,它们不适用于Kubernetes,如果要添加它们,可以通过configmaps对象或直接向每个部署添加示例(来自文档):

apiVersion: v1
kind: Pod
metadata:
name: envar-demo
labels:
purpose: demonstrate-envars
spec:
containers:
- name: envar-demo-container
image: gcr.io/google-samples/node-hello:1.0
env:
- name: DEMO_GREETING
value: "Hello from the environment"
- name: DEMO_FAREWELL
value: "Such a sweet sorrow

另外,最好的标准方法是使用配置映射,例如:
  containers:
- env:
- name: DB_DEFAULT_DATABASE
valueFrom:
configMapKeyRef:
key: DB_DEFAULT_DATABASE
name: darwined-env

和配置图:
apiVersion: v1
data:
DB_DEFAULT_DATABASE: darwined_darwin_dev_1
kind: ConfigMap
metadata:
creationTimestamp: null
labels:
io.kompose.service: darwin-env
name: darwined-env

希望这可以帮助。

关于kubernetes - Next.js环境变量在本地工作,但在Kubernetes上托管时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59051921/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com