In Kubernetes, YAML files play a significant role not just in Kubernetes but also in Ansible, Docker Compose etc. Many beginners are in question about how to write YAML files for pods. In this article we will see how to write a simple YAML file for pods.

First of all, remember 4 things while writing YAML files for pods

What are they? 🤓

They are as follows -

  1. apiVersion
  2. kind
  3. metadata
  4. spec

1. apiVersion

In Kubernetes, there are many Kubernetes objects such as pod, deployment, cronjob, etc. So, apiVersion indicates the Kubernetes API version for creating pods. The image shown below displays different API versions for Kubernetes objects.

Image description

2. kind

The kind field denotes which Kubernetes object will create.

3. metadata

The metadata includes the data about Kubernetes objects which is going to be created, like name, labels, etc.

4. spec

The spec denotes what will be included inside the pods after creation.
For example - Containers.

Now, let's see a simple YAML file for creating pods.

apiVersion: v1
kind: Pod
metadata:
  name: first-pod-01
spec:
  containers:
    - name: nginx
      image: nginx

In the above YAML file, all the 4 things are mentioned, which we have seen above.

After running the kubectl apply -f , the pod will be created.

Image description

Note - Image I have used to show API versions of different Kubernetes object from this link.