如何从作为文件安装的 kubernetes Secrets 填充 application.properties 文件值

Sti*_*ing 6 spring-boot kubernetes

我正在研究 Springboot 和 Kubernetes,并且我有一个连接到 Postgres 数据库的非常简单的应用程序。我想从 configmap 中获取数据源的值,从机密中获取密码作为挂载文件。

配置映射文件:

apiVersion: v1
kind: ConfigMap
metadata:
  name: customer-config
data:
  application.properties: |
    server.forward-headers-strategy=framework
    spring.datasource.url=jdbc:postgresql://test/customer
    spring.datasource.username=postgres
Run Code Online (Sandbox Code Playgroud)

秘密档案:

apiVersion: v1
kind: Secret
metadata:
  name: secret-demo
data:
  spring.datasource.password: cG9zdGdyZXM=
Run Code Online (Sandbox Code Playgroud)

部署文件:

spec:
  containers:
    - name: customerc
      image: localhost:8080/customer
      imagePullPolicy: IfNotPresent
      ports:
        - containerPort: 8282
      volumeMounts:
        - mountPath: /workspace/config/default
          name: config-volume
        - mountPath: /workspace/secret/default
          name: secret-volume
  volumes:
    - name: config-volume
      configMap:
        name: customer-config
    - name: secret-volume
      secret:
        secretName: secret-demo
        items:
          - key: spring.datasource.password
            path: password
Run Code Online (Sandbox Code Playgroud)

如果我将 spring.datasource.password prop 从 Secret 移动到 configmap 那么它可以正常工作,或者如果我将其值填充为 env 变量,那么它也可以正常工作。但据我们所知,这两种方法都不是安全的,有人可以告诉我秘密文件安装有什么问题吗?

And*_*son 5

Spring Boot 2.4 添加了对导入配置树的支持。此支持可用于使用 Kubernetes 挂载的卷中的配置。

\n

例如,让\xe2\x80\x99s 假设 Kubernetes 已安装以下卷:

\n
etc/\n  config/\n    myapp/\n      username\n      password\n
Run Code Online (Sandbox Code Playgroud)\n

用户名文件的内容将是一个配置值,密码的内容将是一个秘密。

\n

要导入这些属性,您可以将以下内容添加到 application.properties 文件中:

\n
spring.config.import=optional:configtree:/etc/config/\n
Run Code Online (Sandbox Code Playgroud)\n

这将导致属性myapp.username被myapp.password设置。/etc/config/myapp/username它们的值将分别是和的内容/etc/config/myapp/password。

\n


小智 1

默认情况下,出于安全原因,不会启用通过 API 消费机密。Spring Cloud Kubernetes 需要访问 Kubernetes API,以便能够检索为单个服务运行的 pod 地址列表。使用 Minikube 时最简单的方法是使用集群管理员权限创建默认 ClusterRoleBinding。

有关如何创建一个的示例:-

$ kubectl 创建集群角色绑定管理 --clusterrole=集群管理 --serviceaccount=default:default