wasAsked.

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

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Kubernetes, DevOps

Community answers

Posted by wasAsked editorial collection

Both ConfigMaps and Secrets inject configuration into Pods without baking it into container images. The difference is the kind of data they are meant for and how Kubernetes treats it. ConfigMap - Stores non-sensitive configuration as key-value pairs or whole files: feature flags, URLs of other services, log levels, application settings files. apiVersion: v1 kind: ConfigMap metadata: { name: orders-config } data: LOG_LEVEL: "Information" PAYMENTS_URL: "http://payments-api" appsettings.Production.json: | { "Features": { "NewCheckout": true } } - Data is stored in plain text in etcd and visible to anyone with read access to ConfigMaps. - Size limit of 1 MiB. Secret - Stores sensitive data: passwords, API keys, tokens, TLS certificates, registry credentials. apiVersion: v1 kind: Secret metadata: { name: orders-secrets } type: Opaque stringData: ConnectionStrings__Orders: "Server=sql;Database=Orders;User Id=app;Password=..." - Values are base64-encoded in the API — encoding is not encryption; anyone who can read the Secret can decode it. - Kubernetes adds some protections: Secrets can be encrypted at rest in etcd (when the cluster enables encryption providers — managed services such as AKS, EKS and GKE support this, often with a cloud KMS), kubelets keep mounted secrets in memory (tmpfs) rather than on disk, access can be restricted separately with RBAC, and secrets are only sent to nodes running Pods that need them. - Special types: kubernetes.io/tls, kubernetes.io/dockerconfigjson, service account tokens. - Size limit of 1 MiB. Using them in Pods As environment variables: envFrom: - configMapRef: { name: orders-config } - secretRef: { name: orders-secrets } As mounted files: volumes: - name: config configMap: { name: orders-config } - name: secrets secret: { secretName: orders-secrets } containers: - name: api volumeMounts: - { name: config, mountPath: /app/config, readOnly: true } - { name: secrets, mountPath: /app/secrets, readOnly: true } Updates - Mounted ConfigMaps and Secrets are updated in the Pod automatically after a short delay (except when mounted with subPath), so applications that watch files can reload them. - Environment variables do not change in running Pods; you must restart Pods (for example kubectl rollout restart, or tools that roll Pods when configuration changes). - Both can be marked immutable: true to prevent accidental changes and reduce API server load. Key differences - Purpose: ConfigMap for non-sensitive settings; Secret for confidential data. - Encoding: plain text vs base64 (not encryption). - Protection: Secrets support encryption at rest, tmpfs mounting and stricter RBAC. - Typical use: URLs and flags vs passwords, keys and certificates. Best practices for secrets - Enable encryption at rest and restrict RBAC access (get, list, watch on Secrets should be limited). - Avoid committing Secret manifests to Git; use sealed or encrypted secrets (Sealed Secrets, SOPS) or sync from external stores with the External Secrets Operator or the Secrets Store CSI Driver (Azure Key Vault, AWS Secrets Manager, HashiCorp Vault). - Prefer workload identity (Azure Workload Identity, EKS Pod Identity or IRSA) so applications access cloud resources without stored credentials. - Prefer mounted files over environment variables for highly sensitive values, since environment variables can leak through logs, crash dumps or child processes. Interview summary: "ConfigMaps hold non-sensitive configuration in plain text; Secrets hold sensitive data, base64-encoded and protected with RBAC, tmpfs mounting and optional encryption at rest. For real security, combine Secrets with encryption, strict access control and external secret managers."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics