Definition
- ConfigMap is a Kubernetes object that stores non-sensitive configuration data as key-value pairs or entire files.
- Secret is a Kubernetes object that stores sensitive data, encoded in Base64, with access controlled by RBAC.
- Environment variable injection reads a ConfigMap or Secret key and sets it as an environment variable in the container.
- Volume mount projects ConfigMap or Secret data as files inside the container's filesystem.
- Encryption at rest encrypts Secret data stored in etcd, which is essential for banking compliance.
Think of the National Bank of India IT department. A ConfigMap is like the public configuration file pinned on the office bulletin board: which database server to connect to, which Kafka topic to use, and what the daily transfer limit is. Anyone in the team can see it. A Secret is like the sealed envelope in the branch manager's locked drawer: the database password, the API key for the payment gateway, and the TLS certificate for HTTPS. Only authorized personnel can open the envelope.
Understanding the Concepts
What is a ConfigMap?
A ConfigMap stores configuration data as key-value pairs. You create it from literal values, files, or directories. Pods consume ConfigMaps as environment variables or mounted files.
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: transfer-config
namespace: banking
data:
DAILY_TRANSFER_LIMIT: "1000000"
MIN_BALANCE: "5000"
KAFKA_TOPIC: "account-transactions"
LOG_LEVEL: "INFO"
application.yml: |
spring:
datasource:
url: jdbc:postgresql://bank-db:5432/bankdb
hikari:
maximum-pool-size: 10
app:
daily-transfer-limit: 1000000
min-balance: 5000
# Create ConfigMap from literal values
kubectl create configmap transfer-config \
--from-literal=DAILY_TRANSFER_LIMIT=1000000 \
--from-literal=MIN_BALANCE=5000 \
-n banking
# Create ConfigMap from a file
kubectl create configmap app-config \
--from-file=application.yml \
-n banking
# View ConfigMap
kubectl get configmap transfer-config -n banking -o yaml
package com.bank.k8s;
public class ConfigMapDemo {
public static void main(String[] args) {
// In a container, these come from the ConfigMap
String dailyLimit = System.getenv("DAILY_TRANSFER_LIMIT");
String minBalance = System.getenv("MIN_BALANCE");
String kafkaTopic = System.getenv("KAFKA_TOPIC");
// Simulate reading from ConfigMap environment variables
if (dailyLimit == null) dailyLimit = "1000000";
if (minBalance == null) minBalance = "5000";
if (kafkaTopic == null) kafkaTopic = "account-transactions";
System.out.println("Configuration from ConfigMap:");
System.out.println(" Daily transfer limit: " + dailyLimit);
System.out.println(" Minimum balance: " + minBalance);
System.out.println(" Kafka topic: " + kafkaTopic);
}
}
Configuration from ConfigMap:
Daily transfer limit: 1000000
Minimum balance: 5000
Kafka topic: account-transactions
What is a Secret?
A Secret stores sensitive data like passwords, tokens, and certificates. The data is Base64-encoded (not encrypted) by default. You must enable encryption at rest in the Kubernetes API server to encrypt Secrets stored in etcd.
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: bank-db-credentials
namespace: banking
type: Opaque
stringData: # Kubernetes encodes this to Base64 automatically
DB_USERNAME: bankadmin
DB_PASSWORD: Pr0d_S3cure_P@ss!
REDIS_PASSWORD: R3d1s_T0k3n_2025
# Create Secret from literal values
kubectl create secret generic bank-db-credentials \
--from-literal=DB_USERNAME=bankadmin \
--from-literal=DB_PASSWORD='Pr0d_S3cure_P@ss!' \
-n banking
# View Secret (values are Base64 encoded)
kubectl get secret bank-db-credentials -n banking -o yaml
# Decode a specific value
kubectl get secret bank-db-credentials -n banking \
-o jsonpath='{.data.DB_USERNAME}' | base64 --decode
bankadmin
package com.bank.k8s;
import java.util.Base64;
public class SecretDemo {
public static void main(String[] args) {
// Simulating how Kubernetes stores Secrets (Base64)
String password = "Pr0d_S3cure_P@ss!";
String encoded = Base64.getEncoder().encodeToString(password.getBytes());
String decoded = new String(Base64.getDecoder().decode(encoded));
System.out.println("Secret storage:");
System.out.println(" Original: " + password);
System.out.println(" Base64 (stored in etcd): " + encoded);
System.out.println(" Decoded (injected into Pod): " + decoded);
System.out.println();
System.out.println("WARNING: Base64 is encoding, NOT encryption.");
System.out.println("Anyone who can read the Secret can decode it.");
System.out.println("Enable encryption at rest for banking Secrets.");
}
}
Secret storage:
Original: Pr0d_S3cure_P@ss!
Base64 (stored in etcd): UHIwZF9TM2N1cmVfUEBzcyE=
Decoded (injected into Pod): Pr0d_S3cure_P@ss!
WARNING: Base64 is encoding, NOT encryption.
Anyone who can read the Secret can decode it.
Enable encryption at rest for banking Secrets.
How do Pods consume ConfigMaps and Secrets?
There are two ways: environment variables and volume mounts.
# deployment-with-config.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: transfer-service
namespace: banking
spec:
replicas: 3
selector:
matchLabels:
app: transfer-service
template:
metadata:
labels:
app: transfer-service
spec:
containers:
- name: transfer
image: national-bank/transfer-service:1.0.0
# Method 1: Environment variables from ConfigMap and Secret
env:
- name: DAILY_TRANSFER_LIMIT
valueFrom:
configMapKeyRef:
name: transfer-config
key: DAILY_TRANSFER_LIMIT
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: bank-db-credentials
key: DB_USERNAME
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: bank-db-credentials
key: DB_PASSWORD
# Method 2: Volume mount for files
volumeMounts:
- name: app-config
mountPath: /app/config
readOnly: true
volumes:
- name: app-config
configMap:
name: transfer-config
items:
- key: application.yml
path: application.yml
package com.bank.k8s;
public class ConsumptionDemo {
public static void main(String[] args) {
System.out.println("Method 1: Environment Variables");
System.out.println(" DAILY_TRANSFER_LIMIT = " + getEnv("DAILY_TRANSFER_LIMIT", "1000000"));
System.out.println(" DB_USERNAME = " + getEnv("DB_USERNAME", "bankadmin"));
System.out.println(" DB_PASSWORD = ******* (hidden)");
System.out.println();
System.out.println("Method 2: Volume Mount");
System.out.println(" /app/config/application.yml (file from ConfigMap)");
System.out.println();
System.out.println("Difference:");
System.out.println(" Env vars: require Pod restart to pick up changes");
System.out.println(" Volume mount: updates automatically (1-2 min delay)");
}
static String getEnv(String key, String defaultVal) {
String val = System.getenv(key);
return val != null ? val : defaultVal;
}
}
Method 1: Environment Variables
DAILY_TRANSFER_LIMIT = 1000000
DB_USERNAME = bankadmin
DB_PASSWORD = ******* (hidden)
Method 2: Volume Mount
/app/config/application.yml (file from ConfigMap)
Difference:
Env vars: require Pod restart to pick up changes
Volume mount: updates automatically (1-2 min delay)
How do you compare ConfigMaps and Secrets?
| Aspect | ConfigMap | Secret |
|---|---|---|
| Purpose | Non-sensitive config | Sensitive data |
| Storage | Plain text in etcd | Base64 in etcd (encrypt at rest) |
| Size limit | 1 MiB per ConfigMap | 1 MiB per Secret |
| Examples | DB URL, log level, feature flags | DB password, API key, TLS cert |
| RBAC | Usually read-all | Restricted to specific roles |
| Consumption | Env vars or volume mount | Env vars or volume mount |
| Update behavior (mounted) | Auto-refreshed (kubelet sync) | Auto-refreshed (kubelet sync) |
| Update behavior (env var) | Requires Pod restart | Requires Pod restart |
What are common mistakes with ConfigMaps and Secrets?
Mistake 1: Hardcoding passwords in the Deployment YAML
Wrong: the password is visible in the Deployment manifest checked into Git.
# WRONG: password in plain text in the Deployment
env:
- name: DB_PASSWORD
value: "Pr0d_S3cure_P@ss!"
Correct: reference a Secret.
# CORRECT: reference a Secret object
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: bank-db-credentials
key: DB_PASSWORD
Mistake 2: Assuming Base64 is encryption
Wrong: thinking Secrets are secure because they are Base64-encoded.
# Anyone with kubectl access can decode the Secret
kubectl get secret bank-db-credentials -n banking \
-o jsonpath='{.data.DB_PASSWORD}' | base64 --decode
Correct: enable encryption at rest and restrict RBAC.
# EncryptionConfiguration for the API server
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
providers:
- aescbc:
keys:
- name: key1
secret: <base64-encoded-32-byte-key>
- identity: {}
How do you use external secret managers?
For banking production systems, Kubernetes Secrets alone are not enough. Use an external secret manager (AWS Secrets Manager, HashiCorp Vault, Azure Key Vault) with the External Secrets Operator or CSI Secrets Store Driver.
# ExternalSecret (using External Secrets Operator)
apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
name: bank-db-credentials
namespace: banking
spec:
refreshInterval: 1h
secretStoreRef:
name: aws-secrets-manager
kind: ClusterSecretStore
target:
name: bank-db-credentials
data:
- secretKey: DB_USERNAME
remoteRef:
key: /banking/production/db-credentials
property: username
- secretKey: DB_PASSWORD
remoteRef:
key: /banking/production/db-credentials
property: password
package com.bank.k8s;
public class ExternalSecretsDemo {
public static void main(String[] args) {
System.out.println("External secret manager flow:");
System.out.println();
System.out.println("1. Secret stored in AWS Secrets Manager");
System.out.println(" Key: /banking/production/db-credentials");
System.out.println(" Value: {username: bankadmin, password: ...}");
System.out.println();
System.out.println("2. ExternalSecret operator syncs to K8s Secret");
System.out.println(" Refresh interval: 1 hour");
System.out.println(" Creates: bank-db-credentials Secret in banking ns");
System.out.println();
System.out.println("3. Pod consumes the K8s Secret as usual");
System.out.println(" env.valueFrom.secretKeyRef: bank-db-credentials");
System.out.println();
System.out.println("Benefits:");
System.out.println(" - Secrets never stored in Git");
System.out.println(" - Rotation handled by the secret manager");
System.out.println(" - Audit trail for access");
}
}
External secret manager flow:
1. Secret stored in AWS Secrets Manager
Key: /banking/production/db-credentials
Value: {username: bankadmin, password: ...}
2. ExternalSecret operator syncs to K8s Secret
Refresh interval: 1 hour
Creates: bank-db-credentials Secret in banking ns
3. Pod consumes the K8s Secret as usual
env.valueFrom.secretKeyRef: bank-db-credentials
Benefits:
- Secrets never stored in Git
- Rotation handled by the secret manager
- Audit trail for access
What should a senior engineer know?
- Immutable ConfigMaps and Secrets. Mark ConfigMaps and Secrets as immutable: true to prevent accidental changes in production. Immutable objects also reduce load on the API server because kubelet does not need to poll for changes.
- ConfigMap/Secret hot-reload. When a ConfigMap or Secret is mounted as a volume, kubelet updates the files within 1-2 minutes. Spring Boot applications can use @RefreshScope or Spring Cloud Config to reload properties without restarting.
- Sealed Secrets. Bitnami Sealed Secrets encrypts Secrets with a cluster-specific key so they can be safely stored in Git. The SealedSecret controller decrypts them inside the cluster.
- Secret rotation. Banking compliance requires periodic credential rotation. Use an external secret manager with automatic rotation. The ExternalSecret operator syncs the rotated value to the Kubernetes Secret, and mounted volumes pick up the change automatically.
- Namespace isolation. ConfigMaps and Secrets are namespace-scoped. A Secret in the banking namespace cannot be accessed by Pods in the monitoring namespace. Use RBAC to restrict which ServiceAccounts can read which Secrets.
What are common interview follow-up questions?
- How do you rotate database credentials without downtime?
- What is the difference between a ConfigMap and an environment variable set directly in the Deployment?
- How do you prevent developers from reading production Secrets?
- What happens to mounted ConfigMap data when the ConfigMap is updated?
- How do Sealed Secrets work?
Visual Overview
ConfigMap and Secret Injection into Pods
==========================================
ConfigMap: transfer-config Secret: bank-db-credentials
┌─────────────────────────┐ ┌─────────────────────────┐
│ DAILY_TRANSFER_LIMIT: │ │ DB_USERNAME: bankadmin │
│ 1000000 │ │ DB_PASSWORD: ********* │
│ MIN_BALANCE: 5000 │ │ (Base64 encoded) │
│ application.yml: ... │ └─────────────────────────┘
└─────────────────────────┘ │
│ │
│ env var env var│
▼ ▼
┌──────────────────────────────────────────────┐
│ Pod: transfer-service │
│ │
│ Environment: │
│ DAILY_TRANSFER_LIMIT=1000000 │
│ DB_USERNAME=bankadmin │
│ DB_PASSWORD=Pr0d_S3cure_P@ss! │
│ │
│ Volume Mount: │
│ /app/config/application.yml (from CM) │
└──────────────────────────────────────────────┘
External Secret Manager Flow
==============================
AWS Secrets Manager ──> ExternalSecret Operator ──> K8s Secret ──> Pod
(source of truth) (syncs every 1h) (local copy) (consumes)
Key Takeaway
ConfigMaps and Secrets separate configuration from container images. The same transfer-service image runs in development with a local database URL and in production with the RDS endpoint. The image never changes. Only the ConfigMap and Secret change per environment.
ConfigMaps hold non-sensitive data: database URLs, feature flags, daily transfer limits, log levels. Secrets hold sensitive data: database passwords, API keys, TLS certificates. Both can be injected as environment variables or mounted as files inside the Pod. The important difference is that Secrets have Base64 encoding and RBAC restrictions, but Base64 is not encryption. For banking systems, you must enable encryption at rest so Secrets stored in etcd are truly encrypted.
For production banking environments, Kubernetes Secrets alone are not sufficient. Use an external secret manager like AWS Secrets Manager or HashiCorp Vault. The External Secrets Operator syncs secrets from the external manager into Kubernetes Secrets, giving you centralized management, automatic rotation, and an audit trail.
The most common mistake is hardcoding credentials in the Deployment YAML and committing it to Git. Even if you delete the commit later, the password is in Git history forever. Always reference a Secret object, and never store the Secret manifest with real values in your repository.
One sentence to remember: ConfigMaps hold config, Secrets hold credentials, both decouple configuration from the image, and production banking systems back Secrets with an external secret manager.
---