kubernetes - Next.js 环境变量在本地工作,但在 Kubernetes 上托管时不工作
问题描述
有一个 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
希望这可以帮助。
推荐阅读
- apache - 一个组件的 Apache Nifi 历史统计
- python - 如何在数组中找到两个相似数字的索引?
- mvvm - 如何将弹出页面按钮绑定到 xamarin 表单中的 viewmodel 命令属性
- hibernate - 如何使用 Envers 审计查询获取多对多连接表的审计修订
- android - How to use model in query string for Retrofit 2?
- javascript - ng-repeat 不重复特定元素
- bootstrap-tags-input - 如何在标签输入中添加弹出框工具提示?
- c++ - poco ping线程安全吗
- stackexchange - Stack Overflow 在哪里存储 GitHub 个人资料链接?
- c++ - 在托管和非托管代码中使用事件