Configuration and Secrets

Introduction

Early in my Kubernetes journey, I hard-coded configuration values directly into container images. Database connection strings, API keys, feature flagsβ€”all baked into the image. This created a nightmare: every configuration change required rebuilding images, retagging, and redeploying. Different environments needed different images. Security was compromised because sensitive data lived in version control and container registries.

The breakthrough came when I properly understood ConfigMaps and Secrets. Configuration became external to the application, environment-specific values could be injected at runtime, and sensitive data could be managed securely. Through deploying applications across development, staging, and production environments, and integrating with external secret managers like AWS Secrets Manager and HashiCorp Vault, I learned that proper configuration management is foundational to operational excellence.

In this article, I'll share practical knowledge about managing configuration and secrets in Kubernetes, from basic ConfigMaps to enterprise-grade secret management solutions.

Table of Contents

Configuration Management Principles

Effective configuration management follows the Twelve-Factor App methodology: separate configuration from code.

The Problem with Hard-Coded Configuration

Problems:

  • Same image can't be used across environments

  • Configuration changes require rebuilding images

  • Secrets exposed in image layers

  • Can't rotate credentials without rebuilding

  • Violates separation of concerns

The Kubernetes Way

spinner

ConfigMaps Fundamentals

ConfigMaps store non-sensitive configuration data as key-value pairs.

Creating ConfigMaps

From Literal Values:

From Files:

From Environment File:

ConfigMap YAML Definitions

Simple Key-Value ConfigMap:

ConfigMap with File Content:

ConfigMap with Multiple Configuration Files:

Binary Data in ConfigMaps

Managing ConfigMaps

Secrets Fundamentals

Secrets store sensitive data like passwords, tokens, and keys.

Types of Secrets

Kubernetes supports several secret types:

  • Opaque: Generic secret (default)

  • kubernetes.io/service-account-token: Service account token

  • kubernetes.io/dockercfg: Docker config file

  • kubernetes.io/dockerconfigjson: Docker config JSON

  • kubernetes.io/basic-auth: Basic authentication

  • kubernetes.io/ssh-auth: SSH authentication

  • kubernetes.io/tls: TLS certificate and key

  • bootstrap.kubernetes.io/token: Bootstrap token

Creating Secrets

From Literal Values:

From Files:

Docker Registry Secret:

Secret YAML Definitions

Generic Secret:

Or with Base64 Encoded Data:

TLS Secret:

Basic Auth Secret:

SSH Auth Secret:

Docker Config Secret:

Managing Secrets

Consuming ConfigMaps and Secrets

There are multiple ways to consume ConfigMaps and Secrets in pods.

As Environment Variables

Single Values:

All Keys as Environment Variables:

Combined Environment Variables:

As Volume Mounts

Mount ConfigMap as Files:

Selective Key Mounting:

Projected Volumes (Multiple Sources):

Hot Reloading Configuration

ConfigMaps and Secrets mounted as volumes are automatically updated (with a delay).

Note: Environment variables from ConfigMaps/Secrets are NOT automatically updated. Pods must be restarted to pick up changes.

Immutable ConfigMaps and Secrets

Prevent accidental modifications:

Once set to immutable, the ConfigMap cannot be modified and must be deleted/recreated to change values.

Secret Encryption

By default, Secrets are stored in etcd as base64-encoded (NOT encrypted). Enable encryption at rest for production.

Encryption Configuration

Generate encryption key:

Configure API server:

Encrypt existing secrets:

Verify Encryption

External Secrets Management

Integrate with external secret managers for enterprise-grade security.

External Secrets Operator

Install External Secrets Operator:

AWS Secrets Manager Integration:

HashiCorp Vault Integration:

Google Secret Manager:

Sealed Secrets

Encrypt secrets for Git storage:

Sealed Secret Example:

Configuration Best Practices

Organize by Environment

Base ConfigMap:

Production Overlay:

Use Descriptive Names

Version Configuration

Validate Configuration

Troubleshooting Configuration Issues

Debugging Missing Configuration

Configuration Not Updating

Secret Decoding Issues

Permission Issues

What I Learned

Mastering configuration management in Kubernetes transformed application deployment and operations:

Separate Configuration from Code: This principle seems obvious but took discipline to implement consistently. Every configuration valueβ€”environment-specific settings, feature flags, credentialsβ€”should be external to the application image.

Use the Right Tool: ConfigMaps for non-sensitive configuration, Secrets for credentials, and external secret managers for enterprise requirements. Don't store secrets in ConfigMaps or configuration in Secrets.

Environment Variables vs. Files: Both have their place. Environment variables are simple and work everywhere. Files are better for complex configuration, allow hot-reloading, and support tools that expect configuration files.

Encryption Matters: Base64 encoding is not encryption. Enable etcd encryption at rest and use external secret managers for sensitive production data.

Immutability Prevents Accidents: Making ConfigMaps and Secrets immutable prevents accidental modifications in production. When updates are needed, create new versions with different names.

Plan for Configuration Updates: Understand that environment variables require pod restarts while volume mounts auto-update (with delay). Design your update strategy accordingly.

Version Everything: Version your ConfigMaps and Secrets just like application code. This enables rollbacks and makes it clear which configuration belongs to which application version.

Proper configuration management is foundational to running applications successfully in Kubernetes. With ConfigMaps, Secrets, and external secret managers, you can build secure, flexible, environment-agnostic applications. In the next articles, we'll explore persistent storage and how to manage stateful applications effectively.

Last updated