YAML Basics for Kubernetes
Introduction
YAML (YAML Ain't Markup Language) is a human-readable data serialization format commonly used for configuration files, and it plays a significant role in Kubernetes (K8s) configurations. In this revision, we'll delve into YAML basics, emphasizing key concepts crucial for understanding and working with Kubernetes manifests (*.yaml
files).
Whitespace and Indentation
YAML relies on whitespace and indentation to denote structure. It's essential to use spaces for indentation, as tabs are not allowed due to inconsistent handling by different tools. The number of spaces used for indentation is flexible but must be consistent within the document.
codekey:
subkey1: value1
subkey2: value2
Newlines and Fields
Newlines typically indicate the end of a field, and indentation defines the hierarchy. The YAML document's structure is shaped through indentation, with one or more spaces at each level. Comments, indicated by a pound sign (#), can appear after a value or occupy an entire line.
key: value # Comment
Mapping
Mappings associate unordered key/value pairs. Nesting is achieved through indentation. The absence of a specific indicator like (-) for nesting makes it crucial to rely solely on indentation.
parent:
child1: value1
child2:
grandchild: value2
Sequences (Lists/Arrays)
Sequences represent ordered elements using a hyphen (-) and space. Lists can be embedded within maps through indentation. The default behavior is ordered, and each element is represented as a new line with consistent indentation.
codehosts:
- servera
- serverb
- serverc
Alternatively, lists can be written using square brackets:
codehosts: [servera, serverb, serverc]
Arrays of mappings (or lists of maps)
Arrays of mappings (or lists of maps) are a way to represent a sequence of maps, where each map contains key-value pairs. This structure is commonly used in various configurations, including Kubernetes manifests.
Let's break down how you can define arrays of mappings in YAML:
- key1: value1
key2: value2
- key1: value3
key2: value4
- key1: value5
key2: value6
In this example, we have a YAML sequence (denoted by the use of - followed by a space), and each item in the sequence is a map with key-value pairs. In Kubernetes, this can be used, for example, to define a list of environment variables or a list of volume mounts for a container.
List within a map
you can embed a list (sequence) within a mapping (map or dictionary). This allows you to associate key-value pairs where one of the values is a list. Here's an example to illustrate this concept:
users:
- name: Alice
roles:
- admin
- editor
- name: Bob
roles:
- viewer
Here:
The top-level key is users, indicating a mapping (map or dictionary).
The users map does not have a single value but a list.
The first item of list has
a map with one value(name: Alice)
a map with list [] as values
Literals
Scalars, such as strings, numbers, and booleans, are represented as literals. Unquoted string literals are common, but quoting is necessary when the content might be misconstrued as a special character.
message: Hello, World!
count: 42
enabled: true
YAML Multiline
YAML supports multiline content using two different styles: |
and >
.
The
|
style preserves newlinesThe
>
style folds newlines, replacing them with spaces.
multiline1: |
This is a multiline
string in YAML.
multiline2: >
This is another way
to represent multiline
content with folded newlines.
Understanding these YAML basics is crucial for working effectively with Kubernetes manifests, where YAML is the primary language for expressing configurations.