如何从 Kubernetes 读取 Secret 并在 Angular 应用程序 enviroment.ts 文件中使用

问题描述 投票:0回答:1

如何从 Kubernetes 读取 Secrets 并在 Angular 应用程序 enviroment.ts 文件中使用。

在Angular应用程序中,我有一个environment.ts文件来配置Oauth的“Clientsecrets”,而不是直接从这个environment.ts使用这个Clientsecrets值,我们需要从Storage下的Kubernetes机密中读取(我们过去从Rancher查看它) ).

我试过这个,

mySecret:process.env.MY_SECRET 但不起作用。这个“MY_SECRET”抛出错误

angular kubernetes
1个回答
0
投票

查看[文档]https://kubernetes.io/docs/tasks/configmap-secret/managing-secret-using-config-file/)和示例此处您可以使用您拥有的秘密和configmap作为工作负载的环境变量:

apiVersion: v1
kind: Secret
metadata:
  name: db-auth
  namespace: app1
type: kubernetes.io/basic-auth
stringData:
  username: admin
  password: admin
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: db-config
  namespace: app1
data:
  server.host: "10.10.10.245"
  server.port: "3660"
  db.name: web-application
---
apiVersion: v1
kind: Pod
metadata:
  name: web-app
  labels:
    app: web-app
spec:
  containers:
    - name: ubuntu-app
      image: ubuntu:latest
      command: ["/bin/sleep", "3650d"]
      ## Setup the volumeMounts
      volumeMounts:
      ## ConfigMap Mount
      - name: db-config
        mountPath: /etc/config
      ## Secret Mount
      - name: db-auth
        mountPath: /etc/authentication
      resources:
        requests:
          memory: "250Mi"
          cpu: "0.5"
          limits:
          memory: "1024Mi"
          cpu: "1"
  volumes:
    - name: db-config
      configMap:
        name: db-config
    - name: db-auth
      secret:
        secretName: db-auth
  restartPolicy: Always

您可以查看这篇文章以获取有关使用秘密中的密钥参考的更多示例

© www.soinside.com 2019 - 2024. All rights reserved.