Bobcares

Kubernetes Configmap Key value: Explained

by | Jul 22, 2022

Kubernetes configmap key value in which configmap allows the users to make programs portable, and isolate environment-specific settings from containers.

Bobcares answers all questions no matter the size, as part of our Server Management Service

Let us take a look at Kubernetes configmap key value in detail.

What is a Kubernetes configmap?

kubernetes configmap key value

A ConfigMap is a Kubernetes API object for storing data as key-value pairs. Kubernetes pods can use the ConfigMaps as Configuration file, the Environment variable, and Command-line argument.

ConfigMaps enables application portability by decoupling environment-specific configurations from containers. Importantly, ConfigMaps are not suitable for storing sensitive information. And, they do not provide any form of encryption. All data contained within them is visible to anyone with access to the file. Another consideration for ConfigMaps is file size. Application configuration ConfigMap files are limited to 1MB. And, separate file mounts, databases, or file services are preferable for larger data sets.

kind: ConfigMap
apiVersion: v1
metadata:
name: example-configmap
namespace: default
data:
# Configuration Values are stored as key-value pairs
system.data.name: "app-name"
system.data.url: "https://app-name.com"
system.data.type_one: "app-type-xxx"
system.data.value: "3"
# File like Keys
system.interface.properties: |
ui.type=2
ui.color1=red
ui.color2=green

The Data field stores the necessary information for a ConfigMap. There are two options for storing values:

  1. As distinct key pair properties
  2. They are fragments of a configuration format in a granular format. (File As Keys)

ConfigMaps and pods go hand in hand. ConfigMaps can be used in a Kubernetes pod as environment variables and configuration information.

  1. ConfigMap generation from directories
  2. ConfigMap generation from files
  3. ConfigMaps generation from an environment file
  4. ConfigMap creation from a file with a predefined key
  5. ConfigMap creation from values

Using ConfigMaps in pods

Create a simple ConfigMap and use it when creating a pod in Kubernetes to use the created ConfigMaps. Firstly, make a file called “app-basic.properties” that contains two key-value pairs.
app-basic.properties
system.type="TESTING CONFIGMAP"
system.number=12345

Then, using the above file and the -from-file option, create a ConfigMap named “app-basic-configmap.”

kubectl create configmap app-basic-configmap --from-file=configmap-example/app-basic.properties
cinfigmap/app-basic-configmap created

kubectl get configmap app-basic-configmap -o yaml
apiVersion:v1
data:
App-basic.properties:
system.type='TESTING CONFIGMAP"
system.number= 123456
kind: ConfigMap
metadata:
creationTimestamp:"2022-07-24T22:08:51Z"
mangeFields:
- apiversion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.:{}
f.app-basic.properties:{}
manager:kubect1-create
operation:update
"time:2022-0724T22:08:51Z"
name:app-basic-configmap
namespace:default
resourceVersion: "21173"
selflink: /api/v1/namsespaces/default/configmaps/app-basic-configmap
uid: acb51647-1c3d-4db1-9a5d-d8d8427b8286

Finally, make a Pod that uses the newly created ConfigMap. Then to create the Pod, use the YAML file shown below.

apiVersion: v1
kind: Pod
metadata:
name: configmap-example-pod
spec:
containers:
- name: configmap-example-busybox
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
# Load the Complete ConfigMap
- configMapRef:
name: app-basic-configmap
restartPolicy: Never

After that, load the entire ConfigMap created to the Kubernetes Pod, as shown in the preceding example.

kubectl create -f example-pod.yaml
pod/configmp-example-pod-created
kubectl get pods
NAME READY STATUS RESTARTS AGE
Configmap-exmaple-pod 0/1 Completed 0 109S

.kubectl logs configmap-example-pod | grep system.number
systme.number=12345

The above result shows that the ConfigMap “app-basic-configmap” went on a successful loading while creating the Kubernetes Pod.

Environment variables in a Kubernetes ConfigMap container as key value pairs.

It is important to note that this feature is available in Kubernetes versions 1.6 and later.
Firstly make a ConfigMap with multiple key-value pairs
apiVersion: v1
kind: ConfigMap metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm

After this, make the ConfigMap:

kubectl create -f https://kubernetes.io/examples/configmap/configmap-multikeys.yaml

Secondly, to define all of ConfigMap’s data as container environment variables, use envFrom. Then the ConfigMap key acts as the name of the environment variable in the Pod.

apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never

and finally, make the Pod:

kubectl create -f https://kubernetes.io/examples/pods/pod-configmap-envFrom.yaml

Therefore, the Pod’s output now includes environmental variables. SPECIAL_LEVEL=very and SPECIAL_TYPE=charm.

[Need assistance with similar queries? We are here to help]

Conclusion

To conclude, A ConfigMap is a Kubernetes API object that stores data in the form of key-value pairs.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.