首页 > 解决方案 > 从 Spring mvc 应用程序的已安装卷中读取秘密值

问题描述

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

大多数示例仅指向 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 应用程序中读取其值的方法吗?

标签: azurespring-mvckuberneteskubernetes-secretsmounted-volumes

解决方案


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

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

然后就可以看到path 中名为usernameand的文件了,其值如下所示:password/etc/app/secrets

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

推荐阅读