We have a spring boot application that is deployed to Kubernetes. We are adding i18n capabilities to this application and want to place the messages.properties file outside the application jar/war. I have been able to do that in spring boot. How will this work when I deploy it on Kubernetes? Do I need to use the configmaps? Following is the code snippet
@Configuration
public class AppConfig {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
//Path to the messages.properties files
messageSource.setBasenames("file:/messages/messages", "classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(60);
return messageSource;
}
}
Yes you can do this with a configmap. It is much the same as accessing an external application.properties file. First you can create a ConfigMap directly from the file or create a ConfigMap representing the file:
apiVersion: v1
kind: ConfigMap
metadata:
name: treasurehunt-config
namespace: default
data:
application.properties: |
treasurehunt.max.attempts=5
Then in your kubernetes Deployment you create a Volume for the ConfigMap and mount that into the Pod under the directory you use for the external configuration:
volumeMounts:
- name: application-config
mountPath: "/config"
readOnly: true
volumes:
- name: application-config
configMap:
name: treasurehunt-config
items:
- key: application.properties
path: application.properties
These snippets come from an example of mounting a Volume from ConfigMap for an application.properties file so they use the spring boot default external properties file path of /config
. You can set that in the yaml for the mount so you could mount the file to use the same relative path that you are already using when running outside of kubernetes.