configmap

ConfigMap

    ConfigMap in the Kubernetes will help you to create an environment variable or properties and then you can access the environment variable inside the pod.

Environment Variable

apiVersion: v1
kind: Pod                                        
metadata:
  name: podsdemo   
spec:
  containers:
  - image: iam7hills/learnkubernetes:podsdemo-1.0 
    name: podsdemo
    ports:
    - containerPort: 80
    env:
      - name: environment
        value: TEST

kubectl exec -it podsdemo /bin/sh
/ # echo $environment
TEST

1. ConfigMap - Using Imperative

In the below command, configmapdemo_key is the key and configmapdemo_value is the value. You can also have multiple literals in the same command.
kubectl create cm configmapdemo --from-literal configmapdemo_key=configmapdemo_value
configmap/configmapdemo created

kubectl get cm
NAME               DATA   AGE
configmapdemo      1      5s

kubectl describe cm configmapdemo
Name:         configmapdemo
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
configmapdemo_key:
----
configmapdemo_value
BinaryData
====
Events:  <none>

2. ConfigMap - Using Declarative/Manifest
configmap.yaml
---------------------
apiVersion: v1
kind: ConfigMap
apiVersion: v1
metadata:
  name: configdata
data:
  configmapdemo_keyconfigmapdemo_value

How to Inject CONFIGMAP into POD MANIFEST
apiVersion: v1
kind: Pod
metadata:
  name: podsdemo
spec:
  containers:
  - image: iam7hills/learnkubernetes:podsdemo-1.0
    name: podsdemo
    ports:
    - containerPort: 80
    env:
      - name: ENVIRONMENT  # this is the environment variable that you need to echo
        valueFrom:
          configMapKeyRef:
            name: configmapdemo # This should match with metadata of config manifest
            key: configmapdemo_key

kubectl exec -it podsdemo /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # echo $ENVIRONMENT
configmapdemo_value

3. USING VOLUMES
    Let us assume, if you want to use the environment variable across all the containers in the same pod, then you can use the volume concept to retrieve the config variable in your containers.

apiVersion: v1
kind: Pod
metadata:
  name: podsvolumedemo
spec:
  volumes:
  - name: volumemap
    configMap:
      name: configmapdemo # This should match with config yaml/manifest
  containers:
  - image: iam7hills/learnkubernetes:podsdemo-1.0
    name: demo
    imagePullPolicy: Always
    volumeMounts:
    - name: volumemap
      mountPath: /iam7hills

kubectl exec -it podsvolumedemo /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # cd /iam7hills
/iam7hills # ls -lrt
total 0
lrwxrwxrwx    1 root     root            24 Dec 28 07:39 configmapdemo_key -> ..data/configmapdemo_key
/iam7hills # cat configmapdemo_key
configmapdemo_value