简体   繁体   中英

use k8s secret from nodejs application

I've created the following sample program which I need to create secret values

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

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


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?

The name is the key for the env var so with what you have, that should be process.env.myenv . You probably instead want to use the envFrom option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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