存在 spring-cloud-kubernetes 配置映射依赖项的 spring 应用程序启动异常

Posted

技术标签:

【中文标题】存在 spring-cloud-kubernetes 配置映射依赖项的 spring 应用程序启动异常【英文标题】:Exception on spring application startup with spring-cloud-kubernetes config maps dependencies present 【发布时间】:2021-07-25 11:00:48 【问题描述】:

我有一些 Spring 服务,它们同时具有 Eureka-client 和 spring-cloud-starter-kubernetes-fabric8-all 依赖项。默认情况下,Eureka 处于启用状态,Kubernetes 处于禁用状态。

management:
  endpoints:
    web:
      exposure:
        include: '*'

eureka:
  client:
    enabled: true
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

spring:
  main:
    banner-mode: off
  application:
    name: service-one
  cloud:
    kubernetes:
      enabled: false
      config:
        enable-api: false
        enabled: false
      reload:
        enabled: false
  zipkin:
    enabled: false

当我启动应用程序时,我收到以下异常作为警告,即使 kubernetes 已禁用。 正在运行 - docker run --rm dhananjay12/demo-service-one:latest

2021-05-02 23:35:51.458  INFO [,,] 1 --- [           main] ubernetesProfileEnvironmentPostProcessor : Did not find service account namespace at: [/var/run/secrets/kubernetes.io/serviceaccount/namespace]. Ignoring.
2021-05-02 23:35:51.464  WARN [,,] 1 --- [           main] ubernetesProfileEnvironmentPostProcessor : Not running inside kubernetes. Skipping 'kubernetes' profile activation.
2021-05-02 23:35:51.980  WARN [,,] 1 --- [           main] io.fabric8.kubernetes.client.Config      : Error reading service account token from: [/var/run/secrets/kubernetes.io/serviceaccount/token]. Ignoring.
2021-05-02 23:35:51.984  WARN [,,] 1 --- [           main] io.fabric8.kubernetes.client.Config      : Error reading service account token from: [/var/run/secrets/kubernetes.io/serviceaccount/token]. Ignoring.
2021-05-02 23:35:51.987  WARN [,,] 1 --- [           main] o.s.c.k.f.Fabric8AutoConfiguration       : No namespace has been detected. Please specify KUBERNETES_NAMESPACE env var, or use a later kubernetes version (1.3 or later)
2021-05-02 23:35:52.441  WARN [service-one,,] 1 --- [           main] s.c.k.f.c.Fabric8ConfigMapPropertySource : Can't read configMap with name: [service-one] in namespace:[null]. Ignoring.

io.fabric8.kubernetes.client.KubernetesClientException: Operation: [get]  for kind: [ConfigMap]  with name: [service-one]  in namespace: [null]  failed.
    at io.fabric8.kubernetes.client.KubernetesClientException.launderThrowable(KubernetesClientException.java:64) ~[kubernetes-client-4.13.2.jar:na]
    at io.fabric8.kubernetes.client.KubernetesClientException.launderThrowable(KubernetesClientException.java:72) ~[kubernetes-client-4.13.2.jar:na]
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.getMandatory(BaseOperation.java:225) ~[kubernetes-client-4.13.2.jar:na]
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.get(BaseOperation.java:186) ~[kubernetes-client-4.13.2.jar:na]
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.get(BaseOperation.java:84) ~[kubernetes-client-4.13.2.jar:na]
    at org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySource.getData(Fabric8ConfigMapPropertySource.java:61) ~[spring-cloud-kubernetes-fabric8-config-2.0.2.jar:2.0.2]
    at org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySource.<init>(Fabric8ConfigMapPropertySource.java:50) ~[spring-cloud-kubernetes-fabric8-config-2.0.2.jar:2.0.2]
    at org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator.getMapPropertySource(Fabric8ConfigMapPropertySourceLocator.java:51) ~[spring-cloud-kubernetes-fabric8-config-2.0.2.jar:2.0.2]
    at org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySourceLocator.getMapPropertySourceForSingleConfigMap(ConfigMapPropertySourceLocator.java:81) ~[spring-cloud-kubernetes-commons-2.0.2.jar:2.0.2]
    at org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySourceLocator.lambda$locate$0(ConfigMapPropertySourceLocator.java:67) ~[spring-cloud-kubernetes-commons-2.0.2.jar:2.0.2]
    at java.base/java.util.Collections$SingletonList.forEach(Unknown Source) ~[na:na]
    at org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySourceLocator.locate(ConfigMapPropertySourceLocator.java:67) ~[spring-cloud-kubernetes-commons-2.0.2.jar:2.0.2]
    at org.springframework.cloud.bootstrap.config.PropertySourceLocator.locateCollection(PropertySourceLocator.java:51) ~[spring-cloud-context-3.0.2.jar:3.0.2]
    at org.springframework.cloud.bootstrap.config.PropertySourceLocator.locateCollection(PropertySourceLocator.java:47) ~[spring-cloud-context-3.0.2.jar:3.0.2]
    at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:95) ~[spring-cloud-context-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:650) ~[spring-boot-2.4.5.jar:2.4.5]
    at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:403) ~[spring-boot-2.4.5.jar:2.4.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.4.5.jar:2.4.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340) ~[spring-boot-2.4.5.jar:2.4.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) ~[spring-boot-2.4.5.jar:2.4.5]
    at com.mynotes.microservices.demo.serviceone.ServiceOneApplication.main(ServiceOneApplication.java:15) ~[classes/:na]
Caused by: java.net.UnknownHostException: kubernetes.default.svc: Name or service not known

如果我将相同的属性放在 env 变量中,则不存在异常。 正在运行 - docker run --rm -e spring.cloud.kubernetes.config.enable-api=false dhananjay12/demo-service-one:latest

虽然应用程序确实启动并且其余部分正常工作,但有人可以建议,为什么首先会出现这个异常?

代码 - https://github.com/dhananjay12/spring-microservice-demo/blob/master/service-one/src/main/resources/application.yml Spring Boot - 2.4.5,Spring Cloud - 2020.0.2

【问题讨论】:

【参考方案1】:

在进一步分析和阅读文档时,必须在 bootstrap.yml - https://docs.spring.io/spring-cloud-kubernetes/docs/current/reference/html/index.html#kubernetes-ecosystem-awareness 中设置禁用这些功能。

当然 env 变量会有优先级

【讨论】:

以上是关于存在 spring-cloud-kubernetes 配置映射依赖项的 spring 应用程序启动异常的主要内容,如果未能解决你的问题,请参考以下文章

#yyds干货盘点#spring-cloud-kubernetes与SpringCloud Gateway

#yyds干货盘点#spring-cloud-kubernetes背后的三个关键知识点

想了解 spring-cloud-kubernetes,那就先来实战一把官方demo

部署 spring-cloud-kubernetes kubernetes-hello-world-example 失败

spring-cloud-kubernetes官方demo运行实战

#yyds干货盘点#spring-cloud-kubernetes官方demo运行实战