YAML and Its Usage in Kubernetes
If you've used Kubernetes before, chances are you have come into contact with YAML files. Find out more about what they do here.
Join the DZone community and get the full member experience.
Join For FreeYAML is a human-friendly data serialization standard for all programming language.
Purpose of YAML
There are hundreds of different languages for programming, but only a handful of languages for storing and transferring data. YAML is a human-friendly data serialization standard for all programming language. Even though its potential is virtually boundless, YAML was specifically created to work well for common use cases such as:
- configuration files,
- log files,
- interprocess messaging,
- cross-language data sharing,
- object persistence, and
- complex data structures
YAML Usage in Kubernetes
The Kubernetes resources are created through a declarative way making use of YAML files. Kubernetes resources such as pods, services, and deployments are created using the YAML files.
This example helps explain the creation of the deployment resource using the YAML:
Note: This example contains both basic and advanced specifications.
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
revisionHistoryLimit: 5
minReadySeconds: 10
selector:
matchLabels:
app: nginx
deployer: distelli
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
replicas: 3
template:
metadata:
labels:
app: nginx
deployer: distelli
spec:
containers:
- name: nginx
image: nginx: 1.7.9
Explanation of Various Fields
-
replicas
– Tells Kubernetes how many Pods to create during a deployment. Modifying this field is an easy way to scale a containerized application -
spec.strategy.type
– The Rolling Update strategy allows Kubernetes to update a service without facilitating an outage by proceeding to update Pods one at a time. -
spec.strategy.rollingUpdate.maxUnavailable
– It is the maximum number of pods that can be unavailable during the update. -
spec.strategy.rollingUpdate.maxSurge
– It is the maximum number of pods that can be scheduled above the desired number of pods. -
spec.strategy.rollingUpdate.maxUnavailable
andspec.strategy.rollingUpdate.maxSurge
can also be defined as a percentage of all Pods. -
spec.minReadySeconds
– It is an optional Integer that describes the minimum number of seconds for which a new pod should be ready without any of its containers crashing, for it to be considered available. -
spec.revisionHistoryLimit
– It is an optional integer attribute you can use to tell Kuberneres explicitly how many oldReplicaSets
to retain at any given time. -
spec.template.metadata.labels
– It is to add labels to a deployment specification. -
spec.selector
– It is an optional object that tells the Kubernetes deployment controller to only target pods that match the specified labels. Thus, to only target pods with the labels “app” and “deployer” we can make the following modification to our deployment yaml.
So with the above information, any developer, from beginner to intermediate, would be able to write a yaml file for creation of various Kubernetes resources.
Uses of YAML File
YAML files are commonly used for configurations, application manifest files, and many other places. In addition, these files are also often used for defining Kubernetes resources.
YAML files are saved with the extension .yml or .yaml.
Kubernetes Resources
Kubernetes resources are objects that can be created, modified, and deleted through the Kubernetes API. Several different types of resources can be defined, including:
- Config Maps – Used to store configuration information that can be used by applications or tools
- Secrets – Used to store sensitive information like passwords or certificates
- Persistent Volumes – Describes storage that can be used by applications running in Kubernetes
- Deployments – Defines how an application should be deployed to Kubernetes
- Services – Describes how a set of pods can be exposed to the outside world
YAML files are commonly used to define these resources, representing a human-readable way.
Resources that YAML Fields Describe
API version: Kubernetes periodically releases updates which are indicated in the API version field
Kind: The kind field specifies what kind of object it is
Metadata: The metadata includes information that identifies the object like labels, names, and UID
Spec: The spec indicates the configuration or state of the object, and it varies with each object
Writing YAML Files
They are relatively simple to write.
It does not use square brackets or braces. Instead, it uses indentations to define the relations, similar to Python.
So, each child item of a parent is defined by two spaces.
For indicating items in a list, use dashes.
Users can choose between mapping, scalar schemes, and sequences when formatting YAML file data.
How YAML Files are Used
YAML files are used in various applications for configuration, storing, and transferring data.
YAML files have a consistent structure that is easy to read and understand. Their readability makes them ideal for use in configuration files, application manifests, and other places where data needs to be represented in a human-readable way.
YAML files are often used in place of JSON files, as they offer some advantages over JSON. For example, YAML files can support comments, which is not possible with JSON.
Additionally, YAML files can be easily hand-edited, which is useful when working with Kubernetes resources.
YAML and JSON are supersets. So, any legal JSON file can also be represented as a valid YAML document.
Advantages of YAML over JSON
There are a few advantages of using YAML over JSON.
- YAML supports comments, which is not possible with JSON.
- YAML files can be easily hand-edited, which can be helpful when working with Kubernetes resources.
- YAML files tend to be more human-readable than JSON files.
Uses of YAML in Kubernetes
In Kubernetes, a YAML file serves several purposes. First, it helps to define the application service layout.
It also specifies the type of container image that should be used.
Additionally, it contains data about how many replicas to deploy.
Developers use YAML files in Kubernetes to indicate whether or not a load balancer should run behind a service.
It also stores information about the authorizations that connect to the container registry. YAML files offer an information model that is more complete when compared to other languages.
Advantages of YAML for Kubernetes
- Convenient: When writing Kubernetes configurations with YAML, you don't have to specify the parameters in the command line.
- Easier to maintain: You can track changes by adding YAML files to source control
- Adaptable: YAML files give greater flexibility when creating more complex structures
- YAML files offer easy portability between programming languages. Also, the native data structures match agile languages.
Structure of YAML Files
- Key-Value Pair: This is the basic YAML file entry, which consists of a key followed by a colon and a space then the value.
- Arrays/Lists: The lists have the name of the list followed by its items.
- Dictionary/Map: This file type includes more complex information.
History of YAML
YAML was created in 2001 by a team of developers. It was designed to be a human-friendly way of representing data.
"YAML" is an acronym for "YAML Ain't Markup Language." Its pronunciation rhymes with "camel." At first, it meant "Yet Another Markup Language," but it eventually changed. It's named this because YAML is not intended to be used as a markup language like HTML or XML.
Instead of writing documents, its purpose is to store data.
When to Use YAML
There are many times when the YAML language is the preferred language.
Here are some examples:
- If you want to store data in a human-readable format.
- If you need a format that is easy to hand-edit.
- When you need a language that supports comments.
Opinions expressed by DZone contributors are their own.
Comments