Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Kubernetes

Kubernetes: Use ConfigMaps and Secrets to Separate Configuration from Code

- 22.03.26 - ErcanOPAK

🔐 Never Hardcode Config Again

API keys in code? Database passwords committed to Git? ConfigMaps and Secrets externalize all configuration.

ConfigMaps vs Secrets

📝 ConfigMaps

Non-sensitive configuration

  • API endpoints
  • Feature flags
  • Environment settings
  • Application config files

🔒 Secrets

Sensitive data (base64 encoded)

  • Database passwords
  • API keys
  • TLS certificates
  • OAuth tokens

Creating ConfigMaps

# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  API_URL: "https://api.example.com"
  LOG_LEVEL: "info"
  MAX_CONNECTIONS: "100"
  FEATURE_FLAG_NEW_UI: "true"
# Create from YAML
kubectl apply -f configmap.yaml

# Or create from command line
kubectl create configmap app-config \
  --from-literal=API_URL=https://api.example.com \
  --from-literal=LOG_LEVEL=info

# From file
kubectl create configmap nginx-config \
  --from-file=nginx.conf

Creating Secrets

# secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: app-secrets
type: Opaque
data:
  DB_PASSWORD: cGFzc3dvcmQxMjM=  # base64 encoded
  API_KEY: YWJjZGVmZ2hpamtsbW5vcA==
# Create from command line
kubectl create secret generic app-secrets \
  --from-literal=DB_PASSWORD=password123 \
  --from-literal=API_KEY=abcdefghijklmnop

# Kubernetes automatically base64 encodes values

Using in Pods (Environment Variables)

apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
  - name: app
    image: myapp:latest
    env:
      # From ConfigMap
      - name: API_URL
        valueFrom:
          configMapKeyRef:
            name: app-config
            key: API_URL
      
      # From Secret
      - name: DB_PASSWORD
        valueFrom:
          secretKeyRef:
            name: app-secrets
            key: DB_PASSWORD
      
      # Load all ConfigMap keys as env vars
    envFrom:
      - configMapRef:
          name: app-config
      - secretRef:
          name: app-secrets

Using as Volume Mounts (Files)

apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
  - name: app
    image: myapp:latest
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
    - name: secret-volume
      mountPath: /etc/secrets
      readOnly: true
  
  volumes:
  - name: config-volume
    configMap:
      name: app-config
  - name: secret-volume
    secret:
      secretName: app-secrets

# Files appear at:
# /etc/config/API_URL
# /etc/config/LOG_LEVEL
# /etc/secrets/DB_PASSWORD
# /etc/secrets/API_KEY

🔄 Update Configuration Live

# Edit ConfigMap
kubectl edit configmap app-config

# Or update from file
kubectl apply -f configmap.yaml

# Pods with mounted volumes see changes within ~60 seconds
# Pods with env vars need restart to see changes
kubectl rollout restart deployment/myapp

✅ Best Practices

  • Never commit secrets to Git: Use sealed-secrets or external secret managers
  • Namespace secrets: Each namespace gets its own secrets
  • RBAC: Restrict who can read secrets
  • Encryption at rest: Enable in Kubernetes for production
  • External managers: Consider HashiCorp Vault, AWS Secrets Manager for production

🎯 Real-World Example

# Development ConfigMap
kubectl create configmap app-config-dev \
  --from-literal=API_URL=https://dev-api.example.com \
  --from-literal=LOG_LEVEL=debug

# Production ConfigMap
kubectl create configmap app-config-prod \
  --from-literal=API_URL=https://api.example.com \
  --from-literal=LOG_LEVEL=error

# Same app, different config per environment
# Deploy uses app-config-dev or app-config-prod

“Removed all hardcoded config from images. Now same image runs in dev/staging/prod with different ConfigMaps. Changed API endpoint across 50 pods by editing one ConfigMap. No rebuild, no redeploy.”

— Platform Engineer

📊 Benefits

0
Secrets in Git

1
Image, Many Envs

✓
Security

Related posts:

Kubernetes: Use HPA to Auto-Scale Pods Based on CPU/Memory

Kubernetes: Use Secrets to Store Sensitive Data Instead of ConfigMaps

Kubernetes Pods Behave Differently After Restarts

Post Views: 5

Post navigation

WordPress: Enable Object Caching with Redis for 10x Performance
Docker: Add Health Checks to Detect and Restart Failing Containers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (751)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (447)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (751)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com