Kubernetes YAML File

What is a YAML File?

YAML stands for “Yet Another Markup Language”. These are essentially plain text files used for converting programming languages into a format that can be stored, transmitted, distributed, and reconstructed.

YAML is a human-friendly language. In the context of Kubernetes, YAML files are mainly used for the configuration of K8 pods, services, and deployments. In Kubernetes, YAML is a manifest file, which performs the functions mentioned above. They dictate how a pod should run, interact with other objects, and more.

Many times,  JSON files are also used for the same purpose but YAML is becoming increasingly popular. This is because it is easy to understand by different people, the structure is pretty simple, and overall, more flexible.

This encapsulates the essence of YAML and its usage with Kubernetes. However, I’ll be taking a deeper dive to illustrate how YAML files are structured, written, and implemented in Kubernetes systems.

How is YAML Related to JSON and XML?

Before we jump in let’s look at how YAML compares with two other popular serialization languages, XML and JSON.

XML and JSON are pretty popular for web development and JavaScript language. While XML is better structured, it has a higher overhead compared to JSON. So, people started using the latter which is currently pretty popular. However, when it comes to complex data structures, XML is far more useful.

When you look at YAML, it is becoming popular with applications such as Kubernetes because it is easier for people to understand. It is also far more flexible compared to XML and can work with different programming languages. So, some developers use YAML while configuring and managing K8 clusters.

Dissecting a YAML File

A YAML file is composed of three important parts. These are:

Key/Value Pair: This is an elemental part of the file where a person declares a variable and passes a value to it. A simple example could be as follows:

 Fruit: Orange

Vegetable: Carrot

Liquid: Water

Here the left side is known as the keys and the right side is known as the values.

Arrays/Lists: This is the second type, where you can store multiple values in a single key. So, you could essentially group similar values under one key. For instance:

Fruit:

Orange

Apple

Grape

Vegetable:

Carrot

Radish

Potato

Here all vegetables are grouped into one list or array. There can be different lists in a YAML file and the indentation of the text is important. It helps the system understand the hierarchy. The fruits are values passed to the key Fruit. The same can be applied to a Vegetable.

Dictionary/Map: A YAML file can be called a dictionary or a map when the complexity increases. Here you can have nested lists such that your values act as keys to more values. For instance:

Banana:

Calories: 150

Fat: 0.25g

Carbs: 15g

Grapes:

Calories: 75

Fat: 0.2g

Carbs: 10g

Here, Banana is a key that stores a list of its various attributes. Each of these attributes is in turn a key, which holds a value.

Here are a few more formats and norms for writing a YAML file.

Scalar: This is a simple key/value pair where you can follow a format if the value is something like a date or time.

Collections: When you want to use the same information in multiple places, you precede the information with a ‘&’ and an ID. You use this ID wherever necessary.

Multi-Line Collections: If your information has multiple key/value pairs nested under a key in different lines then you can use the ‘|’ symbol.

Multi-Line Formatting: If your information is just one string spanning across different lines, then you can use the symbol ‘>’.

I’ll be illustrating how you can use these norms with a YAML file written for Kubernetes in the next section.

How to Use YAML with Kubernetes?

Creation of Kubernetes Pods

To create a K8 pod, you need to create a simple YAML file with three important declarations.

You need to declare the API Version you’ll be using, the kind/type of object you’re creating, and the containers that exist within it. The code is as follows:

apiVersion: v1

kind: Pod

metadata:

  name: mywebapp1

  labels:

role: webserver-role

app: nginx

spec:

  containers:

  – name: webserver1

image: nginx:1.6

ports:

  – containerPort:80

  – name: database-server

image: mysql-3.2

ports:

  – containerPort:3306

volumes:

  – name: websvr-storage

emptyDir: {}

In this case:

●     The API Version is specified with the key/value pair apiVersion: v1.

●     The kind: Pod specifies that you’re creating a Pod.

●     The complete section under containers specifies that you’re declaring two containers which are a web server and a database server.

However, this is a basic example. You can specify numerous details such as the CPU resources the containers can use, which pod can be assigned to which node, and even configure service accounts.

Kubernetes Deployment

The deployment helps with understanding which resources should be used and managed as a single unit. You can also mention the number of replicas and replica sets you want for your K8 pods. Here’s a sample code:

 apiVersion: apps/v1

kind: Deployment

metadata:

  name: nginx-deployment1

  labels:

         app: nginx

spec:

  replicas: 3

  selector:

matchlabels:

   app: nginx

template:

  metadata:

labels:

   app: nginx

  spec:

containers:

– name: webserver1

           image: nginx:1.6

           ports:

           – containerPort:80

– name: database-server

           image: mysql-3.2

           ports:

           – containerPort:3306

In this example,

●     There is a request for 3 replicas of the state.

●     There are also instructions to create 2 pods similar to the previous section. Furthermore, with the matchlabels command, you ensure that only pods with those labels are created and deployed.

●     To get this deploy this configuration,  use the kubectl create -f /k8s/deployments/deployment1.yaml command.

Creation of Kubernetes Service

Now that you’ve created and deployed your K8 pods, you need to ensure that you’re able to access them. For instance, if there’s an HTML request to access your pod-set, you need to be taken to the database that possesses these. Essentially, this step will help you see what kind of applications are running on what pods.

apiVersion: v1

kind: Service

metadata:

  name: nginx-service 

  labels:

app: nginx

spec:

  ports:

  – nodePort: 30500

port: 80

protocol: TCP

targetPort: 80

  selector:

app: nginx

  type: NodePort

With this, you can give the service name such that it matches the label. Furthermore, you’ve also specified the NodePort that will be used to access the service. In this example, the NodePort: 30500 will allow you to access the service Nginx and its corresponding pods, which match the label.

Conclusion

Overall, this is how a YAML file is used with the Kubernetes clusters. While the above examples are simple representations, real-time files are far more detailed and complex. These two factors change depending on how extensive your application is and how you’ve deployed it across your K8 structure.

Stay up to date