【问题标题】:Read secret values from mounted volumes from Spring mvc application从 Spring mvc 应用程序的已安装卷中读取秘密值
【发布时间】:2021-06-01 02:46:59
【问题描述】:

我们在 AKS 中使用 tomcat 映像部署了一个 Spring MVC 应用程序。 如何从挂载为卷的 Secrets 中获取值?

大多数示例仅指向 Spring Boot

我正在从秘密存储中加载值

kind: Pod
apiVersion: v1
metadata:
  name: nginx
  namespace: default
  labels:
    aadpodidbinding: pod-mi
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: foo
          mountPath: "/mnt/secrets"
          readOnly: true
  volumes:
    - name: foo
      csi:
        driver: secrets-store.csi.k8s.io
        readOnly: true
        volumeAttributes:
          secretProviderClass: spc.

可以看到秘密被正确挂载:

kubectl -n default exec -it nginx -- bash
root@nginx:/# ls /mnt/secrets
service-one-secret
service-two-secret

Cat service-one-secret 不返回任何内容

谁能建议一种从 spring mvc 应用程序中读取其值的方法?

【问题讨论】:

    标签: azure spring-mvc kubernetes kubernetes-secrets mounted-volumes


    【解决方案1】:

    当您将机密作为卷挂载到容器时,它将显示该路径中机密的数据。例如,您使用以下命令创建一个秘密:

    kubectl create secret generic basic-secret \
      --from-literal=username="jsmith" \
      --from-literal=password="mysupersecurepassword"
    

    然后你将秘密挂载为一个卷:

    ...
    spec:
      volumes:
      - name: vol-secret
        secret:
          secretName: my-secret
      containers:
      ...
        volumeMounts:
        - name: vol-secret
          mountPath: /etc/app/secrets
    

    然后在/etc/app/secrets路径下可以看到名为usernamepassword的文件,值如下:

    / # ls /etc/app/secrets
    password  user
    / # cat /etc/app/secrets/password
    mysupersecurepassword
    / # cat /etc/app/secrets/username
    jsmith
    

    【讨论】:

    • 我用更多细节编辑了我的问题。你能建议我,如何从 spring mvc 应用程序中读取
    • @Aneena 正如我向您展示的那样,将密钥挂载为卷,然后使用您的代码读取文件。没有什么难的。
    • @Aneena 这个问题有什么更新吗?它解决了你的问题吗?
    • @Aneena 我等了很久 :-)
    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    相关资源
    最近更新 更多