DEV Community

Cover image for K8s Exercise: ConfigMaps
Akshay Rao
Akshay Rao

Posted on • Edited on

K8s Exercise: ConfigMaps

Introduction
Hi, I am Akshay Rao, will be starting a exercise series on k8s.
In this blog there will not explanation only problems and solutions.if you want explanation have a look at this series:-
https://dev.to/aksrao1998/series/24887

Pre-requisite
have minikube or kind running in the local machine.

Note:- k is alias for kubectl.

Let's Start

Problem 1
Create a configMap called 'options' with the value var1=val1. Create a new nginx pod that loads the value from variable 'var5' in an env variable called 'option'

Solution

# create a configmap
[k8s-ckad (⎈|minikube:hands-on)]$ k create configmap options --from-literal=val1=val1 --dry-run=client -o yaml > options.yaml

[k8s-ckad (⎈|minikube:mynamespace)]$ k create -f options.yaml 
configmap/options created
[k8s-ckad (⎈|minikube:mynamespace)]$ k get cm
NAME               DATA   AGE
kube-root-ca.crt   1      3d19h
options            1      5s

# create a pod
[k8s-ckad (⎈|minikube:mynamespace)]$ k run nginx --image=nginx --dry-run=client -o yaml > pods.yaml

#edit the pods.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
  namespace: mynamespace 
spec:
  containers:
  - image: nginx
    name: nginx
    env:
      - name: options
        valueFrom:
          configMapKeyRef:
            name: options
            key: val1
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

[k8s-ckad (⎈|minikube:mynamespace)]$ k create -f pods.yaml 
pod/nginx created
[ts-akshay.rao@JP-FVFZ91DHL414 k8s-ckad (⎈|minikube:mynamespace)]$ k get po
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          3m53s

#verify

[k8s-ckad (⎈|minikube:mynamespace)]$ k exec -it nginx -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=nginx
TERM=xterm
options=val1
Enter fullscreen mode Exit fullscreen mode

Problem 2
Create a configMap 'anotherone' with values 'var2=val2', 'var3=val3'. Load this configMap as env variables into a new nginx pod

Solution

# create another cm
[k8s-ckad (⎈|minikube:mynamespace)]$ k create cm anotherone --from-literal=val2=val2 --from-literal=val3=val3
configmap/anotherone created

#edit the pods.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
  namespace: mynamespace 
spec:
  containers:
  - image: nginx
    name: nginx
    envFrom:
    - configMapRef:
        name: anotherone
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

[k8s-ckad (⎈|minikube:mynamespace)]$ k create -f pods.yaml 
pod/nginx created
[k8s-ckad (⎈|minikube:mynamespace)]$ k get po
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          35s

# verify
[k8s-ckad (⎈|minikube:mynamespace)]$ k exec -it nginx -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=nginx
TERM=xterm
val2=val2
val3=val3
Enter fullscreen mode Exit fullscreen mode

Problem 3
Create a configMap 'cmvolume' with values 'var4=val4', 'var5=val5'. Load this as a volume inside an nginx pod on path '/etc/lala'. Create the pod and 'ls' into the '/etc/lala' directory.
Solution

[k8s-ckad (⎈|minikube:mynamespace)]$ k create cm cmvolume --from-literal=val4=val4 --from-literal=val5=val5
configmap/cmvolume created

# edit the pods.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
  namespace: mynamespace 
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMoints:
    - name: cm-volume
      mountPath: /etc/lala
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  volumes:
    - name: cm-volume
      configMap:
        name: cmvolume
status: {}

[k8s-ckad (⎈|minikube:mynamespace)]$ k create -f pods.yaml 
pod/nginx created

# verify
[k8s-ckad (⎈|minikube:mynamespace)]$ k exec -it nginx bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx:/# cd /etc/lala
root@nginx:/etc/lala# ls
val4  val5

Enter fullscreen mode Exit fullscreen mode

Top comments (0)