如何读取Kubernetes以Volume挂载的Secret并注入Spring Boot的application.properties
你可以直接使用Spring Boot原生提供的文件配置值读取能力实现需求:
2.4及以上主流版本配置
直接使用${file:文件绝对路径}的语法,即可读取对应文件内容作为配置值,修改后配置如下:
spring.datasource.url=jdbc:sqlserver://mycompany.com;databaseName=My_DB # 读取/etc/confidential/username.txt内容作为用户名 spring.datasource.username=${file:/etc/confidential/username.txt} # 读取/etc/confidential/password.txt内容作为密码 spring.datasource.password=${file:/etc/confidential/password.txt} spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
低于2.4版本适配方案
如果你的Spring Boot版本低于2.4,没有内置的文件配置读取能力,可以在容器启动命令中添加参数读取文件内容注入配置:
java -Dspring.datasource.username=$(cat /etc/confidential/username.txt) -Dspring.datasource.password=$(cat /etc/confidential/password.txt) -jar data-api-0.0.1.jar
你需要把上述启动命令配置到Pod的command字段中即可生效。
内容的提问来源于stack exchange,提问作者user3058642
相关产品推荐
相关产品推荐

