spring-cloudfabric8spring-cloud-kubernetes

Exception on spring application startup with spring-cloud-kubernetes config maps dependencies present


I have a few spring services which has both Eureka-client and spring-cloud-starter-kubernetes-fabric8-all dependencies. By default, Eureka is enabled and Kubernetes is disabled.

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

When I startup the app, I get the following exception as a warning, eventough kubernetes is disabled. Running - 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

The exception is not there if I put the same property in env variable. Running - docker run --rm -e spring.cloud.kubernetes.config.enable-api=false dhananjay12/demo-service-one:latest

While the app does spin up and the rest of the thing works, can someone suggest, why is this exception there in the first place?

Code - 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


Solution

  • On further analysis and going through the docs, disabling of these features must be set in bootstrap.yml - https://docs.spring.io/spring-cloud-kubernetes/docs/current/reference/html/index.html#kubernetes-ecosystem-awareness.

    Of course env variable will have precedence