此版本仍在开发中,目前尚不稳定。如需最新稳定版本,请使用 Spring Cloud Kubernetes 5.0.1spring-doc.cadn.net.cn

Secrets PropertySource

Kubernetes 具有 Secrets(密钥)的概念,用于存储敏感数据,例如密码、OAuth Tokens等。本项目提供了与 Secrets 的集成,以便让 Spring Boot 应用程序能够访问这些密钥。您可以通过设置 spring.cloud.kubernetes.secrets.enabled 属性来显式启用或禁用此功能。spring-doc.cadn.net.cn

启用后,Fabric8SecretsPropertySource 会从以下来源查找 Kubernetes 中的 Secretsspring-doc.cadn.net.cn

  1. 递归读取密钥挂载spring-doc.cadn.net.cn

  2. 以应用程序命名(如 spring.application.name 所定义)spring-doc.cadn.net.cn

  3. 匹配某些标签spring-doc.cadn.net.cn

默认情况下,出于安全考虑,通过 API 消费密钥(上述第 2 和第 3 点)未启用。对密钥的 'list' 权限允许客户端在指定命名空间中查看密钥值。此外,我们建议通过挂载卷的方式让容器共享密钥。spring-doc.cadn.net.cn

如果您启用了通过 API 消费密钥,我们建议您通过授权策略(如 RBAC)来限制对密钥的访问。有关通过 API 消费密钥时的风险和最佳实践的更多信息,请参阅 本文档spring-doc.cadn.net.cn

如果找到密钥,其数据将可供应用程序使用。spring-doc.cadn.net.cn

假设我们有一个名为 demo 的 Spring Boot 应用程序,它使用属性来读取数据库配置。我们可以使用以下命令创建一个 Kubernetes 密钥:spring-doc.cadn.net.cn

kubectl create secret generic db-secret --from-literal=username=user --from-literal=password=p455w0rd

前面的命令将创建以下密钥(您可以通过使用 kubectl get secrets db-secret -o yaml 查看):spring-doc.cadn.net.cn

apiVersion: v1
data:
  password: cDQ1NXcwcmQ=
  username: dXNlcg==
kind: Secret
metadata:
  creationTimestamp: 2017-07-04T09:15:57Z
  name: db-secret
  namespace: default
  resourceVersion: "357496"
  selfLink: /api/v1/namespaces/default/secrets/db-secret
  uid: 63c89263-6099-11e7-b3da-76d6186905a8
type: Opaque

注意,数据包含由 create 命令提供的字面量的 Base64 编码版本。spring-doc.cadn.net.cn

您的应用随后可以使用此密钥——例如,通过将密钥的值导出为环境变量:spring-doc.cadn.net.cn

apiVersion: v1
kind: Deployment
metadata:
  name: ${project.artifactId}
spec:
   template:
     spec:
       containers:
         - env:
            - name: DB_USERNAME
              valueFrom:
                 secretKeyRef:
                   name: db-secret
                   key: username
            - name: DB_PASSWORD
              valueFrom:
                 secretKeyRef:
                   name: db-secret
                   key: password

您可以以多种方式选择要使用的密钥:spring-doc.cadn.net.cn

  1. 通过设置命名的密钥:spring-doc.cadn.net.cn

    -Dspring.cloud.kubernetes.secrets.name=db-secret
  2. 通过定义一个标签列表:<br />spring-doc.cadn.net.cn

    -Dspring.cloud.kubernetes.secrets.labels.broker=activemq
    -Dspring.cloud.kubernetes.secrets.labels.db=postgresql

就像对于ConfigMap一样,还可以使用多个Secret实例进行更高级的配置。该spring.cloud.kubernetes.secrets.sources列表使其成为可能。 例如,您可以定义以下Secret实例:spring-doc.cadn.net.cn

spring:
  application:
    name: cloud-k8s-app
  cloud:
    kubernetes:
      secrets:
        name: default-name
        namespace: default-namespace
        sources:
         # Spring Cloud Kubernetes looks up a Secret named s1 in namespace default-namespace
         - name: s1
         # Spring Cloud Kubernetes looks up a Secret named default-name in namespace n2
         - namespace: n2
         # Spring Cloud Kubernetes looks up a Secret named s3 in namespace n3
         - namespace: n3
           name: s3

在前面的示例中,如果未设置spring.cloud.kubernetes.secrets.namespace,则会根据命名空间查找名为s1Secret。要了解如何解决应用程序的命名空间,请参阅命名空间解析获取更多信息。spring-doc.cadn.net.cn

ConfigMaps相似;如果您希望在无法加载Secrets属性源时,您的应用程序无法启动,可以设置spring.cloud.kubernetes.secrets.fail-fast=true属性。.spring-doc.cadn.net.cn

还可以为 0 个属性源启用重试,如 ConfigMaps。 与属性源一样,首先需要设置 spring.cloud.kubernetes.secrets.fail-fast=true。 然后需要将 spring-retryspring-boot-starter-aspectj 添加到 classpath 中。 可通过设置 9 来配置 8 属性源的重试行为spring-doc.cadn.net.cn

如果因为某些原因已经在类路径上拥有spring-retryspring-boot-starter-aspectj,但想要启用快速失败,却不希望启用重试;可以对SecretsPropertySources进行禁用重试,通过设置spring.cloud.kubernetes.secrets.retry.enabled=false

由于从 Secrets 中获取的数据通常被视为敏感数据,因此 actuator 端点 /env/configprops 可以设置为对数据进行清理,以便它们不以明文形式显示。为此,需要设置:spring-doc.cadn.net.cn

spring.cloud.kubernetes.sanitize.secrets=true

这个设置从 3.0.6 版本及以上开始支持。spring-doc.cadn.net.cn

<p>Table 1. 属性:</p>
姓名 类型 默认 描述

spring.cloud.kubernetes.secrets.enabledspring-doc.cadn.net.cn

Booleanspring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

禁用凭据PropertySourcespring-doc.cadn.net.cn

spring.cloud.kubernetes.secrets.namespring-doc.cadn.net.cn

Stringspring-doc.cadn.net.cn

${spring.application.name}spring-doc.cadn.net.cn

设置要查找的秘密的名称spring-doc.cadn.net.cn

spring.cloud.kubernetes.secrets.namespacespring-doc.cadn.net.cn

Stringspring-doc.cadn.net.cn

Client 命名空间spring-doc.cadn.net.cn

设置要在其中查找的 Kubernetes 命名空间spring-doc.cadn.net.cn

spring.cloud.kubernetes.secrets.labelsspring-doc.cadn.net.cn

Mapspring-doc.cadn.net.cn

nullspring-doc.cadn.net.cn

设置用于查找机密的标签spring-doc.cadn.net.cn

spring.cloud.kubernetes.secrets.fail-fastspring-doc.cadn.net.cn

Booleanspring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

设置在配置类上,以编程方式控制是否在从注册表加载时使应用程序启动失败。如果未设置,则默认值为抛出异常,除非有其他特殊规则。spring-doc.cadn.net.cn

spring.cloud.kubernetes.secrets.retry.enabledspring-doc.cadn.net.cn

Booleanspring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

启用或禁用Secrets重试。spring-doc.cadn.net.cn

spring.cloud.kubernetes.secrets.retry.initial-intervalspring-doc.cadn.net.cn

Longspring-doc.cadn.net.cn

1000spring-doc.cadn.net.cn

初始重试间隔(毫秒数)。spring-doc.cadn.net.cn

spring.cloud.kubernetes.secrets.retry.max-attemptsspring-doc.cadn.net.cn

Integerspring-doc.cadn.net.cn

6spring-doc.cadn.net.cn

当前最大尝试次数。spring-doc.cadn.net.cn

spring.cloud.kubernetes.secrets.retry.max-intervalspring-doc.cadn.net.cn

Longspring-doc.cadn.net.cn

2000spring-doc.cadn.net.cn

最大回退间隔。spring-doc.cadn.net.cn

spring.cloud.kubernetes.secrets.retry.multiplierspring-doc.cadn.net.cn

Doublespring-doc.cadn.net.cn

1.1spring-doc.cadn.net.cn

乘数用于下一个间隔。spring-doc.cadn.net.cn

您可以在下面找到使用密钥(尽管尚未更新以使用新项目 0)的应用程序示例:spring-boot-camel-configspring-doc.cadn.net.cn