What is ๐œ๐จ๐ง๐Ÿ๐ข๐  ๐ฆ๐š๐ฉ and how to use it ?

The most asked Kubernetes Question in interviews:

ยท

2 min read

๐‚๐จ๐ง๐Ÿ๐ข๐ ๐Œ๐š๐ฉ๐ฌ in Kubernetes are a way to manage configuration data separately from the application code. They allow you to decouple configuration settings from your application's containers, making it easier to update configuration values without changing your application's code.

๐‚๐ซ๐ž๐š๐ญ๐ž ๐š ๐‚๐จ๐ง๐Ÿ๐ข๐ ๐Œ๐š๐ฉ:

Here's an example of a YAML file to create a ConfigMap:

 apiVersion: v1
 kind: ConfigMap
 metadata:
 name: my-config
 data:
 key1: value1
 key2: value2

Apply this YAML file using kubectl apply -f my-configmap.yaml, and it will create a ConfigMap named my-config with key-value pairs key1: value1 and key2: value2.

๐€๐œ๐œ๐ž๐ฌ๐ฌ๐ข๐ง๐  ๐‚๐จ๐ง๐Ÿ๐ข๐ ๐Œ๐š๐ฉ ๐ƒ๐š๐ญ๐š ๐Ÿ๐ซ๐จ๐ฆ ๐๐จ๐๐ฌ:

Your application code within the pod can access the ConfigMap data using various methods, such as reading environment variables,command-line arguments, or reading configuration files from the mounted volume.

โ˜‘๏ธ ๐€๐ฌ ๐ž๐ง๐ฏ๐ข๐ซ๐จ๐ง๐ฆ๐ž๐ง๐ญ ๐ฏ๐š๐ซ๐ข๐š๐›๐ฅ๐ž๐ฌ:

 apiVersion: v1
 kind: Pod
 metadata:
 name: my-pod
 spec:
 containers:
 - name: my-container
 image: my-image
 env:
 - name: KEY_FROM_CONFIGMAP
 valueFrom:
 configMapKeyRef:
 name: my-config
 key: key1

In this example, the value of the environment variable KEY_FROM_CONFIGMAP is set to the value of key1 from the my-config ConfigMap.

โ˜‘๏ธ ๐€๐ฌ ๐•๐จ๐ฅ๐ฎ๐ฆ๐ž๐ฌ:

You can also use ConfigMaps as volumes to inject configuration files into pods. This is useful when you need to provide configuration files to applications. Here's an example:

 apiVersion: v1
 kind: Pod
 metadata:
 name: my-pod
 spec:
 containers:
 - name: my-container
 image: my-image
 volumeMounts:
 - name: config-volume
 mountPath: /etc/config
 volumes:
 - name: config-volume
 configMap:
 name: my-config

In this case, the contents of the my-config ConfigMap will be mounted as files in the /etc/config directory in the pod.

They help you keep your configuration separate from your application logic, making it easier to manage, update, and maintain your containerized applications.

ย