Basic Structure of Kubernetes configuration file.

Basic Structure of Kubernetes configuration file.

The Kubernetes applications can be configured declaratively using YAML files. These files allow us to control the Kubernetes applications with ease. All of the files in Kubernetes are often written in declarative languages like JSON or YAML. No matter what syntax you choose, the API will transform the file you provide it to at the end into JSON format, however you will typically find files written in YAML because it is simple for people to read and write.

The Kubernetes YAML files include these data structures: key-value pair, lists, maps, list of maps and map of lists.

The four fields that must be present are apiVersion, kind, metadata, and spec (specification). Let's go over each of these and understand them.

  • apiVersion:

From this field, it is clear that it is related to the API, and indeed it is. It is the API used to create the Kubernetes object. The value of apiVersion is usually v1, which contains many core objects. It can also be app/v1, which has objects with some other functionality. Let's see the example with the value v1.

apiVersion: v1

NOTE: For the syntax in the YAML file to be proper, a space must be added after the colon.

  • kind:

Here we define the type of object we aim to create. The objects that we specify here are linked to the apiVersion that we specified above, and this apiVersion gives us access to use the different objects. The types of objects can be Pod, Deployment, Service, Job, or DemonSets.

kind: Pod
  • metadata:

The object that we specified, metadata, provides unique properties of that object. This may contain name, namespace, and label fields. So, through this, we get information about the object. Here is an example.

metadata:
  name: nginx
  • spec:

As we are building an object, we expect the object to do something, so here we define the operation of the object. The specification of the object also depends upon the apiVersion specified above. This includes the containers, memory requirements, storage volumes, network or other details that Kubernetes needs to know about. Here we can also specify more complex properties and give deeper information.

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

NOTE: To ensure that lists and maps are recognised, make sure to properly indent.

An example of a complete and simple kubernetes YAML file.

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

Thank you for reading :)

Happy Learning!

Did you find this article valuable?

Support WeMakeDevs by becoming a sponsor. Any amount is appreciated!