DEV Community

Cover image for ConfigMap in Kubernetes: Solving Problems with Environment Variables
Avesh
Avesh

Posted on

ConfigMap in Kubernetes: Solving Problems with Environment Variables

ConfigMap is a key Kubernetes resource that decouples application configurations from the application itself, enabling dynamic configuration management. By leveraging ConfigMaps, you can address limitations of hardcoded environment variables, simplify updates, and centralize your configuration settings.

This comprehensive guide will cover:

  1. What is ConfigMap?
  2. How ConfigMap solves environment variable problems
  3. Ways to create and use ConfigMaps
  4. Hands-on examples
  5. Best practices for using ConfigMaps

What is ConfigMap?

A ConfigMap is a Kubernetes resource used to store configuration data in the form of key-value pairs. This data can be consumed by Pods in various ways:

  • Environment variables
  • Command-line arguments
  • Mounted volumes containing configuration files

Why Use ConfigMaps?

  1. Decouples Configuration from Code: Keeps your application portable by separating configurations from application logic.
  2. Facilitates Updates: Changes to configurations don't require redeploying the application.
  3. Centralized Management: Manage configurations across multiple applications in one place.
  4. Dynamic Adjustments: Configurations can be updated without modifying the application image.

How ConfigMap Solves Problems with Environment Variables

Hardcoding environment variables directly in Pod specifications can lead to issues such as:

  1. Static Configurations: Changing configurations requires updating Pod definitions.
  2. Duplication: Managing the same configurations across multiple Pods becomes cumbersome.
  3. Lack of Centralization: No single source of truth for configuration data.

ConfigMaps address these challenges by providing:

  • Centralized, reusable configurations for multiple Pods.
  • Easy updates and rollouts without altering Pod definitions.
  • Improved security when combined with Kubernetes Secrets for sensitive data.

Creating and Using ConfigMaps

1. Creating a ConfigMap

a) Using a YAML File

apiVersion: v1  
kind: ConfigMap  
metadata:  
  name: app-config  
data:  
  APP_ENV: "production"  
  DATABASE_URL: "db.example.com"  
  LOG_LEVEL: "info"  
Enter fullscreen mode Exit fullscreen mode

Create the ConfigMap using:

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

b) Using the kubectl create configmap Command

kubectl create configmap app-config --from-literal=APP_ENV=production --from-literal=LOG_LEVEL=info  
Enter fullscreen mode Exit fullscreen mode

c) From a File

Create a text file app.properties:

APP_ENV=production  
DATABASE_URL=db.example.com  
LOG_LEVEL=info  
Enter fullscreen mode Exit fullscreen mode

Generate a ConfigMap from the file:

kubectl create configmap app-config --from-file=app.properties  
Enter fullscreen mode Exit fullscreen mode

2. Using a ConfigMap in Pods

a) Inject as Environment Variables

Example YAML:

apiVersion: v1  
kind: Pod  
metadata:  
  name: app-pod  
spec:  
  containers:  
  - name: app-container  
    image: busybox  
    command: ["/bin/sh", "-c", "env; sleep 3600"]  
    env:  
    - name: APP_ENV  
      valueFrom:  
        configMapKeyRef:  
          name: app-config  
          key: APP_ENV  
    - name: LOG_LEVEL  
      valueFrom:  
        configMapKeyRef:  
          name: app-config  
          key: LOG_LEVEL  
  restartPolicy: Never  
Enter fullscreen mode Exit fullscreen mode

Apply the Pod:

kubectl apply -f pod-with-env.yaml  
Enter fullscreen mode Exit fullscreen mode

Verify the environment variables:

kubectl exec -it app-pod -- env  
Enter fullscreen mode Exit fullscreen mode

b) Mount as Volumes

Example YAML:

apiVersion: v1  
kind: Pod  
metadata:  
  name: app-pod-volume  
spec:  
  containers:  
  - name: app-container  
    image: busybox  
    command: ["/bin/sh", "-c", "cat /etc/config/*; sleep 3600"]  
    volumeMounts:  
    - name: config-volume  
      mountPath: /etc/config  
  volumes:  
  - name: config-volume  
    configMap:  
      name: app-config  
Enter fullscreen mode Exit fullscreen mode

Deploy the Pod:

kubectl apply -f pod-with-volume.yaml  
Enter fullscreen mode Exit fullscreen mode

Inspect the files in the mounted directory:

kubectl exec -it app-pod-volume -- ls /etc/config  
kubectl exec -it app-pod-volume -- cat /etc/config/APP_ENV  
Enter fullscreen mode Exit fullscreen mode

3. Updating ConfigMaps

To update a ConfigMap, you can:

  1. Edit the existing ConfigMap directly:
   kubectl edit configmap app-config  
Enter fullscreen mode Exit fullscreen mode
  1. Replace with a new configuration:
   kubectl apply -f updated-configmap.yaml  
Enter fullscreen mode Exit fullscreen mode
  1. Update with kubectl commands:
   kubectl create configmap app-config --from-literal=APP_ENV=staging --dry-run=client -o yaml | kubectl apply -f -  
Enter fullscreen mode Exit fullscreen mode

Changes in ConfigMaps automatically propagate to Pods using them as mounted volumes. However, they don't automatically update Pods using them as environment variables (requires Pod restart).


4. Using ConfigMaps in Deployments

ConfigMaps can be used in Deployment specifications to dynamically manage configurations for replicated Pods.

Example YAML:

apiVersion: apps/v1  
kind: Deployment  
metadata:  
  name: app-deployment  
spec:  
  replicas: 3  
  selector:  
    matchLabels:  
      app: my-app  
  template:  
    metadata:  
      labels:  
        app: my-app  
    spec:  
      containers:  
      - name: app-container  
        image: busybox  
        command: ["/bin/sh", "-c", "env; sleep 3600"]  
        envFrom:  
        - configMapRef:  
            name: app-config  
Enter fullscreen mode Exit fullscreen mode

Deploy the application:

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

Best Practices for Using ConfigMaps

  1. Use ConfigMaps for Non-Sensitive Data: Store sensitive data in Kubernetes Secrets.
  2. Version Control: Keep ConfigMap YAML definitions in version control.
  3. Combine with Secrets: Use both ConfigMaps and Secrets for comprehensive configuration management.
  4. Namespace Segregation: Create ConfigMaps in the same namespace as their consuming Pods.
  5. Avoid Large Configurations: Use ConfigMaps for lightweight configurations; for larger files, consider PersistentVolumes.
  6. Dynamic Config Updates: Use mounted volumes for dynamic updates without restarting Pods.

Conclusion

Kubernetes ConfigMaps offer a robust and flexible way to manage application configurations. By separating configurations from application logic, ConfigMaps simplify updates, promote reusability, and enhance application portability.

Start integrating ConfigMaps in your Kubernetes workflows to streamline configuration management and improve the scalability of your deployments.

For further learning, check out the official Kubernetes ConfigMap documentation.

Top comments (0)