Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism

Description

Learn how to create a ConfigMaps and use a Kubernetes pod within it with Civo Academy.


Transcription

Introduction

Hi, in this video, we'll be going through ConfigMaps. ConfigMaps lets you decouple your containerized image and provide different configurations to run in different environments.

Creating a ConfigMap

Let's create the ConfigMap. So, you can use the kubectl create configmap command to create the ConfigMap. There are various options that you can explore. Like, you can create it from a file. You can create it from a file with a key, from a literal, or the env file. So let's go through some of these. First, we will create a ConfigMap from the file. To create that, we will use the command kubectl create configmap test --from-file=file.prop, where test is the file's name.

Now, file.prop has nothing but just three fundamental values. So let's see what comes up in the ConfigMap. To see that, use the command kubectl describe configmap test. So you can see it takes data as the name of the file and then pulls in all the data in the file.

Now sometimes, what you want is maybe you have multiple key values in a file, and you want it as separate data. So you can use something called an env file. Run the command, kubectl create configmap test2 --from-file=env.prop. And now, if I use the command kubectl describe configmap test2, we can see that we have different data, env, location, and version.

Now, let's create a ConfigMap using literal. First, use the command kubectl create configmap demo --from-literal=name=saiyam. Then run the command, kubectl get configmap, and you will see the creation of a ConfigMap from literal.

Using a pod in a configmap

Now, I have a pod. Now, how to use ConfigMap in a pod. So you need to have a basic YAML file. In that file, you will have apiVersion, kind, metadata with the labels, and a spec section containing the container with the image as busybox. So in the container, it'll list out all the environment variables. And in the environment section, we are naming the environment variable HELLO, and we are taking its value from the ConfigMap key reference. So the name of the ConfigMap will be demo, and the key that we want to refer to is name.

So, we should get env HELLO with the same value. To see if that happens, run the command, kubectl create -f demo.yaml. You will see that the creation of the pod is completed, and it is running. By running the command, kubectl logs demo, you can see that HELLO=saiyam is printed.

So we got our ConfigMap inside the pod. And now, what we'll be doing is we'll be creating a ConfigMap, and we'll be mounting it as a volume inside a pod. This process is also another way of creating a ConfigMap. So you have a YAML file, apiVersion, kind, metadata, and namespace, which has the following data. So these should be two files created when we mount as a volume.

So, we will now see how to do that. In the container section, you have volumes. So in the volume, we specify the ConfigMap that we want to mount inside the container and the volume's mount path. So let's create the ConfigMap by using the command kubectl create -f cm-for-volume.yaml. Next, let's create the pod using the command kubectl create -f demo2.yaml. You will see that the pod is created and is completed. Now, let's see the logs using the command kubectl logs cm-volume. You can see that file1 and file2 are present over there. So they get mounted as files onto the pod, that specific directory inside the folder.

Conclusion

So you can mount a ConfigMap as volume. You can use ConfigMap as an environment variable. You can create it using different yaml files, environment files, and you can also create a ConfigMap using literal. That's it for this lecture. I'll see you at the next one.

Don't stop now, check out your next lesson