DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

Protecting Your Credentials with ConfigMaps in Spring Boot

1. Understanding ConfigMaps in Kubernetes

ConfigMaps are a powerful feature in Kubernetes that allow you to decouple configuration artifacts from container images. By using ConfigMaps, you can keep your sensitive data out of your codebase and ensure that your applications are easier to maintain and more secure.

1.1 What Are ConfigMaps?

ConfigMaps are Kubernetes objects designed to store non-sensitive configuration data as key-value pairs. These can be used to configure applications, providing a means to separate configuration from code. Unlike Secrets, ConfigMaps are intended for less sensitive data.

Image

1.2 Why Use ConfigMaps for Spring Boot?

Using ConfigMaps with Spring Boot offers several advantages:

  • Separation of Concerns : By keeping configuration separate from application code, you make it easier to manage and update configurations without rebuilding Docker images.
  • Flexibility : ConfigMaps can be updated dynamically, allowing for real-time configuration changes without redeploying applications.
  • Environment Management : Different ConfigMaps can be used for different environments (e.g., development, staging, production), simplifying environment-specific configurations.

1.3 Key Considerations

While ConfigMaps are useful, it's important to remember:

  • Data Sensitivity : For highly sensitive data like passwords or API keys, consider using Kubernetes Secrets instead.
  • Size Limits : ConfigMaps have size limitations, so ensure that they do not exceed these constraints to avoid deployment issues.

2. Implementing ConfigMaps in Spring Boot

To leverage ConfigMaps in a Spring Boot application, follow these steps:

2.1 Creating a ConfigMap

Start by defining a ConfigMap that contains your configuration data. Here's a simple YAML example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  application.properties: |
    spring.datasource.url=jdbc:mysql://localhost:3306/mydb
    spring.datasource.username=user
    spring.datasource.password=pass
Enter fullscreen mode Exit fullscreen mode

Save this configuration as configmap.yaml and apply it using the following command:

kubectl apply -f configmap.yaml
Enter fullscreen mode Exit fullscreen mode

2.2 Accessing ConfigMap Data in Spring Boot

Once the ConfigMap is created, you need to mount it into your Spring Boot application. Modify your Deployment YAML to include the ConfigMap as a volume:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-spring-boot-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-spring-boot-app
  template:
    metadata:
      labels:
        app: my-spring-boot-app
    spec:
      containers:
      - name: my-spring-boot-app
        image: my-spring-boot-image
        volumeMounts:
        - name: config-volume
          mountPath: /config
      volumes:
      - name: config-volume
        configMap:
          name: my-config
Enter fullscreen mode Exit fullscreen mode

In your Spring Boot application, you can then load the configuration from the mounted file. For example, you can use @PropertySource or @Value to read properties from the file:

@Configuration
@PropertySource("file:/config/application.properties")
public class AppConfig {
    @Value("${spring.datasource.url}")
    private String datasourceUrl;

    @Value("${spring.datasource.username}")
    private String datasourceUsername;

    @Value("${spring.datasource.password}")
    private String datasourcePassword;

    // getters and other configurations
}
Enter fullscreen mode Exit fullscreen mode

3. Testing and Results

Deploy your application and verify that the configuration values are correctly applied. Here’s how you can test the setup:

  • Deploy the Application : Apply your deployment YAML file with kubectl apply -f deployment.yaml.
  • Check ConfigMap Values : Use kubectl exec to access the container and verify that the configuration values are correctly mounted.
  • Application Logs : Check the Spring Boot application logs to ensure that the configuration is being applied as expected.

When the application starts, it should use the values defined in the ConfigMap. For example, if you configured a database URL in the ConfigMap, verify that the application connects to the correct database.

4. Conclusion

Using ConfigMaps is a strategic approach to managing application configuration in Kubernetes, especially when working with Spring Boot. It not only improves security and flexibility but also enhances your ability to manage application settings efficiently.

Feel free to leave any questions or comments below!

Read posts more at : Protecting Your Credentials with ConfigMaps in Spring Boot

Top comments (0)