Basic Use Of Yaml In Kubernetes.

What is a YAML file?

Wikipedia termed YAML as a data serialization language, which is true but if I were to define YAML to a five-year-old I would say it's simply a configuration file that is easily readable by all programming languages and humans. YAML simply stands for "YAML ain't markup language". There are other types of data serialization such as JSON and XML but in DevOps, Yaml is the most preferred configuration file. data serialization i the process of converting data to a format that can be easily stored or sent over a network. Some technologies where YAML is used are docker-compose, Kubernetes, ansible, puppet, and many more. Also, another thing to note is that YAML works in a key-value data format. a key, which is a constant defining a data set, while the value is simply the data or sets of data. Data types supported by YAML are strings, booleans, numbers, and arrays.

Here is a simple example of how a YAML key: value file looks like.

     key: value
    name: john
    age: 20
    job: DevOps

hobbies:          #-------list
  - swimming
  - dancing
  - cooking

skill_set:            #-------dictionary
  python: okay
  Jenkins: superb

 work-experience:       #-------lists inside a dictionary
    company-01:
        - 2_years
    company-02:
        - 3_years

NOTE: A dictionary is a collection of key-value pairs while a list is multiple values of a key, however, a dictionary can also contain a list and another dictionary. One thing to note when creating YAML is the strict indentation rule. YAML shares the same indentation rule as python and it doesn't accommodate tab spaces (spaces made with the tab key on your keyboard will print an error message.) only white spaces are allowed. There are online software that helps you validate indentations so you don't get error messages when running your codes.

Why do we need YAML in k8s?

The use of YAML in k8s is essential in building and setting up a Kubernetes cluster. One beautiful thing about k8s is that it creates a YAML file automatically with the help of a command. Here is a simple command to generate a pod definition YAML file.

kubectl run nginx-pod --image=nginx --dry-run=client -o yaml**

The command will display an excellently indented YAML file just like this...

apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: nginx name: nginx spec: containers:

  • image: nginx name: nginx resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {} You can create and copy it into a file with a .yaml or .yml extension.

This is just a simple illustration of creating a pod with a YAML file, you can easily create a pod with a simple kubectl create podname --image=

but the main reason why we use YAML to create pods is to add more specifications, labels, and creating multiple pods running the same applications, etc.

components of a k8s YAML file

  • API-version: Kubernetes provide API-versions we use in creating objects, The API version depends on the kind of object to create, for example, if you want to create a pod, the version will be v1, the same as the service object, for replicaSet we use the version apps/vi and also for the deployment object.

  • kind: simply means the type of object. It can be a pod, a service, a deployment,replicaset, etc. The kind and API-version work hand in hand, if they do not correspond, you will surely get an error message.

-metadata: Thi contains details or data about the object you want to create Data that helps uniquely identify the object, including a name string, UID, and optional namespace.

-spec: Always in dictionary format, helps in providing supplementary details to k8s the object being created. Here is an example of details in the spec.

  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

These four components basically make up the object creation YAML file, and by objects, I mean pod, resource quota,replicaSet, services, deployments, etc.

This article doesn't cover the entire use of YAML but it should give you a brief insight of how to use YAML to carry out tasks in k8s. for more info visit kubernetes web page official web page.

Thank you for your time, kindly like and share i you find this page useful.