spring-boot - 如何从安装为文件的 kubernetes Secrets 中填充 application.properties 文件值

标签 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
secret 文件:
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 Prop 从 secret 移动到 configmap 那么它可以正常工作,或者如果我将其值填充为 env 变量,那么它也可以正常工作。
但正如我们所知,这两者都不是安全的方法,有人可以告诉我为 secret 文件挂载有什么问题吗?

最佳答案

默认情况下,出于安全原因,不启用通过 API 使用 secret 。Spring Cloud Kubernetes 需要访问 Kubernetes API,以便能够检索为单个服务运行的 pod 的地址列表。使用 Minikube 时最简单的方法是使用集群管理员权限创建默认的 ClusterRoleBinding。
关于如何创建一个的示例:-
$ kubectl create clusterrolebinding admin --clusterrole=cluster-admin --serviceaccount=default:default

关于spring-boot - 如何从安装为文件的 kubernetes Secrets 中填充 application.properties 文件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66444197/

相关文章:

kubernetes - kubelet 无法获取 docker 和 kubelet 服务的 cgroup 统计信息

java - 在 Tomcat 中访问 externalize application.properties for Spring boot 应用程序?

java - JPA 存储库上的 Spring 重试

java - Spring Boot 2.2.0 不推荐使用syncBody

java - 使用继承的 Groovy 命令无法在 Spring Boot 远程 shell 中编译

kubernetes - 访问Kubernetes负载均衡服务背后的客户端IP地址

apache-spark - 如何在没有 kerberos 的情况下启动 spark 3.0.0 kubernetes 工作负载?

kubernetes - 为什么入口服务有地址?该地址有什么用?

java - 请求反序列化期间的自定义声明式 spring pojo 验证

ssl - 如何强制 apiserver 代理使用 HTTPS 连接到容器