Kubernetes YAML files: The basics

2
2545
Kubernetes YAML files
Kubernetes YAML files

Hello guys! i am sorry i couldn’t write new articles the last days cause i was busy with my common life problems etc. So today, We have a new articles series to master the Kubernetes YAML files one time for all and to do that we will start with the basics of YAML and why we should use a YAML file rather than commands to deploy our pods or deployments etc… Let’s go!

So, first of all

Why YAML files are better for Kubernetes definitions?

Using YAML for K8s gives you a number of advantages for example, you will have no need to add your parameters to your commands (no long commands) also, you will be able to create a more complex structures with keeping your work more organized and flexible without forgetting that you can add your YAML files to the source control to keep the track of changes.

What is YAML?

Simply, YAML is a human-readable text-based format for specifying configuration-type information and we use it on Kubernetes to define or specify the behavior of our objects, also i should mention that if you are good with JSON you will not have any problem with understanding a YAML file.

Nothing is complicated about a YAML file, cause there is only two types of structures that you should know which are Lists and Maps.

YAML Maps

A YAML map is simply a name-value definition pairs, it’s the equivalent of objects in JSON. you can see the example below:

apiVersion: v1
kind: Pod

Here, apiVersion and kind are the keys or the names and v1 and Pod are the values.

Note:

The quotation marks are optional cause the processor can tell that you’re looking at a string based on the formatting.

We can write a little more complicated definitions by giving a map as a value for a key:

apiVersion: v1
kind: Pod
metadata:
  name: my-site
  labels:
    app: web

Here the key metadata has a map as a value with two keys which are name and labels and the last key itself has a map as a value with app as key and web as a value. Here we should know that the starts and ends of maps are specified with indentations, as you can see the second and third maps ( with name and labels keys and app key ) is indented by two spaces to tell the processor that those are new maps.

To understand this more you can see the equivalent JSON for the last YAML file:

{
  "apiVersion": "v1",
  "kind": "Pod",
  "metadata": {
               "name": "my-site",
               "labels": {
                          "app": "web"
                         }
              }
}

I think all is clear now about the maps,so let’s move to the Lists.

YAML lists

A YAML list is the equivalent of a JSON arrays and it can take any number of items or maps of course as you can see below:

ourList:
  - firstItem
  - "500"
  - ThirdItem
  - "Hello lists!"

As you can see here, lists are defined as items that start with a dash (-) indented from the parent. The equivalent JSON is below:

{
   "ourList": ["firstItem", "500", "ThirdItem", "Hello lists!"]
}

Let’s see an example with lists and maps together:

  
containers:
    - name: frontend1
      image: nginx
      ports:
        - containerPort: 80
    - name: frontend2
      image: nginx
      ports:         
       - containerPort: 81

Here we have two lists of containers each one contains three maps which with the keys: name and image and ports and the last one is a list which contains a map with one key which is containerPort.

This is the JSON equivalent if you didn’t uderstand it well:

      
 "containers": [{
                       "name": "frontend1",
                       "image": "nginx",
                       "ports": [{
                                  "containerPort": "80"
                                 }]
                      }, 
                      {
                       "name": "frontend2",
                       "image": "nginx",
                       "ports": [{
                                  "containerPort": "81"
                                 }]
                      }]

 

So here we are, we reaching the end of this article were we got the basics of YAML which are the definitions of a map and a list and how to create more complex structures with them.

On rur next article, we will focus on the YAML file of Kubernetes Pods, the maps that we can define and the values that they can take. See you!

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here