简体   繁体   English

使用 nodejs 应用程序中的 k8s 密钥

[英]use k8s secret from nodejs application

I've created the following sample program which I need to create secret values我创建了以下示例程序,我需要创建秘密值

index.js index.js

const express = require("express");
const port = process.env.PORT || 3000;


app = express();
app.get('/', (req, res) => (
    res.send("hello from k8s"))
)

app.listen(3000, function () {
    console.log("my secret ---> ", process.env.TOKEN1)
    console.log("server is listen to port", port)
})

This is the secret.yaml这是秘密。yaml

apiVersion: v1
kind: Secret
metadata:
  name: secert1
  namespace: trail
type: Opaque
data:
  TOKEN1: cmVhbGx5X3NlY3JldF92YWx1ZTE=

and this is how I connected between of them这就是我在他们之间建立联系的方式

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1
  namespace: trail
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: app1
    spec:
      containers:
        - name: app1
          image: myimage
          imagePullPolicy: Always
          ports:
            - containerPort: 5000
          env:
            - name: myenv
              valueFrom:
                secretKeyRef:
                  name: secert1
                  key: TOKEN1



When deploying the program I see in k8s logs部署程序时我在 k8s 日志中看到


my secret --->  undefined
server is listen to port 5000

What am I missing here?我在这里想念什么? in addition assume that I've more than 20 properties which I need to read from my app, is there a better way or just map each of the key value in the secret?另外假设我需要从我的应用程序中读取 20 多个属性,是否有更好的方法或者只是 map 密钥中的每个键值?

The name is the key for the env var so with what you have, that should be process.env.myenv .name是 env var 的键,因此对于您所拥有的,应该是process.env.myenv You probably instead want to use the envFrom option.您可能希望使用envFrom选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM