Apply Now Apply Now Apply Now
header_logo
Post thumbnail
DEVOPS

Kubernetes ConfigMaps and Secrets: Managing Application Configuration

By Vishalini Devarajan

Kubernetes ConfigMaps store non-sensitive configuration such as URLs and feature flags. Secrets store sensitive data such as passwords and API keys. Both are API objects that you inject into Pods as environment variables or mounted files, so the same container image works in every environment.

Hardcoding a database URL or password inside a container image causes problems fast. Change one value and you rebuild the image. Share the image and you may leak credentials with it.

Kubernetes configuration management fixes this by separating configuration from code. In this guide, you will learn how Kubernetes ConfigMaps and Secrets work, how to create them with YAML, how to use them in Pods, and how to keep them secure.

Table of contents


  1. What Are Kubernetes ConfigMaps?
  2. How to Create and Use a ConfigMap
    • Create a ConfigMap with YAML
    • Create a ConfigMap with kubectl
  3. What Are Kubernetes Secrets?
  4. How to Create and Use a Secret
    • Create a Secret with YAML
    • Create a Secret with kubectl
  5. ConfigMap vs Secret: Key Differences
  6. How to Use Environment Variables in Kubernetes
  7. Mounting ConfigMaps and Secrets as Volumes
  8. Kubernetes Configuration Management Best Practices
  9. Common Mistakes to Avoid
  10. Practical Use Cases
    • Conclusion
  11. Frequently Asked Questions
    • What is the difference between a ConfigMap and a Secret in Kubernetes?
    • Are Kubernetes Secrets really secure?
    • How do I use a ConfigMap as an environment variable in a Pod?
    • Does a Pod pick up ConfigMap changes automatically?
    • What is the size limit for a ConfigMap or Secret?

What Are Kubernetes ConfigMaps?

A ConfigMap is a Kubernetes API object that stores non-confidential configuration as key-value pairs. Pods can read these values as environment variables, command-line arguments, or files in a volume.

Use a ConfigMap when:

  • The same image runs in dev, staging, and production with different settings.
  • You need config files such as nginx.conf or app.properties.
  • The values are safe to expose, like LOG_LEVEL or an API base URL.

A ConfigMap can hold up to 1 MiB of data. It does not encrypt anything, so never put passwords in it.

How to Create and Use a ConfigMap

Create a ConfigMap with YAML

apiVersion: v1

kind: ConfigMap

metadata:

  name: app-config

data:

  APP_ENV: "production"

  LOG_LEVEL: "info"

  app.properties: |

    cache.ttl=300

    feature.newUI=true

The first two keys are simple values. The app.properties key holds a whole file, written as a multi-line string.

Create a ConfigMap with kubectl

kubectl apply -f configmap.yaml

kubectl create configmap app-settings --from-literal=APP_ENV=production --from-file=app.properties

kubectl get configmap app-config -o yaml

Use –from-literal for single values and –from-file to load a file as one key.

Want to build practical DevOps skills and work with tools used in modern software environments? Explore the HCL GUVI DevOps Course and gain hands-on experience with Docker, Kubernetes, CI/CD, and cloud technologies to advance your DevOps career.

What Are Kubernetes Secrets?

A Secret is a Kubernetes API object designed for sensitive data such as passwords, tokens, and TLS keys. Keeping this data in a Secret instead of the Pod spec or the image reduces accidental exposure and lets you control access with RBAC.

Common Secret types include:

  • Opaque: the default, for any arbitrary key-value data.
  • kubernetes.io/tls: a TLS certificate and private key.
  • kubernetes.io/dockerconfigjson: credentials for pulling private images.
  • kubernetes.io/basic-auth: a username and password pair.

Important: Secret values are base64-encoded, not encrypted. Anyone who can read the Secret can decode it in seconds. Real protection comes from RBAC and encryption at rest.

How to Create and Use a Secret

Create a Secret with YAML

apiVersion: v1

kind: Secret

metadata:

  name: db-credentials

type: Opaque

stringData:

  DB_USER: appuser

  DB_PASSWORD: "S3cr3t-P@ss"

stringData lets you write plain text and Kubernetes encodes it for you. If you use the data field instead, you must supply base64 values yourself:

echo -n 'S3cr3t-P@ss' | base64

Create a Secret with kubectl

kubectl create secret generic db-credentials \

  --from-literal=DB_USER=appuser \

  --from-literal=DB_PASSWORD='S3cr3t-P@ss'

This keeps the password out of any YAML file. Never commit a Secret manifest with real values to Git.

ConfigMap vs Secret: Key Differences

Both objects look similar in YAML, but they serve different purposes. Here is the ConfigMap vs Secret comparison at a glance.

FeatureConfigMapSecret
PurposeNon-sensitive configurationSensitive data
Typical contentURLs, feature flags, config filesPasswords, tokens, TLS keys
Data formatPlain text in data, binary in binaryDataBase64-encoded in data, plain text accepted in stringData
EncryptionNoneNone by default; encryption at rest can be enabled
TypesOne generic typeOpaque, kubernetes.io/tls, kubernetes.io/dockerconfigjson, and more
Size limit1 MiB1 MiB
Access controlStandard RBACStandard RBAC, usually restricted more tightly
Volume storage on nodesNode filesystemMemory-backed tmpfs, not written to disk
Immutable optionYes (immutable: true)Yes (immutable: true)
Use in PodsEnv vars, volumes, command argumentsEnv vars, volumes, image pull credentials

The rule of thumb is simple: if leaking the value would cause harm, use a Secret.

Also Explore: What to Learn to Become a DevOps Engineer in 2026? 

How to Use Environment Variables in Kubernetes

Environment variables in Kubernetes are the most common way to pass configuration into a container. You can inject a single key or every key at once.

apiVersion: v1

kind: Pod

metadata:

  name: demo-app

spec:

  containers:

    - name: app

      image: nginx:1.27

      envFrom:

        - configMapRef:

            name: app-config

      env:

        - name: DB_PASSWORD

          valueFrom:

            secretKeyRef:

              name: db-credentials

              key: DB_PASSWORD

Here is what each part does:

  • envFrom with configMapRef loads every key from app-config as an environment variable.
  • env with secretKeyRef loads one key, DB_PASSWORD, from the Secret.
  • Use configMapKeyRef when you want a single ConfigMap key under a custom variable name.

Keys that are not valid environment variable names, such as some file-style keys, are skipped when you use envFrom.

Watch out: environment variables are read once, when the container starts. If you update the ConfigMap or Secret, restart the Pods to pick up the change:

kubectl rollout restart deployment my-app

Mounting ConfigMaps and Secrets as Volumes

Volume mounts suit config files and certificates. Each key becomes a file inside the container, and the key’s value becomes the file’s content.

apiVersion: v1

kind: Pod

metadata:

  name: demo-app-volumes

spec:

  containers:

    - name: app

      image: nginx:1.27

      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: db-credentials

        defaultMode: 0400

Inside the container, you will find /etc/config/app.properties and /etc/secrets/DB_PASSWORD. The defaultMode: 0400 setting makes the Secret files readable only by their owner.

GUVI Ad

A few behaviors worth knowing:

  • Mounted ConfigMaps and Secrets update automatically after a short delay, without a Pod restart. Your application must re-read the files.
  • Mounts that use subPath do not receive these updates.
  • Secret volumes are backed by memory (tmpfs), so the values are not written to the node’s disk.

Also Read: Tech Upskilling Roadmap 2026: Top Skills, Tools, and Platforms You Need 

Kubernetes Configuration Management Best Practices

  1. Enable encryption at rest. Configure the API server to encrypt Secrets in etcd, since base64 offers no protection.
  2. Apply least-privilege RBAC. Limit get, list, and watch on Secrets to the workloads and people who need them. Note that list also exposes values.
  3. Prefer volume mounts for Secrets. Environment variables can leak through logs, crash dumps, and child processes.
  4. Use an external secret manager for production. Tools like HashiCorp Vault, cloud secret managers, or the External Secrets Operator keep the source of truth outside the cluster.
  5. Keep secrets out of Git. If you follow GitOps, use Sealed Secrets or SOPS to commit encrypted values only.
  6. Mark stable objects immutable. Setting immutable: true prevents accidental edits and reduces load on the API server.
  7. Separate config by namespace. Keep dev, staging, and production configuration in different namespaces with their own access rules.
  8. Rotate credentials regularly. Update the Secret, then roll out the Pods so they pick up the new value.

Common Mistakes to Avoid

  • Storing passwords in a ConfigMap. ConfigMaps are plain text and easy to read. Use a Secret instead.
  • Treating base64 as encryption. Anyone with read access can decode a Secret with one command.
  • Committing Secret YAML to Git. Once a password is in your repository history, assume it is exposed.
  • Expecting env vars to update live. Environment variables change only when the Pod restarts.
  • Ignoring the size limit. Both objects cap at 1 MiB. Large files belong in a volume or an external store.
  • Giving broad Secret access. Granting a wide get or list permission on Secrets across a namespace exposes every credential in it.
  • Using subPath and expecting updates. Files mounted with subPath never refresh automatically.
GUVI Ad

Practical Use Cases

ScenarioUseWhy
Different API URLs for dev and productionConfigMapValues are not sensitive and change per environment
Custom nginx.conf mounted into a web serverConfigMapConfig files fit naturally into a volume
Feature flags and log levelsConfigMapEasy to change without rebuilding the image
Database username and passwordSecretLeaking them would expose your data
Third-party API keys and tokensSecretCredentials need access control and careful handling
TLS certificate and private keySecret (kubernetes.io/tls)Built-in type for Ingress and HTTPS workloads
Pulling images from a private registrySecret (kubernetes.io/dockerconfigjson)Used through imagePullSecrets

In a real application, you will usually combine both. The ConfigMap supplies the database host and port, while the Secret supplies the username and password.

Conclusion

Kubernetes ConfigMaps and Secrets make application configuration easier to manage by separating settings and sensitive information from application code. ConfigMaps handle non-sensitive values such as API URLs, feature flags, and log levels, while Secrets store credentials, passwords, and API keys.

Choosing the right configuration object is only the first step. You also need to understand how to inject values into Pods, mount configuration files, manage updates, and apply security practices such as encryption at rest, least-privilege RBAC, and regular credential rotation.

By using these practices, you can build more secure, flexible, and maintainable Kubernetes applications across development, staging, and production environments.

Frequently Asked Questions

1. What is the difference between a ConfigMap and a Secret in Kubernetes?

A ConfigMap stores non-sensitive configuration in plain text. A Secret stores sensitive data such as passwords and keys, with base64 encoding, type support, and tighter handling on nodes. Neither is encrypted by default.

2. Are Kubernetes Secrets really secure?

Not by default. Secrets are only base64-encoded, so anyone with API access can read them. To secure them, enable encryption at rest, restrict access with RBAC, and consider an external secret manager.

3. How do I use a ConfigMap as an environment variable in a Pod?

Reference it in the container spec. Use envFrom with configMapRef to load all keys, or env with configMapKeyRef to load one key under a name you choose.

4. Does a Pod pick up ConfigMap changes automatically?

It depends on how you consume the ConfigMap. Mounted volumes refresh after a short delay, except with subPath. Environment variables never refresh, so you need to restart the Pods.

5. What is the size limit for a ConfigMap or Secret?

Each object can hold up to 1 MiB of data. For larger files, use a persistent volume or an external storage service.

Success Stories

Did you enjoy this article?

Learn with HCL GUVI

Schedule 1:1 free counselling

Similar Articles

Loading...
Get in Touch
Chat on Whatsapp
Request Callback
Share logo Copy link
Table of contents Table of contents
Table of contents Articles
Close button

  1. What Are Kubernetes ConfigMaps?
  2. How to Create and Use a ConfigMap
    • Create a ConfigMap with YAML
    • Create a ConfigMap with kubectl
  3. What Are Kubernetes Secrets?
  4. How to Create and Use a Secret
    • Create a Secret with YAML
    • Create a Secret with kubectl
  5. ConfigMap vs Secret: Key Differences
  6. How to Use Environment Variables in Kubernetes
  7. Mounting ConfigMaps and Secrets as Volumes
  8. Kubernetes Configuration Management Best Practices
  9. Common Mistakes to Avoid
  10. Practical Use Cases
    • Conclusion
  11. Frequently Asked Questions
    • What is the difference between a ConfigMap and a Secret in Kubernetes?
    • Are Kubernetes Secrets really secure?
    • How do I use a ConfigMap as an environment variable in a Pod?
    • Does a Pod pick up ConfigMap changes automatically?
    • What is the size limit for a ConfigMap or Secret?