Categories DevOps

Kubernetes ConfigMaps for Container Environment Variables

Introduction

In modern containerized applications, managing configuration data effectively is crucial for maintaining flexible and portable deployments. Kubernetes ConfigMaps provide a powerful mechanism for handling application configuration, particularly when it comes to setting environment variables in containers. This guide explores how to leverage ConfigMaps to manage container environment variables efficiently and securely.

ConfigMaps are Kubernetes API objects designed to store non-confidential configuration data in key-value pairs. They serve as a separation layer between your application’s configuration and the container images, enabling you to modify configuration without rebuilding containers.

This guide is intended for Kubernetes administrators, DevOps engineers, and developers who want to implement robust configuration management in their Kubernetes environments. We assume basic familiarity with Kubernetes concepts and kubectl commands.

Throughout this guide, we’ll cover everything from basic concepts to advanced implementation patterns, ensuring you have the knowledge to effectively use ConfigMaps in your container environments.

Understanding ConfigMaps Basics

Core Concepts and Functionality

ConfigMaps serve as Kubernetes’ native configuration management solution, designed to:

  • Store configuration data separately from application code
  • Support multiple data formats (key-value pairs, configuration files)
  • Enable dynamic configuration updates
  • Provide a consistent configuration interface across clusters

Benefits of Using ConfigMaps

Configuration Separation from Pod Specs

ConfigMaps allow you to maintain clear separation between application code and configuration, following the Twelve-Factor App methodology. This separation makes it easier to maintain and modify configurations without touching the application code.

Reusability Across Multiple Pods

Once created, a ConfigMap can be referenced by multiple pods, promoting configuration reuse and consistency across your applications.

Easy Maintenance and Updates

ConfigMaps centralize configuration management, making it easier to track and update configurations across your cluster.

Common Use Cases

  • Database connection settings
  • Application feature flags
  • API endpoints and service URLs
  • Logging configurations
  • Application-specific settings

Limitations and Constraints

  • Size Limit: ConfigMaps are limited to 1MB in size
  • Non-sensitive Data: ConfigMaps store data in plain text and shouldn’t be used for sensitive information
  • Creation Order: ConfigMaps must exist before pods can reference them
  • Update Behavior: Changes to ConfigMaps don’t automatically propagate to running pods

Creating and Managing ConfigMaps

Command-line Approach

Using kubectl, you can create ConfigMaps directly from the command line:

kubectl create configmap app-config \
    --from-literal=DB_HOST=mysql.default \
    --from-literal=DB_PORT=3306 \
    --from-literal=APP_MODE=production

YAML Manifest Approach

Alternatively, you can create ConfigMaps using YAML manifests app-config.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  DB_HOST: "mysql.default"
  DB_PORT: "3306"
  APP_MODE: "production"
kubectl apply -f app-config.yaml

The output from either the command line or YAML output of kubectl describe configmap should look the same:

kubectl describe configmap -n default app-config
Name:         app-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
APP_MODE:
----
production

DB_HOST:
----
mysql.default

DB_PORT:
----
3306


BinaryData
====

Events:  <none>

Best Practices for Creation

Naming Conventions

  • Use descriptive, lowercase names
  • Include the environment or application name
  • Follow a consistent naming pattern
  • Example: myapp-prod-config

Organization Strategies

  • Group related configurations together
  • Use separate ConfigMaps for different components
  • Consider environment-specific ConfigMaps
  • Maintain clear documentation

Version Control Considerations

  • Store ConfigMap definitions in version control
  • Use templates for different environments
  • Document all configuration changes
  • Implement review processes for configuration updates

Implementing ConfigMaps as Environment Variables

Different Implementation Methods

Single Environment Variable

env:
- name: DATABASE_HOST
  valueFrom:
    configMapKeyRef:
      name: app-config
      key: DB_HOST

All Values as Environment Variables

envFrom:
- configMapRef:
    name: app-config

Specific Keys as Environment Variables

env:
- name: DB_CONNECTION
  valueFrom:
    configMapKeyRef:
      name: app-config
      key: database-url
      optional: true

Practical Examples

Database Configuration

Create the ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: database-config
data:
  DB_HOST: "mysql.default.svc.cluster.local"
  DB_PORT: "3306"
  DB_NAME: "myapp" 

Use the ConfigMap in a Kubernetes deployment:

---
apiVersion: v1
kind: Pod
metadata:
  name: database-app
spec:
  containers:
  - name: app
    image: myapp:1.0
    envFrom:
    - configMapRef:
        name: database-config

Best Practices and Common Patterns

Naming and Organization

  • Use consistent prefixes for related ConfigMaps
  • Implement clear naming hierarchies
  • Group configurations logically
  • Document naming conventions

Environment-specific Configurations

  • Maintain separate ConfigMaps for each environment
  • Use namespaces to isolate configurations
  • Implement environment-specific validation
  • Document environment differences

Namespace Usage

  • Organize ConfigMaps by namespace
  • Use namespace-specific configurations
  • Implement proper access controls
  • Consider multi-tenant scenarios

Handling ConfigMap Updates

Update Mechanisms

  • Manual updates via kubectl
  • CI/CD pipeline automation
  • GitOps workflows
  • Configuration management tools

Pod Behavior with ConfigMap Changes

  • Changes don’t automatically propagate
  • Pods require restart for updates
  • Volume mounts have different behavior
  • Consider update strategies

Manual Update Processes

  1. Update ConfigMap definition
  2. Apply changes to cluster
  3. Restart affected pods
  4. Verify configuration updates

Rolling Updates Best Practices

  • Implement gradual rollouts
  • Use readiness probes
  • Monitor application behavior
  • Plan rollback procedures

Security Considerations

Storage and Encryption

  • ConfigMaps are stored unencrypted
  • Use Secrets for sensitive data
  • Implement proper RBAC
  • Consider encryption at rest

Read Storing Secrets in Kubernetes for more information on secret storage.

Access Control with RBAC

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: configmap-reader
rules:
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["get", "list"]

Alternative Solutions for Sensitive Data

  • Kubernetes Secrets
  • External secret managers
  • Vault integration

Conclusion

ConfigMaps are a powerful tool for managing container environment variables in Kubernetes environments. By following the practices outlined in this guide, you can implement robust configuration management that is both maintainable and scalable.

More From Author

You May Also Like