DEV Community

Cover image for KUBERNETES: FROM ZERO TO HERO (PART 8) - CONFIG MAPS AND SECRETS
Samuel Ogunmola
Samuel Ogunmola

Posted on

KUBERNETES: FROM ZERO TO HERO (PART 8) - CONFIG MAPS AND SECRETS

In Kubernetes, Config Maps and Secrets are two types of resources that allow you to store and manage configuration data and sensitive information, respectively. Both types of resources are used to inject data into the containers in a pod, and they can be used to decouple the configuration of your applications from the code itself.

In this article, we will cover the following topics:

  • What are Config Maps and Secrets, and how are they different
  • How to create and use Config Maps and Secrets in your pods
  • The different types of Config Maps and Secrets, and their use cases
  • Best practices for managing Config Maps and Secrets in your cluster

What are Config Maps and Secrets, and how are they different

Config Maps and Secrets are both types of resources in Kubernetes that allow you to store and manage configuration data and sensitive information, respectively. However, they have some key differences:

Config Maps are used to store non-sensitive configuration data, such as environment variables, command-line arguments, and configuration files. They are stored in plain text, and they can be easily accessed and modified by anyone who has access to the cluster.

Secrets are used to store sensitive information, such as passwords, tokens, and certificates. They are stored in an encrypted form, and they can only be accessed and modified by authorized users.

Both Config Maps and Secrets are stored as key-value pairs, and they can be used to inject data into the containers in a pod. However, Config Maps are intended for non-sensitive data, while Secrets are intended for sensitive data.

How to create and use Config Maps and Secrets in your pods
To use Config Maps and Secrets in your pods, you need to create them first, and then reference them in the pod's configuration file.

Here is an example configuration file that creates a Config Map and a Secret:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config-map
data:
  my-key: my-value
---
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
data:
  my-key: bXktdmFsdWU=
Enter fullscreen mode Exit fullscreen mode

This configuration file creates a Config Map named my-config-map with a single key-value pair, and a Secret named my-secret with the same key-value pair. The value of the Secret is encoded using base64, to ensure that it is stored in an encrypted form.

To create the Config Map and the Secret in the cluster, you can use the kubectl create -f command, passing the configuration file as an argument:

kubectl create -f config-map-secret.yaml
Enter fullscreen mode Exit fullscreen mode

Once the Config Map and the Secret are created, you can reference them in the pod's configuration file. Here is an example configuration file that creates a pod that uses the Config Map and the Secret:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx
    env:
    - name: MY_ENV_VAR
      valueFrom:
        configMapKeyRef:
          name: my-config-map
          key: my-key
    - name: MY_SECRET_ENV_VAR
      valueFrom:
        secretKeyRef:
          name: my-secret
          key: my-key
Enter fullscreen mode Exit fullscreen mode

Types of Kubernetes Secret

There are several types of Secrets that you can use in Kubernetes, depending on your needs:

Generic Secrets: These are the most basic type of Secrets, and they can be used to store any kind of sensitive data. You can create Generic Secrets from a file or a directory on the host, or from literal key-value pairs.

TLS Secrets: These Secrets are used to store TLS certificates and keys, and they are used to secure the communication between pods and services. You can create TLS Secrets from a file or a directory on the host, or from literal key-value pairs.

Docker Registry Secrets: These Secrets are used to authenticate with a Docker registry, and they are used to pull images from a private registry. You can create Docker Registry Secrets from a file or a directory on the host, or from literal key-value pairs.

Service Account Tokens: These Secrets are used to authenticate with the Kubernetes API server, and they are automatically created and managed by Kubernetes. You don't need to create Service Account Tokens manually, but you can reference them in your pods and services.

In addition to these types of Secrets, Kubernetes also supports custom Secret types, which you can define and use in your cluster

Best practices for managing Config Maps and Secrets in your cluster

Here are some best practices for managing Config Maps and Secrets in your cluster:

  • Use Config Maps for non-sensitive data, and Secrets for sensitive data: As we saw in the beginning of this article, Config Maps are intended for non-sensitive data, while Secrets are intended for sensitive data. Make sure to use the appropriate type of resource for your data.
  • Use Config Maps and Secrets sparingly: Config Maps and Secrets can be useful for injecting data into the containers in a pod, but they should not be used as a replacement for proper configuration management. Avoid storing large amounts of data in Config Maps and Secrets, and consider using a configuration management tool such as Ansible, chef, or puppet.
  • Use the --dry-run flag to test your Config Maps and Secrets: Before creating a Config Map or a Secret in the cluster, you can use the--dry-run flag to test the resource without actually creating it. This can be useful for testing the resource and for debugging any issues.
  • Use the--output=yaml flag to view the generated Config Map or Secret: After creating a Config Map or a Secret in the cluster, you can use the --output=yaml flag to view the generated resource. This can be useful for verifying the content of the resource and for debugging any issues.
  • Use Config Maps and Secrets sparingly
  • : Config Maps and Secrets can be useful for injecting data into the containers in a pod, but they should not be used as a replacement for proper configuration management. Avoid storing large amounts of data in Config Maps and Secrets, and consider using a configuration management tool such as ansible, chef, or puppet.

Overall, Config Maps and Secrets are useful resources for injecting data into the containers in a pod, and they can be used to decouple the configuration of your applications from the code itself. By following the best practices outlined in this article, you can effectively manage the configuration data and sensitive information in your cluster.

🌟 🔥 If you want to switch your career into tech and you are considering DevOps, you can join our online community here for live classes and FREE tutorial videos.

Top comments (0)