首页 > 解决方案 > Helm 图表中的 Spring Boot 应用程序属性

问题描述

我有一个 Spring Boot 应用程序,现在我为它生成了一个舵图。我正在使用来自 k8s 的 ConfigMap 来创建这个应用程序属性。但是当我检查 pod 时,我看到以下错误:

2021-05-31 09:39:31.815 WARN 1 --- [ost-startStop-1] osbarm.jpa.DatabaseLookup:无法从数据源确定 jdbc url

org.springframework.jdbc.support.MetaDataAccessException:无法获取用于提取元数据的连接;嵌套异常是 org.springframework.jdbc.CannotGetJdbcConnectionException: 获取 JDBC 连接失败;嵌套异常是 org.postgresql.util.PSQLException:连接尝试失败。

我将应用程序属性编写为 ConfigMap:

kind: ConfigMap
apiVersion: v1
metadata:
  name: myconfigmap
data:
  application.properties: |-
    server.port = 8080
    spring.datasource.url={{ .Values.database.url }}
    spring.datasource.username={{ .Values.database.username }}
    spring.datasource.password={{ .Values.database.password }}

在 deployment.yaml 中,我使用它来调用它:

      envFrom:
        - configMapRef:
            name: myconfigmap

我使用 azure 的 keyvault 覆盖了 .Values.database...。使这个文件在我的 k8s 集群上可用的最佳方法是什么?

我用这个命令覆盖了变量:

helm upgrade --namespace namescpace --install --set "database.url=database_url,database.username=username,database.password=password" name_application chartname

主类:

@SpringBootApplication
@Configuration
@EnableScheduling

公共类应用程序扩展 SpringBootServletInitializer {

/**
 * Main method.
 *
 * @param args
 *            args passed to the Spring Boot App. Can be used to set the
 *            active profile.
 */
public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

/**
 * Configure method for enabling deployment in external tomcat.
 *
 * {@inheritDoc}
 */
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}

}

标签: javaspringazurekubernetes

解决方案


您不能将文件作为 env-var 注入。只有简单的 key=value 条目。

如果你想保持你的 configMap 原样,你应该把它作为一个卷安装在你的容器中。

          volumeMounts:
          - name: application-config 
            mountPath: "/config" 
            readOnly: true
      volumes:
      - name: application-config
        configMap:
          name: myconfigmap
          items:
          - key: application.properties 
            path: application.properties

Application.properties 现在将放在 /config 目录下。

Spring 将在启动时按照文档加载安装的文件:https ://docs.spring.io/spring-boot/docs/2.1.8.RELEASE/reference/html/boot-features-external-config.html#boot-功能外部配置应用程序属性文件


推荐阅读