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;
}
}
Adding Message Properties Into The Spring Boot Project In order to do that, just create two files in src/main/resources/messages folder with naming as below, api_error_messages. properties. api_response_messages.
By default, a Spring Boot application will look for message files containing internationalization keys and values in the src/main/resources folder. The file for the default locale will have the name messages. properties, and files for each locale will be named messages_XX. properties, where XX is the locale code.
To override your Spring Boot application properties when it's running on Kubernetes, just set environment variables on the container. To set an environment variable on a container, first, initialise a ConfigMap containing the environment variables that you want to override.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With