Kubernetes Demystified: YAML Tutorial for DevOps
What is YAML? YAML is a data serialization language, like XML and JSON. What is a serialization language? Applications written with different technologies and languages, which have different data structures, can transfer data to each other on a common grid or standard format. Ex: JSON, YAML, XML. YAML is not — yet another markup language. "Human friendly" data serialization standard for all…
YAML, short for YAML Ain't Markup Language, is a data serialization format similar to XML and JSON. Serialization languages enable applications using diverse technologies and data structures to exchange information through a shared format. Alongside YAML, JSON and XML serve this purpose.
YAML distinguishes itself from markup languages like HTML, prioritizing human-friendly data serialization suitable for any programming language. Developers often store YAML configuration files alongside their codebase or in a separate Git repository. The file extension for YAML is .yaml or .yml.
Compared to other serialization formats, YAML is known for its readability and intuitiveness. Line separation and indentation play crucial roles in structuring YAML data. The language finds utility in various DevOps tools, including Docker Compose files, Docker, Kubernetes, Ansible, and Prometheus.
In YAML, line separation and indentation serve as key identification markers. The language is a superset of JSON, meaning that any valid JSON file can also be considered a valid YAML file. This compatibility facilitates easy data interchange between YAML and JSON-based systems.
In terms of basic syntax, YAML employs key-value pairs to represent data. For instance, an example might look like this:
app: user-authentication
port: 9000
version: 1.7
Objects in YAML are defined by indenting key-value pairs and further indentation for nested objects. Here's an example:
microservice:
app: user-authentication
port: 9000
version: 1.7
Lists are another essential concept in YAML. They are expressed using hyphens (-) for list items. For example:
microservice:
- app: user-authentication
port: 9000
version: 1.7
Boolean values in YAML can be represented using specific keywords:
deploy:
yes
no
on
off
false
true
Additionally, YAML supports nested lists. For instance:
microservice:
- app: user-authentication
version:
- 1.7
- 2.1
- 8.1
port: 9000
When it comes to Kubernetes, YAML files play a vital role in defining and managing resources within the cluster. A Kubernetes YAML file typically includes the following elements:
- apiVersion: Specifies the Kubernetes API version used.
- kind: Indicates the type of Kubernetes resource (e.g., Pod, Service).
- metadata: Contains metadata about the resource, such as its name.
- labels: Allows for the assignment of key-value pairs to categorize and organize resources.
- spec: Defines the resource's desired state, including container specifications and other settings.
A Kubernetes YAML file can include lists of objects, such as containers and volume mounts. For example, the following snippet demonstrates a container specification:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: nginx-vol
mountPath: /usr/nginx/html
This example showcases the key-value pair structure, object indentation, list representation, and Boolean values commonly found in YAML syntax. By utilizing YAML in Kubernetes configuration files, DevOps engineers can effectively manage their containerized applications and infrastructure.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.