Configuration Management
ConfigMaps
ConfigMaps are Kubernetes’ primary mechanism for managing application configuration in a declarative, version-controlled way. They allow you to decouple configuration from your application code and infrastructure, enabling cleaner deployments, easier debugging, and better scalability. In this section, we’ll explore two critical use cases: environment variables and configuration injection.
Environment Variables
Environment variables are a fundamental way to pass configuration to containers, but managing them directly in deployment manifests becomes cumbersome as your application grows. ConfigMaps provide a structured solution to define and inject these values securely and consistently.
Here’s how to create a ConfigMap with environment variables:
<code class="language-yaml">apiVersion: v1 <p>kind: ConfigMap</p> <p>metadata:</p> <p> name: app-config</p> <p>data:</p> <p> DB_URL: "http://db-service:5432"</p> <p> DB_TIMEOUT: "30s"</code>
This ConfigMap stores two critical configuration values. Now, inject these into a container using env fields in your deployment:
<code class="language-yaml">apiVersion: apps/v1 <p>kind: Deployment</p> <p>metadata:</p> <p> name: app-deployment</p> <p>spec:</p> <p> template:</p> <p> spec:</p> <p> containers:</p> <p> - name: app</p> <p> env:</p> <p> - name: DB_URL</p> <p> valueFrom:</p> <p> configMapKeyRef:</p> <p> name: app-config</p> <p> key: DB_URL</p> <p> - name: DB_TIMEOUT</p> <p> valueFrom:</p> <p> configMapKeyRef:</p> <p> name: app-config</p> <p> key: DB_TIMEOUT</code>
Key insights:
- The
valueFrom.configMapKeyRefsyntax directly maps ConfigMap keys to environment variables - You can inject multiple ConfigMap values at once using
envFrom(see example below) - This approach keeps configuration external to your container image, avoiding hard-coded values
For complex deployments, use envFrom to simplify configuration injection:
<code class="language-yaml">apiVersion: apps/v1 <p>kind: Deployment</p> <p>metadata:</p> <p> name: complex-app</p> <p>spec:</p> <p> template:</p> <p> spec:</p> <p> containers:</p> <p> - name: complex-app</p> <p> envFrom:</p> <p> - configMapRef:</p> <p> name: app-config</code>
This single line injects all values from app-config into the container’s environment. Perfect for applications that need many configuration parameters without verbose env declarations.
💡 Pro Tip: When using JSON configuration, store it as a string in the ConfigMap and parse it in your application. Example:
<code class="language-yaml">apiVersion: v1
<p>kind: ConfigMap</p>
<p>metadata:</p>
<p> name: api-config</p>
<p>data:</p>
<p> api.json: |</p>
<p> {</p>
<p> "port": 8080,</p>
<p> "timeout": "30s"</p>
<p> }</code>
Configuration Injection
Beyond environment variables, ConfigMaps excel at injecting configuration files directly into your application’s filesystem. This is essential for applications that require file-based configuration (e.g., Spring Boot, Java, or Python apps).
Here’s a practical example of injecting a application.properties file:
<code class="language-yaml">apiVersion: v1 <p>kind: ConfigMap</p> <p>metadata:</p> <p> name: app-config</p> <p>data:</p> <p> application.properties: |</p> <p> spring.config.location=file:./application.properties</p> <p> server.port=8080</p> <p> logging.level.root=INFO</code>
Now mount this ConfigMap as a volume in your container:
<code class="language-yaml">apiVersion: apps/v1 <p>kind: Deployment</p> <p>metadata:</p> <p> name: java-app</p> <p>spec:</p> <p> template:</p> <p> spec:</p> <p> containers:</p> <p> - name: java-app</p> <p> volumeMounts:</p> <p> - name: app-config-volume</p> <p> mountPath: /app/config</p> <p> command: ["sh", "-c"]</p> <p> args: ["cat /app/config/application.properties"]</p> <p> volumes:</p> <p> - name: app-config-volume</p> <p> configMap:</p> <p> name: app-config</code>
Why this matters:
- Applications can read configuration from disk instead of environment variables
- Enables runtime configuration changes without restarting containers
- Works seamlessly with frameworks that use file-based configuration (e.g., Spring Boot, Django)
- Provides auditability through Kubernetes’ version control
For real-world applications, you might combine both approaches:
<code class="language-yaml">apiVersion: apps/v1 <p>kind: Deployment</p> <p>metadata:</p> <p> name: full-stack-app</p> <p>spec:</p> <p> template:</p> <p> spec:</p> <p> containers:</p> <p> - name: full-stack-app</p> <p> env:</p> <p> - name: DB_URL</p> <p> valueFrom:</p> <p> configMapKeyRef:</p> <p> name: app-config</p> <p> key: DB_URL</p> <p> volumeMounts:</p> <p> - name: config-volume</p> <p> mountPath: /app/config</p> <p> volumes:</p> <p> - name: config-volume</p> <p> configMap:</p> <p> name: app-config</code>
Summary
In this section, we’ve covered how ConfigMaps solve two critical configuration challenges in Kubernetes:
- Environment variables: Inject configuration values securely into container environments using
envorenvFrom - Configuration injection: Mount configuration files directly into your application’s filesystem for runtime flexibility
By leveraging ConfigMaps, you achieve:
- Clean separation of configuration from application code
- Version-controlled configuration across deployments
- Safe, declarative configuration updates
- Enhanced observability through Kubernetes-native configuration management
This approach scales seamlessly with your application’s complexity while maintaining the simplicity and reliability that make Kubernetes so powerful. 🌟