【问题标题】:read kubernetes secrets mounted as volume in application.properties spring boot application在 application.properties spring boot 应用程序中读取作为卷安装的 kubernetes 机密
【发布时间】:2021-10-30 09:28:23
【问题描述】:

我创建了 kubenertes 秘密

echo -n 'myusername' > username.txt
echo -n 'pa55word' > password.txt
kubectl create secret generic esb-database-secret-vol --from-file=username.txt --from-file=password.txt

我将 pod 清单创建为

spec:
  containers:
  - image: data-api-0.0.1.jar
    ports:
    - containerPort: 8080
    name: esb-data-api
    imagePullPolicy: Never
    resources: {}
    volumeMounts:
    - name: esb-secret-vol
      mountPath: "/etc/confidential"
      readOnly: true
  volumes:
  - name: esb-secret-vol
    secret:
      secretName: esb-database-secret-vol

我验证了 pod /etc/confidential 文件夹中现在可以使用机密

这些值如何在 application.properties 中可用,以便我可以获得数据库连接

spring.datasource.url=jdbc:sqlserver://mycompany.com;databaseName=My_DB
# need to read from /etc/confidential/username.txt
spring.datasource.username=??????
# need to read from /etc/confidential/password.txt  
spring.datasource.password=??????
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

【问题讨论】:

    标签: spring-boot kubernetes spring-cloud kubernetes-secrets


    【解决方案1】:

    您有多种选择:

    1. 提供已在正确写入的属性文件中的数据
    2. 使用 init 容器来使用机密数据并使用 shell 脚本创建属性文件

    第一种方法看起来像

    echo 'spring.datasource.username=myusername' >> app.properties
    echo 'spring.datasource.password=pa55word' >> app.properties
    kubectl create secret generic esb-database-secret-vol --from-file=app.properties
    

    您稍后将在容器中以application.properties 的形式安装秘密。

    【讨论】:

      猜你喜欢
      • 2020-03-29
      • 2018-02-19
      • 2020-09-12
      • 2021-06-01
      • 2021-12-07
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      相关资源
      最近更新 更多