Post

Kubernetes - ConfigMaps

Lab assignment for creating configmaps using kubectl

Kubernetes - ConfigMaps

Prerequisites

  • Kubernetes
  • kubectl

Assignment

  • Lets start by creating a configmap to pass the environmental variables to the pod.

Job

1️⃣ Create a ConfigMap with two entries using the –from-literal parameter.

1
kubectl create configmap colour-configmap --from-literal=COLOUR=red --from-literal=KEY=value
1
kubectl describe configmap/colour-configmap
1
kubectl delete configmap/colour-configmap

2️⃣ Use an alternative approach for creating the same ConfigMap.

1
2
3
4
cat <<EOF > configmap-colour.properties
COLOUR=green
KEY=value
EOF
1
cat configmap-colour.properties
1
kubectl create configmap colour-configmap --from-env-file=configmap-colour.properties
1
kubectl describe configmap/colour-configmap

3️⃣ Create a pod that dumps environmental variables and sleeps for infinity. Review the environmental variables dumped by the pod.

1
kubectl run --image=ubuntu --dry-run=client --restart=Never -o yaml ubuntu --command bash -- -c 'env; sleep infinity' | tee env-dump-pod.yaml
1
kubectl apply -f env-dump-pod.yaml
1
kubectl get pods
1
kubectl logs ubuntu

4️⃣ Update the pod definition to include the configmap using envFrom.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cat <<EOF > env-dump-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: ubuntu
  name: ubuntu
spec:
  containers:
  - command:
    - bash
    - -c
    - env; sleep infinity
    image: ubuntu
    name: ubuntu
    resources: {}
    envFrom:
    - configMapRef:
        name: colour-configmap
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
cat <<EOF > calculatepi.yaml
apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  name: calculatepi
spec:
  completions: 20
  parallelism: 5
  template:
    metadata:
      creationTimestamp: null
    spec:
      containers:
      - command:
        - perl
        - -Mbignum=bpi
        - -wle
        - print bpi(2000)
        image: perl:5.34.0
        name: calculatepi
        resources: {}
      restartPolicy: Never
status: {}
EOF
1
kubectl apply -f env-dump-pod.yaml

5️⃣ The above command will fail as we can only update certain fields of a pod. Recreate the pod with the configmap.

1
kubectl delete -f env-dump-pod.yaml --now; kubectl apply -f env-dump-pod.yaml
1
kubectl logs ubuntu

6️⃣ Edit the configmap and change the color to red. Recreate the pod and verify the changes.

1
kubectl edit configmap/colour-configmap
1
kubectl delete -f env-dump-pod.yaml --now && kubectl apply -f env-dump-pod.yaml
1
kubectl logs ubuntu

7️⃣ Make the configmap immutable and update the color to purple. Try changing the color to yellow and confirm that if fails are configmap is immutable.

1
kubectl edit configmap/colour-configmap
1
kubectl delete -f env-dump-pod.yaml --now && kubectl apply -f env-dump-pod.yaml
1
kubectl logs ubuntu

8️⃣ Delete the pod and configmaps.

1
kubectl delete pod/ubuntu configmap/colour-configmap --now
1
rm configmap-colour.properties env-dump-pod.yaml
This post is licensed under CC BY 4.0 by the author.