【问题标题】:How to populate application.properties file value from kubernetes Secrets mounted as file如何从挂载为文件的 kubernetes Secrets 填充 application.properties 文件值
【发布时间】:2021-06-01 06:43:33
【问题描述】:

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

配置映射文件:

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

秘密文件:

apiVersion: v1
kind: Secret
metadata:
  name: secret-demo
data:
  spring.datasource.password: cG9zdGdyZXM=

部署文件:

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

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

【问题讨论】:

    标签: spring-boot kubernetes


    【解决方案1】:

    Spring Boot 2.4 添加了对importing a config tree 的支持。此支持可用于使用 Kubernetes 挂载的卷中的配置。

    例如,假设 Kubernetes 挂载了以下卷:

    etc/
      config/
        myapp/
          username
          password
    

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

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

    spring.config.import=optional:configtree:/etc/config/
    

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

    【讨论】:

      【解决方案2】:

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

      如何创建一个示例:-

      $ kubectl create clusterrolebinding admin --clusterrole=cluster-admin --serviceaccount=default:default

      【讨论】:

        【解决方案3】:

        您需要在清单文件中提供秘密类型。希望它会起作用。

        apiVersion: v1
        kind: Secret
        metadata:
          name: secret-demo
        type: Opaque
        data:
          spring.datasource.password: cG9zdGdyZXM
        

        【讨论】:

        • 不,它不起作用。其实它已经在那里了,但忘了在这里粘贴
        猜你喜欢
        • 2019-10-05
        • 2022-11-04
        • 1970-01-01
        • 2020-06-04
        • 2017-11-11
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 2021-05-03
        相关资源
        最近更新 更多