Create PODs using YAML

Create PODs using YAML

In this tutorial we will learn about how to create PODs using YAML files.

Kubernetes uses YAML files as input for the creation of objects such as PODs, replicas, deployments, services etc. All of these follows similar structure.

Basically YAML is based on a key-value system. Where the key is a string, and the value is essentially a container that can hold all kinds of things, such as:

  • A string
  • An array of values (which in turn are containers)
  • Another key value pair
  • A dictionary (a set of key value pairs).

One of the nice things about YAML is that it’s relatively human readable when compared to XML or JSON.

Note

YAML files are space sensitive. So need to always make sure all your indents are correct.

Required Fields

A Kubernetes definition file always contains 4 top level fields.

apiVersion:
kind: 
metadata:



spec:

These are the top level/root level properties. These are all required fields. So you must have them in your configuration file. Let us discuss on each one of them.

Here key names (apiVersion, kind etc) are all case sensitive.

The apiVersion, kind, metadata, and spec, are the required fields, for all kubernetes object files.

1. apiVersion – This is the version of the Kubernetes API you’re using to create this object. Depending on what we are trying to create, we must use the right api version.

The different possible values for apiVersion are

Kubernetes apiVersions

2. kind – The kind refers to the type of object you want to create. The different possible values for kind are

Kubernetes apiVersions

3. metadata – The metadata is the data about the object like name, labels etc. metadata.name is used to assign a name to the object. metadata.labels is also another really important feature. It not only lets you organize your resources, but it also offers a means to filter your resources, and can be used as a way to refer to a group of resources.

4. spec – The spec property is used to describe what exactly you want Kubernetes to build.

Note

The first 2 property (apiVersion and kind) values are in the form of String only and next 2 values (metadata and spec) are in the form of dictionary.

E.g

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels: 
    app: myapp

spec:
  containers:
    - name: nginx-container
      image: nginx
      
    - name: backend-container
      image: redis

Once the above YAML file is created, we will save the file with the name of myapp-pod-definition.yml and run the create command to run the document.

kubectl create –f myapp-pod-definition.yml

To get the list pods you can run get pods command

kubectl get pods
NAME                READY   STATUS      RESTARTS   AGE
myapp-pod           1/1     Running        0       45s
Updating objects

Kubernetes is smart enough to identify which objects have been created by a particular config file. It does so by using the configs about the ‘kind’ and metadata.name info. The YAML descriptors filename itself doesn’t matter, as long as it ends with the .yml/.yaml extension. You can make changes to the YAML files (as long as it isn’t changing the ‘kind’ or ‘metadata.name’ fields) and just apply them again for the changes to take affect.

Create PODs using YAML
Scroll to top