🐶
Kubernetes

Kubernetes Secrets vs ConfigMaps: When to Use Which?

By Jan on 02/10/2025

Learn when to leverage Kubernetes Secrets for sensitive data and when ConfigMaps are suitable for configuration settings in your applications.

Kubernetes Secrets vs ConfigMaps: When to Use Which?

Table of Contents

Introduction

In Kubernetes, managing application configuration and sensitive data effectively is crucial for portability and security. ConfigMaps and Secrets provide mechanisms to store this data externally from your codebase. This separation enhances flexibility and allows for easier management of different environments. Let's explore when to use each and understand their key differences.

Step-by-Step Guide

Both ConfigMaps and Secrets are used in Kubernetes to store data externally from your application code, promoting portability and flexibility.

Let's say you have a database connection string:

db_host=my-database
db_user=admin
db_password=MySuperSecretPassword

When to use ConfigMaps:

  • Store non-sensitive configuration data.
  • Data is stored in plain text.

Example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  db_host: my-database
  db_user: admin

When to use Secrets:

  • Store sensitive data like passwords, API keys, and tokens.
  • Data is stored base64 encoded, providing a basic level of obfuscation.

Example:

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  db_password: TXlTdXBlclNlY3JldFBhc3N3b3Jk

Key Differences:

  • Data Sensitivity: ConfigMaps for non-sensitive, Secrets for sensitive data.
  • Encoding: ConfigMaps store data in plain text, Secrets use base64 encoding.
  • Security: Secrets offer a minimal layer of security, but neither are truly secure for highly sensitive information. Consider using a dedicated secrets management solution for enhanced security.

Important Considerations:

  • While Secrets offer basic obfuscation, they are not a complete security solution.
  • Both ConfigMaps and Secrets can be mounted as volumes or exposed as environment variables within your Pods.
  • Choose the appropriate method based on your application's needs and the sensitivity of the data.

Code Example

This code provides an example of managing database connection details in Kubernetes using ConfigMaps and Secrets. A ConfigMap stores non-sensitive data like database host and username. A Secret securely stores the sensitive database password in base64 encoded format. A Deployment configuration demonstrates how to use these ConfigMaps and Secrets to inject environment variables into a container, allowing the application to access database connection details securely. The example also includes commands to apply these configurations using kubectl.

This example demonstrates how to use ConfigMaps and Secrets to manage database connection details in a Kubernetes deployment.

1. Create a ConfigMap for non-sensitive data:

apiVersion: v1
kind: ConfigMap
metadata:
  name: db-config
data:
  db_host: my-database
  db_user: admin

2. Create a Secret for the sensitive password:

apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
data:
  db_password: TXlTdXBlclNlY3JldFBhc3N3b3Jk  # Base64 encoded "MySuperSecretPassword"

3. Create a Deployment that uses both ConfigMap and Secret:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: <your-app-image>
        env:
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: db-config
              key: db_host
        - name: DB_USER
          valueFrom:
            configMapKeyRef:
              name: db-config
              key: db_user
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: db_password

Explanation:

  • ConfigMap: We store the non-sensitive db_host and db_user in a ConfigMap named db-config.
  • Secret: The sensitive db_password is stored base64 encoded in a Secret named db-secret.
  • Deployment:
    • We define environment variables (DB_HOST, DB_USER, DB_PASSWORD) within the container.
    • We use configMapKeyRef to fetch values from the db-config ConfigMap.
    • We use secretKeyRef to fetch the password from the db-secret Secret.

Applying the configuration:

You can apply these configurations using kubectl:

kubectl apply -f configmap.yaml
kubectl apply -f secret.yaml
kubectl apply -f deployment.yaml

This setup ensures that sensitive data is handled securely while keeping your application code independent of specific configuration values. Remember that while Secrets offer a basic level of obfuscation, consider using a dedicated secrets management solution for enhanced security in production environments.

Additional Notes

Security:

  • Neither ConfigMaps nor Secrets are truly secure by default. Base64 encoding in Secrets is easily reversible and should not be considered encryption.
  • Treat any data in ConfigMaps and Secrets as potentially accessible. Avoid storing highly sensitive information in them.
  • Implement additional security measures for sensitive data:
    • Secrets Management Solutions: Consider dedicated tools like HashiCorp Vault, AWS Secrets Manager, or Azure Key Vault for robust encryption, access control, and auditing.
    • Encryption at Rest: Enable etcd encryption to protect data stored in the Kubernetes cluster.
    • RBAC: Use Role-Based Access Control (RBAC) to restrict access to Secrets and ConfigMaps based on user roles and permissions.

Best Practices:

  • Use the principle of least privilege. Grant access to Secrets and ConfigMaps only to the Pods and users that absolutely require it.
  • Rotate Secrets regularly. This limits the impact of a potential compromise.
  • Use environment variables for injecting configuration. This promotes flexibility and avoids hardcoding values in your application code.
  • Version your ConfigMaps and Secrets. This helps track changes and roll back to previous configurations if needed.
  • Consider using tools for managing ConfigMaps and Secrets:
    • Helm: Package and deploy applications with their configurations.
    • Kustomize: Customize Kubernetes configurations using overlays.

Alternatives to Consider:

  • Sealed Secrets: Encrypts Secrets at rest using public-key cryptography, so only authorized users can decrypt them.
  • External Secrets Operator: Manages Secrets stored in external secrets management systems, syncing them into your Kubernetes cluster.

Remember: Security is an ongoing process. Regularly review and update your security practices and tools to mitigate risks.

Summary

Feature ConfigMap Secret
Purpose Store non-sensitive configuration data Store sensitive data like passwords and API keys
Data Storage Plain text Base64 encoded
Security No obfuscation Basic obfuscation, not suitable for highly sensitive data
Example Use Case Database hostname, application settings Database password, API tokens
Accessibility Mountable as volumes, exposed as environment variables Same as ConfigMaps

Key Takeaways:

  • Choose ConfigMaps for non-sensitive data and Secrets for sensitive data.
  • Neither option provides robust security. Consider dedicated secrets management solutions for highly sensitive information.
  • Both ConfigMaps and Secrets offer flexibility in how you access the data within your Pods.

Conclusion

In conclusion, ConfigMaps and Secrets are essential tools in Kubernetes for managing application configuration and sensitive data, respectively. While Secrets offer a basic level of obfuscation through base64 encoding, it's crucial to remember that neither mechanism provides ironclad security. For highly sensitive information, adopting a dedicated secrets management solution is non-negotiable. By understanding the strengths, limitations, and best practices associated with ConfigMaps and Secrets, developers can leverage these Kubernetes objects to build secure, portable, and easily manageable applications.

References

  • Secrets vs Configmaps and its security : r/kubernetes Secrets vs Configmaps and its security : r/kubernetes | Posted by u/maqueonlyverse - 3 votes and 15 comments
  • In-Depth Guide to Kubernetes ConfigMap & Secret Management ... In-Depth Guide to Kubernetes ConfigMap & Secret Management ... | Nov 15, 2023 ... ConfigMaps stores data as key-value pairs, whereas Secrets stores data as base64-encoded data, thereby ensuring an additional layer of security.
  • Difference between Kubernetes ConfigMaps and Secrets | CloudTruth Difference between Kubernetes ConfigMaps and Secrets | CloudTruth | Learn differences between ConfigMaps and Secrets. Both are stored the same way, but ConfigMaps are plain text and secrets are only for apps.
  • Kubernetes ConfigMaps and Secrets | by Sandeep Dinesh | Google ... Kubernetes ConfigMaps and Secrets | by Sandeep Dinesh | Google ... | Everyone needs to configure applications. You often need to reference “special” bits of data, such as API keys, tokens, and other secrets…
  • Secrets | Kubernetes Secrets | Kubernetes | A Secret is an object that contains a small amount of sensitive data such as a password, a token, or a key. Such information might otherwise be put in a Pod specification or in a container image. Using a Secret means that you don't need to include confidential data in your application code. Because Secrets can be created independently of the Pods that use them, there is less risk of the Secret (and its data) being exposed during the workflow of creating, viewing, and editing Pods.
  • Kubernetes Configmaps and Secrets | Create and Update Kubernetes Configmaps and Secrets | Create and Update | Kubernetes ConfigMaps and Secrets are Kubernetes resources that you can use to separate configuration from pod specs.
  • ConfigMaps | Kubernetes ConfigMaps | Kubernetes | A ConfigMap is an API object used to store non-confidential data in key-value pairs. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume. A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable. Caution:ConfigMap does not provide secrecy or encryption. If the data you want to store are confidential, use a Secret rather than a ConfigMap, or use additional (third party) tools to keep your data private.
  • Kubernetes 101: Secret and ConfigMap | by 8grams | Medium Kubernetes 101: Secret and ConfigMap | by 8grams | Medium | Introduction to Kubernetes Secrets
  • Kubernetes ConfigMaps and Secrets: When to Use Them? Kubernetes ConfigMaps and Secrets: When to Use Them? | Learn how Kubernetes ConfigMaps and Secrets work, see if they interact with CAST AI, and what that means for your config security.

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait