YAML for docker and kubernetes : The basics

YAML for docker and kubernetes : The basics

You can get all the files on my GitHub Repo: Saad/DevOps-Material

What is YAML Exactly?

  • YAML is like the A, B, C, and D of DevOps as it is needed in Kubernetes, Cloud computing and many different softwares which are used in the field of DevOps daily.

  • It is very similar to JSON and XML. YAML file extension can be .YAML or .YML, both work.

  • Previously the Full form of YAML was ‘Yet Another Markup Language’ but now it has been changed to ‘YAML Ain’t Markup Language’. Since it is not just a markup language anymore.

  • YAML is not a programming language but a Data format (like JSON or XML) which is used to exchange data in a structured and human-readable format.

  • YAML can only store data and not commands.

  • It avoids data duplication by using the same file for multiple projects on different mediums.

  • YAML is very useful for carrying data obtained from Data serialization and deserialization.

What are Serialization and Deserialization you ask?

Let's assume you have 3 different projects on 3 different mediums but you need them all to consume the same data without replicating the data by making specific data for all the projects.

This is where these two processes come into action.

  • Serialization is a process of converting the data objects into a complex data structure which can be then deserialized and used as per our needs.

Where is it Used?

  • It is used in Configuration files for docker, Kubernetes, etc

  • It is also used in caches, logs, etc.

  • We can assume that it is fit for use where ever data transfer among the medium is needed, it can even replace XML and JSON for that instance.

Benefits of YAML:

  • It is human-readable and very easy to read.

  • It has a strict syntax that ensures the decided constant convention is followed. Indentation matters a lot.

  • Easily convertible to JSON and XML.

  • YAML is used by many popular programming languages and software

  • YAML is proven to be very powerful when representing complex data

After the basic intro for YAML we now move on to one of the most important part, doing it practically!

Basics of YAML:

The Characters on the left are Keys and on the right, we have value of that respective key.

#Key value Pairs
"Car" : "A vehicle with four wheels"
18 : "Legal age"

Colon (:) plays a very important role in YAML, It is used in almost every single instance.

To make a Simple list in YAML, we use a dash/hyphen (-) followed by a space.

#List
- Bugatti
- Tesla
- Honda
- Toyota

#With a key
Fruits:
- Apple
- Mango
- Plum

If there is even a single indentation out of place it will render the whole list invalid.

#Incorrect Syntax for List
#Identation is very important, that is why this list is incorrect.
- Bugatti
  - Tesla
- Honda
- Toyota

To save the lines and for better readability, we can resort to another way of representing a list.

#Another Valid way to write Lists
Fruits : [Mango, Apple, Plum]

--- : (Three dashes) are used to separate documents within a stream.

... : (Three Dots) Represents the end of the document.

DataTypes in YAML

Like python, we don’t need to explicitly mention the datatype, this is automatically done by YAML, but we can also mention the data type if we desire to do so.

Strings

#Three Ways to represent String value
Name : "Saad Mansuri"
Age : Twenty-Two
Country : 'India'

#Mutli-Line. All the sentence on a different line
Intro : |
This is an intro to YAML.
A Data Format which is Similar to JSON and XML
Used to transfer Data to multiple projects

#Writing a Single Line on multiple lines
SingleLineString: >
All of this
Will be
on a single line.

#Specifying the datatype
ExplicitString : !!str "I am clearly a string!"

Integer

One : 1

#Specifying the data type
One : !!int 1
negative : !!int -10
exponentials : 3.03E45
NumbersWithComma : !!int +12_300 #12,300

Floats

Profitpercent : !!float 200.34
not A Number : .nan

Null

Null can be represented as Null, null, ~ (Tilde).

Missing Data : !!null Null
~ : Tilde also represents null

Boolean

FalseStates : !!bool false
TrueState : !!bool true

Advance DataTypes

Sequence

Just like we have lists in other programming languages, we have Sequences in YAML.

A Sequence with an empty element is known as a ‘Sparse Sequence’.

The data type is ‘seq’.

car : !!seq
- Colour
- Wheels
- Price
- Name

#Sparse Sequence
#Sequence with empty element(s) are called Sparse Sequence
Free Car: !!se
- Colour
- Wheels
-
- Name

Nested Sequence

A Sequence inside a sequence is A nested sequence.

Be very careful with the indentations!

-
 - Cars
 - Bikes
 - Scooters
 - Trucks
-
 - Keyboard
 - Mouse
 - Monitor
 - CPU

It's JSON representation will be

[
  [
    "Cars",
    "Bikes",
    "Scooters",
    "Trucks"
  ],
  [
    "Keyboard",
    "Mouse",
    "Monitor",
    "CPU"
  ]
]

Maps

Key-value pairs are called maps

Datatype is ‘map’

#Nested Maps
Vehicle : Cars
parts :
Wheels : "Plain"
Colour : "Black"
Price : 100000
Owner : ~

#Another way to Represent
parts : {Wheels : "Plain",Colour : "Black",Price : 100000,Owner : ~}

Pairs

Pairs can have duplicate keys

Datatype is ‘pairs’

pair example: !!pairs
- Name : David
- Name : David
- Name : John

#Another way to represent
pair example: !!pairs [ Name : David, Name : David, Name : John]

Set

A set cannot have a duplicate key

Datatype is ‘set’

people : !!set
? David
? Mary
? John

Anchors

#Anchors for Reusing properties

Parts : &parts
Body: true
wheels : plain


Car :
Name : "Honda"
<<: *parts


#Car is Same as
Car :
Name : "Honda"
Body: true
wheels : plain

#Some or All Properties can be overriden by explicitly mentioning new value for the key
Car2 :
Name : "Toyota"
<<: *parts
wheels : "Graphics"

Try this out on your own and you can also check the validity of your YAML file here

This is it for today, this is enough for someone who is just starting with docker and Kubernetes. We will explore more ways to use YAML along the way.

I am doing the DevOps series from beginners to advance. Tag along and let's learn together!

You can get all the files on my GitHub Repo: Saad/DevOps-Material