YAML fundamentals for Kubernetes

YAML is a human friendly data serialization standard for all programming languages. It is a simple, text/human-readable annotation format that can be used to store/represent data.

We don’t really need to know the entire set of complex YAML syntax in order to work with the K8S YAML object definition files.

Once we grasp the below concepts, we are good to go:

Key-Value pairs/Maps:

Represented by key: value (mind the space after the colon).

Any one such pair is nothing but a dictionary at its simplest form (single element dictionary). More than one such pairs will form a multi-element dictionary.

YAML

Fruits: Banana 
Vegetables: Carrot

Interpretation
{‘Fruits’: ‘Banana’, ‘Vegetables’: ‘Carrot’}
It is a Dictionary with two key: value pairs.

Arrays/Lists:


Every individual element of a list/array starts with a – (dash) followed by a space. Arrays can be used as values within a dictionary.

YAML

- Banana
- Carrot

Interpretation
[‘Banana’, ‘Carrot’]

YAML

Fruits:
 - Banana
 - Orange
 - Apple

Interpretation

{‘Fruits’: [‘Banana’, ‘Orange’, ‘Apple’]}

It is a dictionary with a single key: value pair. The value being an array with 3 elements.

It is possible to have a complex combination of one or more Lists and/or Maps in order to form:

I. maps of maps
II. maps of lists
III. lists of lists
IV. lists of maps

Multiple levels of nesting can be represented by appropriate indentation.

YAML

Identity:
    Name: John Doe
    Age: 42
    Sex: M
    Skill:
       - Java
       - C++

Interpretation
{‘Identity’: {‘Age’: 42,’Name’: ‘John Doe’, ‘Sex’: ‘M’,
‘Skill’: [‘Java’, ‘C++’]}}

It is a nested dictionary. The top level dictionary has one element having a key named “Identity” . Its value being another dictionary inside with four key: value pairs. The value of the 4th key named “Skill” (inside the nested dictionary) is an array/list.

Note: YAML does not allow the use of tabs while creating YAML files; spaces are allowed instead.

Useful Links:

https://yaml.org/
https://en.wikipedia.org/wiki/YAML
http://yaml-online-parser.appspot.com/

Join the Conversation

4 Comments

Leave a comment

Your email address will not be published. Required fields are marked *