使用ConfigMap实例的另一个选项是通过运行Spring Cloud Kubernetes应用程序并让Spring Cloud Kubornetes从文件系统中读取它们,将它们装载到Pod中。此举动由spring.cloud.kubernetes.config.paths属性控制。您可以在前面形貌的机制的基础上使用它,也可以使用它来代替前面介绍的机制。可以使用、分隔符在spring.cloud.kubernetes.config.path中指定多个(正确的)文件路径。
NameTypeDefaultDescriptionspring.cloud.kubernetes.config.enabled
Boolean
true
Enable Secrets PropertySource
spring.cloud.kubernetes.config.name
String
${spring.application.name}
Sets the name of ConfigMap to look up
spring.cloud.kubernetes.config.namespace
String
Client namespace
Sets the Kubernetes namespace where to lookup
spring.cloud.kubernetes.config.paths
List
null
Sets the paths where ConfigMap instances are mounted
spring.cloud.kubernetes.config.enableApi
Boolean
true
Enable or disable consuming ConfigMap instances through APIs
Kubernetes有Secrets的概念,用于存储密码、OAuth令牌等敏感数据。该项目提供了与Secrets集成,使Spring Boot应用程序可以访问机密。您可以通过设置spring.cloud.kubernetes.secrets.enabled属性来显式启用或禁用此功能。
启用后,SecretsPropertySource从以下泉源查找Kubernetes的Secrets:
从秘密装载中递归读取
以应用程序命名(由spring.application.name定义)
匹配一些标签
请注意,默认情况下,由于安全原因,不会启用通过API(上面第2点和第3点)使用Secrets。此外,我们建议容器通过装入的卷共享机密。如果您启用通过API消耗机密,我们建议您使用[授权策略,如RBAC]来限定对机密的访问(https://kubernetes.io/docs/concepts/configuration/secret/#best-实践)。
如果发现了这些秘密,应用程序就可以使用它们的数据。
假设我们有一个名为demo的spring-boot应用程序,它使用属性读取其数据库配置。我们可以使用以下命令创建Kubernetes机密: