ConfigMap

ConfigMap

  • ConfigMaps in Kubernetes solves the problem of managing configuration data in a flexible and efficient way.

  • Configmap is an API object which is mainly used to store non-confidential data.

  • API objects in Kubernetes are objects that can be manipulated using the Kubernetes API. Examples of API objects include Deployments, Services, ConfigMaps, and Pods.

  • The data that is stored in ConfigMap is stored as key-value pairs. ConfigMaps are configuration files that may be used by pods as command-line arguments, environment variables, or even as configuration files on a disc.

  • ConfigMap can be created and managed using the Kubernetes API. ConfigMaps can be created manually using the kubectl command-line tool, or programmatically using the Kubernetes API.

How Configuration data is managed before ConfigMap is Introduced?

Before ConfigMaps, managing configuration data in Kubernetes involved hard-coding configuration data into container images or passing configuration data as command-line arguments or environment variables. This made it difficult to manage configuration data separately from the application code and to update configuration data without rebuilding container images.

ConfigMaps can be created in several ways:

  1. Using the kubectl command-line tool: You can create a ConfigMap by running the kubectl create configmap command, which takes a set of key-value pairs or a configuration file as input.

     kubectl create configmap <map-name> <data-source>
    
  2. Using a YAML file: You can create a ConfigMap by defining a YAML file that specifies the key-value pairs or configuration data, and then using the kubectl apply -f command to create the ConfigMap.

     kubectl create configmap [configmap_name] --from-file [path/to/file]
    

    If you have multiple files then write like this:

     kubectl create configmap [configmap_name] --from-file [path/to/file1] --from-file [path/to/file2] --from-file [path/to/file3]
    

    You must specify the address of the YAML file containing the configuration data in the "path/to/file" field.

    YAML File could be like this:

     apiVersion: v1
     kind: ConfigMap
     metadata:
       name: my-config
     data:
       key1: value1
       key2: value2
    
  3. Build a ConfigMap From Directories: Like in the above method you specify file to build ConfigMap, but in this way you can specify the whole directory which contains configuration files.

    It is much better to specify only a directory rather than a list of files.

    You can create a single Configmap with all the files within the directories. Use the following command to create the configuration:

     kubectl create configmap [configmap_name] --from-file [path/to/directory]
    
  4. Using the ConfigMap generator in a Kubernetes manifest file: You can use the ConfigMap generator in a Kubernetes manifest file to automatically generate a ConfigMap from a set of key-value pairs or configuration files.

Features:

  1. Separation of configuration data: ConfigMaps provide a way to decouple configuration data from container images, allowing you to make changes to configuration without changing the container image.

  2. Key-value pairs or files: ConfigMaps can store data as key-value pairs or as files. You can use them to store environment variables, configuration files, or any other type of configuration data.

  3. Mounted as volumes: ConfigMaps can be mounted as volumes in containers, making the configuration data available to the application at runtime.

  4. Used for pods and deployments: ConfigMaps can be used to provide configuration data to individual pods or to all the pods in a deployment.

  5. ConfigMap as environment variables: You can also use ConfigMaps to create environment variables in your containers. This allows you to access the configuration data directly as environment variables.

  6. Dynamically updated: ConfigMaps can be updated dynamically by using Kubernetes API or kubectl command, which makes it easy to make changes to configuration data without having to redeploy the entire application.

Kubernetes Secrets vs ConfigMaps

If you don't know about secrets, then you can simply understand them by saying that they are a safe way to store private data such as API keys, passwords, and certificates.

  1. Use Secrets for things that are actually secret like API keys, credentials, etc

  2. Use ConfigMaps for not-secret configuration data.

Another difference is that Secrets are always base64-encoded, which means that the data is obfuscated but not encrypted, while ConfigMaps store data in plain text format. This is because Secrets are designed to handle sensitive data that should not be exposed in plain text.

Overall, while Secrets and ConfigMaps are both used to store configuration data, they are designed for different types of data and have different security considerations.

Real Example where we use configmap

Let's say you have a web application that needs to read some configuration data at runtime. You can store this configuration data in a ConfigMap and then mount the ConfigMap as a volume in the pod running the web application.

If you need to change the configuration data, you can update the ConfigMap using the kubectl edit command or by creating a new ConfigMap with the updated data.

By using ConfigMaps in this way, you can update your application's configuration data without needing to rebuild or redeploy your container images. This can save you time and reduce the risk of errors or downtime that can occur during the deployment process.

So there you have it - ConfigMap Concept explained in simple terms.😊

🎉 We hope this blog post has given you a better understanding of ConfigMap.

As always, if you have any questions or comments, we'd love to hear from you. 💬 Thanks for reading 🐳🎉