Updated April 15, 2023
Definition of Kubernetes YAML
Kubernetes YAML is defined as, YAML is the text-based information which can be read by humans and allows us to simply describe the configuration-type with the help of union of a map and name-value pairs and record of items, we can get additional information on streaming the objects by interrogating Kubernetes to express them, consequently, the resources of Kubernetes can be generated in an indicative way and then making use of these files, by using the YAML files the resources of Kubernetes like a pod, services, and deployment are created and it has four types of files such as apiVersion, kind, metadata, and spec.
What is Kubernetes YAML?
The YAML stands for Yet Another Mark-up Language which can be used for data serialization, it is a text-based format like JSON and XML in which it can be read by humans to increase readability and in data serialization standard which can be used in all programming languages, it can be updatable by humans due to this it can work for both human use cases and machine log files, configuration files, inter-process messaging, there are different type of YAML files in Kubernetes that apiVersion, kind, metadata, UID, namespace, and spec.
How to write YAML files?
- To create the YAML files it has a hash map containing one or more key-value pairs,
key: value
A value can be edited by setting another key-value pair,
key:
another_key: "another value"
- If we wanted to use a multiline string then we have to use ‘>’ character come after any number of lines, it can be used when we have extended commands,
Jaiku :
Please remember me
If anyone write it
ohhh, and welcome.
- To write YAML file it has sequences by using key and values we can scalar to a sequence,
scalar:
-has
-done
-he
-you
We can also write these items in key-value pairs,
- We can use Anchors and Aliases in the config file, anchors are recognized by &, and aliases are recognized by *,
song:
- &game A2
- he
- man
- fall
- you
- she
- *game
When we use the same file in YAML then it looks like,
song:
- A2
- he
- man
- fall
- you
- she
- A2
- We can also use merging maps to create YAML files,
default : &default
collage : jumbokid
sonu:
<< : * default
home : mumbai
dolly :
<< : * default
home : chennai
We can merge multiple maps,
name : &sonu_name
first_name : sonu
last_name : dolly
address : &sonu_address
street : 5, villa
district : Little whising
country: England
sonu_date:
<<: [* sonu_name, *sonu_address]
In this way, we can write YAML files,
YAML basics
The YAML is convenient, in which we do not want to append all the variables to the command line, it is well maintained in which the YAML files can be appended to source control so that with the help of it we can trace interchange, and it is flexible hence by using the YAML files we can able to create a better composite form so that we can go on the command line.
It is closely related to JSON it means any well-founded JSON file is a YAML file, we can able write the JSON file then we no need to worry because that file can only need to write in YAML, whereas, if we are trying to search the same example on the web then we get the files in YAML, not in JSON.
It has more basic components than the conventional block format which uses hyphen+space to start an item in a list, the inline format is established with comma and space, the folded format can change the end of lines to space and eliminate the superior whitespace.
YAML maps
The YAML maps constitute the disordered assembly of key-value pairs, the key which is used must be unique because of duplication, if we are trying to configure the information then to understand this let us consider we have a config file like,
Example#1
---
apiVersion: v1
kind : pod
In the above information, the 1st line is a separator which is optional, in one file we can write multiple structures, and in the above file, there are two values v1 and pod which are mapped to apiVersion and kind.
Example#2
{
"apiVersion" : "v1"
"kind" : "pod"
}
In this, we can use quotations that are optional and it can be used when we are searching for a string that is based on formatting, to create the complicated structures we can create a key that maps the another map.
Kubernetes YAML lists:
The list in a Kubernetes YAML is an order of objects, we can use many items in a list and we have to define the item with a dash (-) which is followed by the parent item. We can put the member of lists on the map also.
YAML works fast as compared to JSON, we can use many structures such as maps with a group of name-value pairs, individual items can be used in lists also, maps of maps, maps of lists, lists of lists, lists of the map, etc.
Creating Kubernetes YAML:
Let us see an example of creating a YAML file,
Example:
import yaml
members = [{'name': 'Anisha', 'occupation': 'Teacher'},
{'name': 'Zoya', 'occupation': 'Doctor'}]
print(yaml.dump(members))
Output:
In this example we have written the YAML file and try to convert it in python for that we have to use the dump() method to serialize the python objects, in which the yaml.dump() method can take two argument data and stream which will give the produced document.
Conclusion – Kubernetes YAML
In this article, we conclude that the YAML format is used to be readable for a human that means it converts the file format, so also we have discussed how to create the Kubernetes YAML list, how to create Kubernetes YAML files, YAML maps, and also we have seen the YAML basics.
Recommended Articles
We hope that this EDUCBA information on “Kubernetes YAML” was beneficial to you. You can view EDUCBA’s recommended articles for more information.