Tutorial: Learn how to create a ConfigMap in k8s now! 🤔

In this post, we’ll dive into a Kubernetes practice question scenario that requires you to create a ConfigMap in Kubernetes (k8s) containing two environment variables within a specific namespace.

The following is the second mock Kubernetes Practice Exam question and is taken from the Practice Exam for Certified Kubernetes Application Developer (CKAD) Certification on Matthew Palmer’s website and reads as follows:

All operations in this question should be performed in the ggckad-s2 namespace.
Create a ConfigMap called app-config that contains the following two entries:

  • ‘connection_string’ set to ‘localhost:4096’
  • ‘external_url’ set to ‘google.com’

Run a pod called question-two-pod with a single container running the kubegoldenguide/alpine-spin:1.0.0 image, and expose these configuration settings as environment variables inside the container.

Let’s review the steps required to achieve this outcome in the next section.

This article was updated on May 28, 2025.

Learn how to create a ConfigMap in Kubernetes with two environment variables in five steps

In this section we’ll assign two environment variables in a k8s ConfigMap and then we’ll test our result.

Article Prerequisites

Refer to the article prerequisites for details about technology and setup instructions required to answer this question.

Step One: Create the ggckad-s2 namespace.

We create the ggckad-s2 namespace as follows:

				
					k create namespace ggckad-s2
				
			

We can see the expected output in the following image.

The result of executing 'kubectl create namespace ggckad-s2' shows that the namespace/ggckad-s2 namespace has been created in minikube.
Question Two, Step One: Create the ggckad-s2 namespace in minikube.

Step Two: Create a ConfigMap in Kubernetes.

In the second step we need a ConfigMap, which can be created via the CLI as follows:

				
					kubectl create configmap app-config --from-literal connection_string=localhost:4096 --from-literal external_url=google.com --dry-run -oyaml --namespace ggckad-s2
				
			

and which is defined in the following gist (see: app-config.yaml):

				
					apiVersion: v1
data:
  connection_string: localhost:4096
  external_url: google.com
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: app-config
  namespace: ggckad-s2
				
			

Note that the connection_string on line three and external_url on line four are environment variables that will be set in k8s.

See the image below for an example regarding what this command should produce.

Use the kubectl command to successfully create a ConfigMap in Kubernetes named app-config in the ggckad-s2 namespace. This configuration map includes two key-value pairs: connection_string set to localhost:4096 and external_url set to google.com. The command is run in dry-run mode and outputs the result in YAML format.
Question Two, Step Two: Use the kubectl command to successfully create a ConfigMap in Kubernetes named app-config which is in the ggckad-s2 namespace and which contains two environment variables.

Step Three: Apply the Kubernetes ConfigMap configuration file.

Next, we can apply the Config Maps configuration making sure to include the specified namespace:

				
					k apply -f ./app-config.yaml --namespace ggckad-s2
				
			

We can see the expected output in the following screenshot:

The result of executing 'kubectl apply -f ./app-config.yaml --namespace ggckad-s2' shows that pod/question-two-pod ConfigMap configuration has been created.
Question Two, Step Three: Setting environment variables in Kubernetes happens via the app-config.yaml file in this example and we can see that the question-two-pod pod has been created.

Step Four: Apply the pod configuration file.

The Kubernetes pod configuration file for the question-two-pod appears below (see question-two-pod.yaml):

				
					apiVersion: v1
kind: Pod
metadata:
  name: question-two-pod
  labels:
    role: myrole
spec:
  containers:
    - name: web
      image: kubegoldenguide/alpine-spin:1.0.0
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
      envFrom:
        - configMapRef:
            name: app-config
				
			

We need to apply the pod configuration file, and we do so via the following command:

				
					k apply -f ./question-two-pod.yaml --namespace ggckad-s2
				
			

Below I’ve included a copy of what the output should look like when this command returns successfully.

The result of executing 'kubectl apply -f ./question-two-pod.yaml --namespace ggckad-s2' shows that pod/question-two-pod has been created.
Question Two, Step Four: The pod/question-two-pod pod has been created.

Finally, we need to verify our work.

Step Five: Verify the solution by inspecting the container environment variables.

Verifying this solution is simple: First we’ll get CLI access to the container and then we’ll check the environment, if the environment variables have been set then we’re finished.

Execute the first step on the command line as follows:

				
					k exec -it question-two-pod -c web --namespace ggckad-s2 -- /bin/sh
				
			

Next we’ll use the env command and visually inspect the results.

Question 2 Answer Verification with pointers to the connection_string and external_url environment settings with the expected values assigned to each.
Question 2 Answer Verification with pointers to the connection_string and external_url environment settings.

The yellow pointers indicate that the connection_string and external_url environment variables have the correct values assigned in k8s so we’re now finished with this question.

Tutorial Conclusion

I hope this guide has been helpful in understanding how to create a ConfigMaps with two environment variables — a requirement that appears with some frequency when it comes to developing Kubernetes microservices.

See Also

Other tutorials focusing on aspects of Kubernetes can be found below.

  1. Learn how to use kubectl to get the cluster name in this guide.
  2. Tutorial: Learn how to quickly install the minikube Metrics Server in five easy steps!
  3. Learn how to mount a local directory in minikube by exploring three possible solutions.
  4. Example One: Learn how to create a new pod in Kubernetes now!
  5. Example Three: Set user IDs and file system group IDs in containers using the Kubernetes SecurityContext.
  6. Example Four: Learn how to configure Kubernetes health checks using liveness and readiness probes.
  7. Example Five: Learn How To Create A Deployment With Kubernetes Replicas Now!

Frequently Asked Questions

This section includes some frequently asked questions that may come up when software engineers are learning how to create a ConfigMap in Kubernetes and other closely related topics.

What is the hostPath in Kubernetes?

A hostPath in Kubernetes (k8s) is a volume type that mounts a specific directory or file from the host node filesystem, such as /etc, directly into a container thereby allowing the container to access or store data on the node.

The k8s hostPath is useful for scenarios such as logging or local storage.

One risk that comes from the use of the k8s hostPath is that it ties the container to a specific node and hence this impacts portability.

What is a Kubernetes ConfigMap?

A Kubernetes ConfigMap is an object that stores configuration data structured in key-value pair format thereby allowing the software developer the ability to inject environment-specific configuration details, such as environment variables or paths to configuration files, into containers without needing to hardcode these values in application code.

What is the use of ConfigMap in Kubernetes?

Kubernetes Config Maps are used to store configuration data as key-value pairs thereby allowing containers to access environment-specific settings without needing to hardcode these values into application code.

author avatar
ThosPFuller
I am a software engineer based in Northern Virginia (USA) and this website focuses on content engineering, web development, Technical SEO, Search Engine Optimization (SEO).

Leave a Reply