YAML in Kubernetes
YAML is a human-readable data serialization format. It is often used for configuration files, data exchange between languages with different data structures, and in various applications where data needs to be represented in a structured yet human-readable way. YAML was designed to be easy for humans to write and read, and it uses indentation to represent the hierarchical structure of data, similar to how programming languages use whitespace for code blocks.
In this article you will learn more about it in detail and how it applies for Kubernetes.
Intro do IaaC
Infrastructure as Code (IaC) is a practice that involves managing and provisioning infrastructure resources, such as virtual machines, networks, and storage, through machine-readable files and code. IaC has evolved over the years in response to various needs and challenges in the field of IT and infrastructure management.
In the early days of IT, infrastructure was managed manually. System administrators and operations teams would configure servers and networks by physically setting up hardware and manually applying configurations. This approach was time-consuming, error-prone, and not scalable.
As organizations began to recognize the inefficiencies of manual management, they started using scripts and automation tools to streamline certain tasks. This was an improvement, but it often led to a collection of ad-hoc scripts that were difficult to maintain and lacked consistency.
The advent of virtualization and cloud computing technologies transformed infrastructure management. These technologies allowed for more flexible and dynamic provisioning of resources. However, managing virtual and cloud-based infrastructure manually was still complex and required a new approach.
The need for a more systematic and automated approach to infrastructure management led to the emergence of Infrastructure as Code. IaC treats infrastructure as software, allowing infrastructure to be defined, provisioned, and managed using code. This shift in mindset and practices has several drivers:
- Scalability
- Consistency
- Version Control
- Automation
- Self-Service
YAML Structure (Pods)
Now let’s see how it operates YAML. YAML is like XML or JSON, only with different syntax to it. Some of the things you have to pay attention are the spaces, they have a purpose to it, providing an understanding to the system that it means a dependency or not.
The variables and declarations work in Key-Value mode. Then there are dictionaries, lists/arrays. Basically dictionaries are unordered and lists are ordered.
To declare an array in yaml is something like the following:
<entity>
-
<data>
-
<data>
This shows a list/array with 2 positions.
Now in terms of Kubernetes to declare Pods and other elements you have to use YAML files describing everything.
First let’s look to the structure of declaring a Pod in YAML:
apiVersion: V1
kind: Pod
metadata:
name:
label:
spec:
containers:
-
name:
label:
The 4 main elements are apiVersion, kind, metada and spec.
One it is created you can run:
kubectl create -f pod-definition.yml
Visual Studio Plugins
Another detail to have in mind is the plugins or extensions that can help you track the keywords and the format, the proper number of spaces to declare each element of the file. In this article you will see by using VS Code how to ensure that the YAML files are correctly done.
Near uninstall you have a gear icon you have to click and select properties. From there you click where it says “Edit settings.json” and you add the following:
"yaml.schemas":{
"kubernetes": "*.yaml"
}
Restart it and now you can create your YAML files, add just the:
apiVersion V1
And it automatically will provide auto complete for the remaining elements.
Conclusion
And there you go, now you know the basis of YAML to start setting up Pods and more.