如何在 Spring Cloud Kubernetes 中使用多个秘密源

问题描述 投票:0回答:0

我的 Kubernetes 集群中有多个 Secret 资源,我想让我的 Spring Boot 应用程序使用它们,它使用 Spring Cloud Kubernetes。

我已经阅读了它的文档,但我仍然无法理解它。

我的 Secret 资源如下所示:

apiVersion: v1
kind: Secret
metadata:
  name: service1-secrets
  namespace: my-app-namespace
type: Opaque
data:
  config.service1.pass=<base64-encoded String>
  config.service1.user=<base64-encoded String>

对于我的应用程序需要的其他服务,我也有类似的 Secret 资源。

秘密部署在同一个命名空间和 ServiceAccount 中,我可以看到它们。

如何指定多个秘密来源? 我是否必须使用

bootstrap(-env).yaml
而不是属性文件?

在我的

bootstrap.properties
文件中(对于正确的环境,
-azure
),我有:

    spring.cloud.kubernetes.secrets.enabled=true
    spring.cloud.kubernetes.enabled=true
    spring.cloud.kubernetes.config.namespace=my-app-namespace
    spring.cloud.kubernetes.client.namespace=my-app-namespace
    spring.cloud.kubernetes.secrets.namespace=my-app-namespace
    spring.cloud.kubernetes.secrets.sources={labels={name=service1-secrets}}, {name=service2-secrets}

不幸的是,在启动 pod 时,出现以下错误:

{"TIMESTAMP":"2023-03-26 03:34:35,697","SEVERITY":"WARN","APPLICATION":"springAppName_IS_UNDEFINED","CLASS":"o.s.c.a.AnnotationConfigApplicationContext","MESSAGE":"Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'secretsPropertySourceLocator' defined in class path resource [org/springframework/cloud/kubernetes/client/config/KubernetesClientBootstrapConfiguration.class]: Unsatisfied dependency expressed through method 'secretsPropertySourceLocator' parameter 0; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'spring.cloud.kubernetes.secrets-org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties': Could not bind properties to 'SecretsConfigProperties' : prefix=spring.cloud.kubernetes.secrets, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.cloud.kubernetes.secrets.sources' to java.util.List<org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties$Source>",...}

其次是:

{"TIMESTAMP":"2023-03-26 03:34:35,994","SEVERITY":"ERROR","APPLICATION":"springAppName_IS_UNDEFINED","CLASS":"o.s.b.d.LoggingFailureAnalysisReporter","MESSAGE":"\n\n***************************\nAPPLICATION FAILED TO START\n***************************\n\nDescription:\n\nFailed to bind properties under 'spring.cloud.kubernetes.secrets.sources' to java.util.List<org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties$Source>:\n\n    Property: spring.cloud.kubernetes.secrets.sources\n    Value: \"{labels={name=service1-secrets}}, {name=service2-secrets}\"\n    Origin: class path resource [config/bootstrap-azure.properties] - 6:41\n    Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.List<org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties$Source>]\n\nAction:\n\nUpdate your application's configuration\n",...}

请注意:我目前没有在我的

deployment.yaml
文件中使用 volumeMounts 或环境变量来传递秘密。据我了解,启用 Spring Cloud Kubernetes 的应用程序可以通过 API 在实际 Kubernetes 集群上的给定命名空间中“发现”或“查找”秘密。我知道这很不安全(或气馁),但我暂时不在乎。

spring-cloud spring-cloud-kubernetes
© www.soinside.com 2019 - 2024. All rights reserved.