Day-41: ConfigMap in Kubernetes

Kubernetes is a powerful platform that automates the deployment, scaling, and operation of application containers. One of the key components that make Kubernetes so versatile is ConfigMap. It helps in managing configuration data separately from application code, allowing you to easily update configurations without rebuilding container images.

What is a ConfigMap?

A ConfigMap in Kubernetes is an object used to store non-confidential configuration data in key-value pairs. It allows you to inject configuration data into your application’s containers, ensuring that your application remains environment-agnostic and flexible.

Real-Life Example in Hindi:

maan leejie ki aapake paas ek veb eplikeshan hai jo vibhinn paryaavaran (jaise vikaas, pareekshan, utpaadan) ke anusaar vibhinn detaabes se kanekt hota hai. agar aap detaabes kee jaanakaaree ko apane kod mein haardakod karate hain, to har baar jab aap paryaavaran badalenge, to aapako apane kod mein bhee badalaav karana padega. isake bajaay, aap chonfigmap ka upayog kar sakate hain aur apane eplikeshan ko vibhinn chonfigmap se kanekt kar sakate hain, jisase aapaka eplikeshan adhik portebal ho jaega.

Types of ConfigMaps

Kubernetes provides several methods to create and utilize ConfigMaps, each serving different needs:

  1. Literal Value ConfigMap: Directly create ConfigMap using key-value pairs via command line.

  2. File-Based ConfigMap: Create a ConfigMap from a file where each line contains a key-value pair.

  3. Directory-Based ConfigMap: Create a ConfigMap from a directory, with each file representing a key-value pair.

  4. Environment Variable ConfigMap: Use ConfigMap to populate environment variables in your containers.

Example: Creating and Using ConfigMaps

Here’s how you can create and use different types of ConfigMaps.

1. Literal Value ConfigMap

kubectl create configmap app-config --from-literal=APP_ENV=production --from-literal=APP_DEBUG=false

This command creates a ConfigMap named app-config with two key-value pairs: APP_ENV=production and APP_DEBUG=false.

2. File-Based ConfigMap

First, create a file named config.properties with the following content:

DB_HOST=localhost
DB_PORT=5432

Now, create a ConfigMap from this file:

kubectl create configmap db-config --from-file=config.properties

3. Directory-Based ConfigMap

Suppose you have a directory named config/ with two files:

  • db_host: contains localhost

  • db_port: contains 5432

You can create a ConfigMap from this directory with:

kubectl create configmap dir-config --from-file=config/

4. Environment Variable ConfigMap

To inject ConfigMap data as environment variables into your pods, modify your pod manifest like this:

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: app-container
    image: my-app-image
    envFrom:
    - configMapRef:
        name: app-config

Verifying the ConfigMap

Once you’ve created the ConfigMap, you can check its contents with:

kubectl describe configmap app-config

To ensure the environment variables are correctly set in your pod, you can execute:

kubectl exec -it my-app -- printenv | grep APP_ENV

Conclusion

ConfigMaps are an essential part of Kubernetes, enabling you to separate configuration from code. This separation allows for more flexible and scalable applications, especially in cloud-native environments. By understanding and using ConfigMaps, you can ensure that your applications are easy to manage and can seamlessly adapt to different environments.

Connect and Follow Me on Socials

LINKDIN | GITHUB |TWITTER

1
Subscribe to my newsletter

Read articles from priyadarshi ranjan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

priyadarshi ranjan
priyadarshi ranjan

Greetings! 👋 I'm Priyadarshi Ranjan, a dedicated DevOps Engineer embarking on an enriching journey. Join me as I delve into the dynamic realms of cloud computing and DevOps through insightful blogs and updates. 🛠️ My focus? Harnessing AWS services, optimizing CI/CD pipelines, and mastering infrastructure as code. Whether you're peers, interns, or curious learners, let's thrive together in the vibrant DevOps ecosystem. 🌐 Connect with me for engaging discussions, shared insights, and mutual growth opportunities. Let's embrace the learning curve and excel in the dynamic realm of AWS and DevOps technology!