YAML Files and Pods

Let’s get started with creating a pod in Kubernetes

Aman Arham
Better Programming

--

Photo by Jonathan Farber on Unsplash.

Now that we have covered the architecture and general concepts of Kubernetes, along with getting you set up with a Kind cluster, it is time to create a pod the hard way (with YAML files). As mentioned before, pods are groups of containers that run the container images. In these pods, a container image is required. These are obtained from the Docker Hub page.

Let’s get started with creating a pod in Kubernetes.

YAML Files

Before creating a pod, you need to understand the general idea behind YAML files. Usually, JSON files are used, but YAML is new, much more efficient, and easier to use.

One of the main concepts of YAML files is key-value pairs. In essence, a key (e.g. food) is assigned to a value (e.g. pasta). The two values are a key-value pair. They represent one another! Here are some examples of key-value pairs in YAML format:

Fruit: Apple
Vegetable: Carrot
Liquid: Water
City: Austin

As you can see, the format is key: value. One important thing to note is the space between the key and value. If there is no space after the :, the key-value pair will not be recognized. This is one of the main components of a YAML file.

The other component is an array/list. Based on the name, you can tell that the main purpose of this function is to list many different attributes of the YAML file, whether it be a key-value pair or a dictionary/map. Here is an example of an array/list in YAML format:

Cities:
- Austin
- Dallas
- San Francisco
- New York

Based on this example, you can see that there is a category followed by a `:`. After that, a list is created with dashes and the list values are written. You may notice that the spacing is the same with all of the values in the list. This is something that must be kept in mind when creating arrays/lists in YAML files.

Now the final thing that I will be discussing in regards to YAML files is creating dictionaries. This is a mix of arrays/lists and key-value pairs. In general, the main purpose of dictionaries in YAML files is to store specific pieces of data that will be useful in certain cases (e.g. when creating services, pods, deployments, etc. in Kubernetes). Here is an example of a dictionary in YAML format:

Mango:
calories: 105
fat: 0.4g
carbs: 28g

As you can see, the items in the dictionary are all indented at the same level. This is very important! YAML is very picky when it comes to this matter. If the dictionary has many different elements, remember to indent them on the same level. Otherwise, you will get an error.

Creating a Kubernetes Pod

Now that we have gone over the basics of YAML files in the context of Kubernetes, we will create a pod. Remember? It’s a group of containers running the same image and configurations.

Now we will start off with… you guessed it! A YAML file. These YAML files are called definition files since they define the service that you are deploying. Since we are deploying a pod, we will be using a pod definition file.

Before writing the file, we will need to create the file:

vi pod-def.yaml

After running this command, you should see a window that is similar to this:

Don’t worry! This page is just showing you that you are ready to edit the file and put the YAML code in.

Here is the YAML file we will be using:

This is the pod definition file. There are many aspects and components in this file. Don’t be overwhelmed. I will explain each element!

  • Let’s start with the apiVersion (key-value pair). This is used to clarify what API server and version you will be running in the background when creating the pod.
  • Next is the kind, which signifies the kind of definition file this is. In our case, it is a pod. When we learn about the other offerings in Kubernetes, there will be other kinds you will be able to experiment with.
  • Then there is the metadata, which is a dictionary including the item name. The metadata stores values that you can look at in more depth on the Kubernetes documentation page. In our case, when creating a Kubernetes pod, we have the name Pod.
  • spec is actually an array/list. There are certain specifications present in the pod. For a normal pod definition file, the only spec needed is containers, which is a list. We will need to specify the configurations of the containers that will be in the pod, so this part is vital. In the containers list, we will include the name of the container and the image name will be provided after. In this example, the name of the container will be nginx and we will use nginx:latest as our image name for the containers running in the pod.
  • Finally, there is a command section that signifies specific commands you will use. It is not necessary to fill out, but in some cases, the commands items may come in handy.

We are done with the pod definition file. Now click on Control + C, then :wq! to save and exit the file.

Use this command to create the pods based on the YAML file:

kubectl create -f pod-def.yaml

Once done, use this command to view the pod:

kubectl get pods

Conclusion

Yay! You created your first pod! You have taken one of many steps toward entering the realm of Kubernetes and its functionalities. I hope you are enjoying it thus far. We will be going over more information in the articles to come.

If you’d like to learn more and get some experimenting done, visit the Kubernetes documentation page below.

Thanks for reading.

--

--

Senior at Rick Reedy High School and aspiring Data Scientist; Writer for Better Programming.