Learn more >
Raghavendra Deshpande | Published February 20, 2019
In this tutorial, we will learn the structure of YAML and basics of how we write a YAML file. We will also explore with an example of a YAML file that’s used in Kubernetes.
In this tutorial, you will learn:
This tutorial assumes that you already know the basics of languages that are used for storing and transferring data, such as XML and JSON.
Before going further, let’s understand the definition of YAML. As the definition on www.yaml.org states, “YAML is a human-friendly, data serialization standard for all programming languages.”
YAML was created specifically for common use cases such as:
There are a few advantages to using YAML files:
XML is a pioneer in many domains. XML was originally designed to be backwards compatible with the Standard Generalized Markup Language (SGML), which was designed to support structured documentation. Because of this, there are many design constraints with XML.
JSON’s design goal is simplicity and universality, with its goal to generate and parse. It has reduced human readability, but its data can be processed easily by every modern programming environment.
YAML’s design goals are human readability and a more complete information model. YAML is more complex to generate and parse, therefore it can be viewed as a natural superset of JSON. Every JSON file is also a valid YAML file.
All this to say, it makes it easy to migrate from JSON to YAML if/when the additional features are required. YAML is the result of lessons learned from XML.
The following are the building blocks of a YAML file:
Keep in mind the following guidelines when you write a YAML file:
Consider the below diagram, which has details about “Banana.” There are 3 attributes:
Suppose there is an extra indentation or tabs used – then the whole meaning of the YAML object changes as we can see below. Hence the need for us to be careful with respect to indentation and tabs when it comes to your YAML files.
There are other aspects that we need to concentrate when we create YAML files, such as:
Having seen the advantages and basic building blocks of a YAML file, let’s understand how YAML is used in Kubernetes.
The Kubernetes resources are created through a declarative way, thus making use of YAML files. Kubernetes resources, such as pods, services, and deployments are created by using the YAML files.
This example helps explain the creation of the deployment resource by using the YAML:
Note: This example contains both basic and advanced specifications.
Here’s a brief explanation of the various fields:
We can see from the above that YAML files can really help you maintain and control Kubernetes resources in a declarative way.
YAML is on its way to being considered a de facto standard for configuration files.
You can continue your learning by visiting the Kubernetes Workshop material.
These labs are an introduction on how to use Docker containers on Kubernetes in the IBM Cloud Kubernetes Service. By…
In this tutorial, you'll learn about Kubernetes clusters, how to debug them, and get logs from your application.
Back to top