我正在尝试运行一个使用docker
和kubernetes
的项目。当我尝试运行应用程序时,它会给我以下错误:
> auth@1.0.0 start
> ts-node-dev src/index.ts
[INFO] 11:03:41 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.8.2, typescript ver. 4.7.4)
Error: JWT_KEY must be defined
at E:advshop_backauthsrcindex.ts:7:11
at Generator.next (<anonymous>)
at E:advshop_backauthsrcindex.ts:8:71
at new Promise (<anonymous>)
at __awaiter (E:advshop_backauthsrcindex.ts:4:12)
at start (E:advshop_backauthsrcindex.ts:5:26)
at Object.<anonymous> (E:advshop_backauthsrcindex.ts:25:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module._compile (E:advshop_backauthnode_modulessource-map-supportsource-map-support.js:521:25)
at Module.m._compile (C:UsersAAppDataLocalTempts-node-dev-hook-2465378030590002.js:69:33)
[ERROR] 11:03:48 Error: JWT_KEY must be defined
我尝试使用以下命令创建JWT_KEY
:
kubectl create secret generic jwt-secret --from-literal JWT_KEY=this_is_the_jwt_key
但它的反应是一个新的错误:
error: failed to create secret secrets "jwt-secret" already exists
我记得我以前使用过kubectl create secret ...
命令来测试另一个项目(尽管在=
之后使用了不同的表达式,如JWT_KEY=asdf
(。
我不知道这些钥匙存放在哪里?如果已经存储了JWT_KEY
,为什么新程序不起作用?
附言:我使用Windows 10机器和Docker Desktop应用程序。
这是我的index.js
文件:
import mongoose from "mongoose";
import { app } from "./app";
const start = async () => {
if (!process.env.JWT_KEY) {
throw new Error("JWT_KEY must be defined");
}
if (!process.env.MONGO_URI) {
throw new Error("MONGO_URI must be defined");
}
try {
await mongoose.connect(process.env.MONGO_URI, {});
console.log("Connected to MongoDb");
} catch (err) {
console.error(err);
}
app.listen(3000, () => {
console.log("Listening on port 3000!!!!!!!!");
});
};
start();
当我运行kubectl get secrets
时,我得到:
kubectl get secrets
NAME TYPE DATA AGE
jwt-secret Opaque 1 38d
这是deployment.yaml
文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: auth-depl
spec:
replicas: 1
selector:
matchLabels:
app: auth
template:
metadata:
labels:
app: auth
spec:
containers:
- name: auth
image: tester/auth
env:
- name: MONGO_URI
value: 'mongodb://auth-mongo-srv:27017/auth'
- name: JWT_KEY
valueFrom:
secretKeyRef:
name: jwt-secret
key: JWT_KEY
---
apiVersion: v1
kind: Service
metadata:
name: auth-srv
spec:
selector:
app: auth
ports:
- name: auth
protocol: TCP
port: 3000
targetPort: 3000
您可以使用以下命令删除密钥:kubectl删除秘密jwt秘密
然后您可以再次创建kubectl创建机密通用jwt机密——来自文字jwt_KEY=asdf