Introduction to YAML for Kubernetes
Otuekong Arthur
Posted on December 16, 2023
Welcome as we go through the basics of YAML, the go-to configuration language for Kubernetes. No tech jargon or complex terms here, just straightforward insights to help you understand YAML.
In this article, we will check out YAML as it pertains to Kubernetes.
YAML?
YAML, short for YAML Ain’t Markup Language, is a file format for making configurations. It is a data serialization language for all programming languages, which means it acts as a middleman to let programming languages talk to each other. Although Kubernetes files can be written in JSON and XML, YAML is the most preferred for this.
Among the benefits of YAML are:
- Human readable
- Intuitive Structure
- Easy to Learn and Write
- Versatility in Configuration
Assumptions:
I assume that you:
- Know what Kubernetes is
- Know what Kubernetes config files are.
If you know the above, let's jump right into it.
Understanding YAML
It will be nice to get some understanding of YAML.
Let's start with indentation.
Indentation
YAML uses indentation to represent the structure of data. Think of it like an organized to-do list. Each task (or data element) is neatly indented to show its relationship to the broader category. Indentation signifies the hierarchy and relationship between different components in a YAML file.
Notice how in the first countries list, the countries are all aligned, while in the second countries list the fact that the countries are not all aligned makes it invalid YAML.
YAML takes indentation very seriously.
#valid yaml
countries:
-Nigeria
-India
-USA
-Germany
#invalid yaml
countries:
-Nigeria
-India
-USA
-Germany
Key-Value Pairs
It uses a key-value pair structure. Let's look at how this key-value setup works.
name: Otuekong
In the above example, the colon separates the key from the value, with the name being the key and Otuekong
being the value.
Sequence/Lists
Sequence/List offers a way to group related data in YAML.
countries:
-Nigeria
-India
-USA
-Germany
In the above example, the countries
key has a list of strings representing different countries.
The above may also be written as:
countries: [Nigeria, India, USA, Germany]
There are nested lists too in YAML. See it as a list inside a list or lists inside a list.
nested_list:
- list1
- list2:
-sublist1
-sublist2
In the preceding example, the nested_list
key has list1
and list2
under it.list2
holds sublist1
and sublist2
Scalars
A scalar is a single piece of information, such as a number or some text. It's like a basic building block for data, like a single Lego piece instead of the whole Lego set. Scalars in YAML are simple, just like writing a single number or a short sentence. They help you express simple stuff without getting too fancy or complicated.
There are 4 scalar types in YAML:
- Strings
- Numbers
- Booleans
- Null
Let’s go a little deeper into the scalar types.
Strings
Unlike Javascript-Object Notation(JSON), Yaml accepts strings with or without quotation marks.
name: Hector #string without quotation marks
name2: 'Hector' #string with quotation marks
The above demonstrates the two ways of declaring a string in YAML and they are both valid.
Numbers
When it comes to numbers, it can be represented in YAML as:
- Integers
- Floating-point numbers
- Scientific notation (exponential notation)
- Hexadecimal and octal notation (using
0x
and0o
prefixes) - Numeric strings
age: 50 #I am an integer
temperature: 23.5 #I am a floating-point
large_number: 4.022e23 #I am a scientific notation
hex_value: 0xFF #I am a hex value
octal_value: 0o12 #I am an octal value
numeric_string: "007" #I am a numeric string
Booleans
In YAML, on, true, and yes represent true while off, false, and no represent false.
#These are all true values
christmas: on
harmattan: true
holiday: yes
#These are all falsy values
rains: off
travel: false
something: no
Null
Null represents the absence of a value. It can be represented with the string null
or the tilde sign (~) which on most keyboards is located on the top-left corner of the keyboard, to the left of the number 1 key.
christmasHamper: null
reason: ~
The above snippet demonstrates the usage of null.
Maps
A map is like a dictionary where you have a key (the word) and a value (the definition). Maps in YAML help organize data in a clear and easy-to-read way.
The snippet below represents a simple map under the key scores. Each entry within the scores map consists of a name (key) and a corresponding score (value).
scores:
Williams: 45
Francis: 13
Charles: 65
Comments
Comments are like little notes you can add to explain things. You put a # (hash symbol) and then write whatever you want next to it. YAML ignores it as it's just for you and others to understand what's going on.
desserts:
- Cupcakes #This is a comment
- Chocolate Chip Cookies
- Brownies
#This is also a comment
YAML in Kubernetes
It is possible to imperatively manage Kubernetes resources by directly giving Kubernetes the instructions to manage your resources but soon enough this will get tiring and complicated to manage. Then comes the declarative way. Using YAML configuration files. With this, you just write what you want Kubernetes to do in a YAML file, point Kubernetes to the file, and let it handle the rest. Let's relate what we learned up there to Kubernetes.
We will need a Kubernetes config file. For this, let's go with a Pod file:
apiVersion: v1
kind: Pod
metadata:
name: merry_christmas
spec:
containers:
- name: nginx-container
image: nginx:stable-alpine-slim
ports:
- containerPort: 80
restartPolicy: Always
nodeSelector:
gpu: true
activeDeadlineSeconds: null
Let’s break down the content of the above file.
- apiVersion: This is a string scalar that Specifies the Kubernetes API version used.
- kind: This is still another string scalar that Indicates the type of Kubernetes resource in this case Pod.
- metadata: It is a map that Contains metadata about the Pod. In this case, It contains the name of the pod.
- spec: This is a map that Describes the desired state of the Pod. This contains a map of:
- containers: Specifies a list of containers within the Pod.
- name: This is a string scalar inside containers stating the name of the container.
- image: This is also a string scalar inside containers specifying the Docker image to use. 8.ports: This contains a list of ports for the container. 9.containerPort: This is a scalar integer and indicates to Kubernetes the port to open on the container in this case port 80.
- restartPolicy: It is a string scalar that sets the restart policy for the Pod.
- nodeSelector: This is a map that specifies node selection criteria for the Pod.
- gpu: This is a boolean scalar indicating whether the node should have a graphics processing unit(GPU). Here it is set to true. It could also take yes.
-
activeDeadlineSeconds: Specifies the maximum time (in seconds) for the Pod to run. It's set to
null
scalar, indicating an undefined value.
Three things are worthy of note here. They are:
- scalars represent single values which could be numbers, strings, booleans, and null.
- Mappings represent key-value pairs.
- Sequences represent lists of items.
Understand the above and throw in indentation and you are on your way to conquer YAML.
Conclusion
Understanding YAML is essential for anyone working with Kubernetes. In this article, we went into the basics of YAML, looking at indentation, scalars, maps, and sequences. We finally connected the dots and made sense of the pieces that make up YAML. I hope this article made YAML more approachable and in a way simplified your Kubernetes operations.
Thanks.
Resource
If you are looking to advance your understanding of YAML, I recommend this article.
YAML Tutorial: A Complete Language Guide with Examples
Posted on December 16, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.