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.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF