Spring Cloud Kubernetes - Spring boot not reading config map
The Spring Cloud Kubernetes documentation is incomplete. It lacks the instruction to include this dependency to enable loading application properties from ConfigMap's:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-kubernetes-config</artifactId></dependency>
You are close:
1) Define the ConfigMap a bit differently so it contains a properties file. For example:
apiVersion: v1kind: ConfigMapmetadata: name: demodata: demo.properties: | bean.message: This is an info from k8
2) Mount the ConfigMap as a volume:
...spec: containers: - name: demo ... volumeMounts: - name: config mountPath: /demo/config volumes: - name: config configMap: name: demo
As result, a demo.properties
file as defined in the ConfigMap will "appear" in the /demo/config
directory inside the running container.
3) Add @PropertySource
annotation to the MyConfig
class:
@Configuration@PropertySource("file:/demo/config/demo.properties")@ConfigurationProperties(prefix = "bean")public class MyConfig { ...}