【问题标题】:k8s read secert from nodejs applicationk8s 从节点 js 应用程序读取秘密
【发布时间】:2021-07-31 09:24:59
【问题描述】:

我有一个 nodejs 应用程序需要在 RT 中读取密钥

这就是秘密

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

我使用了一个卷来挂载密钥,因为我需要读取许多字段并且我不想使用 var 选项。

我已将卷添加到部署中,如下所示:

          volumeMounts:
            - name: secret-volume
              mountPath: /etc/secret-volume
      volumes:
        - name: secret-volume
          secret:
            secretName: secert1

我的问题是我应该如何从 nodejs 应用程序访问密钥?

我尝试了以下方法,但没有得到任何数据,有什么想法吗?

const fs = require('fs');
fs.readFile('/etc/secret-volume', function read(err, data) {
    if (err) {
        throw err;
    }
    const content = data;


});


【问题讨论】:

  • 每个data 属性都是一个文件:/etc/secret-volume/TOKEN1
  • @Matt - 它有效,请将其写为答案
  • @Matt - btw ,如果我来自 TOKEN1 - TOKEN20 ,我应该如何阅读它们,因为每个属性都是一个文件?

标签: javascript node.js azure kubernetes google-cloud-platform


【解决方案1】:

秘密中的每个data 项目将成为基于秘密卷的mountPath 中的一个文件。

要读取大量令牌,您可以使用 readdirreadFile 搜索目录

const fsp = require('fs').promises
const path = require('path')

async function readTokens(token_path) {
  const tokens = {}
  const entries = await fsp.readdir(token_path, { withFileTypes: true })
  for (const entry of entries) {
    if (!entry.isFile()) continue
    const buf = await fsp.readFile(path.join(token_path, entry.name), 'utf8')
    tokens[file] = buf.toString()
  }
  return tokens
}

readTokens('/etc/secret-volume').then(console.log).catch(console.err)

【讨论】:

  • 谢谢,这种方法比使用env 更好吗?使用secretKeyRef 并在其中放置 20 个属性?您的建议是什么,更好的方法是什么?
  • 真的不能说哪个更好。环境变量可能更容易在节点中访问。无论哪种方式部署都非常相似¯\_(ツ)_/¯
  • 谢谢,我尝试了代码,但它不起作用,出错了(node:1) UnhandledPromiseRejectionWarning: Error: EISDIR: illegal operation on a directory, read (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict`(见nodejs.org/api/cli.html#cli_unhan`有什么想法吗?
  • 有什么想法吗?提示?
  • 它是否试图读取目录中的文件?也许首先测试条目是否是文件
【解决方案2】:

您可以阅读如下。TOKEN1 是来自秘密secert1 的密钥

var token1_value = fs.readFileSync("/etc/secret-volume/TOKEN1", 'utf8');

【讨论】:

  • 谢谢,如果我有多个 TOKEN,比如TOKEN1- TOKEN20,如果每个令牌都映射到一个文件,我应该如何处理?
  • 你有什么建议在这种情况下该怎么做,使用fs.read 20次(读取20个文件)我认为这是个坏主意,你怎么看?
【解决方案3】:

我通常以这种方式将秘密设置为 K8s 中的环境变量:

          env: 
            - 
              name: MY_SECRET_VARIABLE
              valueFrom:
                secretKeyRef:
                  name: secert1
                  key: MY_SECRET_VARIABLE

然后在您的代码中,只需使用process.env.MY_SECRET_VARIABLE 即可访问它。

您可以在此处查看有关如何执行此操作的更多详细信息: https://medium.com/faun/using-kubernetes-secrets-as-environment-variables-5ea3ef7581ef

【讨论】:

  • 谢谢,我明白了,但是我想避免这种情况,因为我有很多领域,我想避免它并使用音量,你能采纳你对音量使用的回答吗?
【解决方案4】:

我找到了一个非常有用的解决方案,我遇到了同样的问题,我已经这样解决了

 apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: insertmendoza
  name: sarys-authentications
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sarys-authentications
  template:
    metadata:
      labels:
        app: sarys-authentications
    spec:
      containers:
        - name: sarys-authentications
          image: 192.168.88.246:32000/sarys:authentications
          imagePullPolicy: Always
          resources:
            limits:
              memory: "500Mi"
              cpu: "50m"
          ports:
            - containerPort: 8000

          envFrom:
            - configMapRef:
                name: authentications-config

            - secretRef: <<-- add
                name: authentications-sercret <<-- add

          volumeMounts:
            - name: config-volumen
              mountPath: /etc/config/
              readOnly: true

            - name: secret-volumen
              mountPath: /etc/secret/
              readOnly: true

      volumes:
        - name: config-volumen
          configMap:
            name: authentications-config

        - name: secret-volumen
          secret:
            secretName: authentications-sercret

【讨论】:

    猜你喜欢
    • 2021-10-26
    • 2021-02-06
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2022-11-09
    • 2023-03-11
    • 2011-02-28
    • 2021-10-06
    相关资源
    最近更新 更多