Kubernetes POD YAML Definition

After reading this post you will understand the basics of creating kubernetes objects using a YAML definition file. You will also learn know how to create a POD using a YAML file.

Let's get started.

Kubernetes uses YAML files as inputs for the creation of objects such as:

  • PODs
  • ReplicaSets
  • Deployments
  • Services

All of these follow a similar structure.

Usually, a Kubernetes YAML definition file contains four top level fields:

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

spec:

These are the top level or root level properties.

They are also required fields so you must have them in your configuration file.

Let's look at each one of them.

apiVersion

This is the version of the kubernetes API you are using to create the object. Depending on what you're trying to create, you must use the right API version.

In this case, since we are working with PODs. We will set the apiVersion as v1.

apiVersion: v1
kind:
metadata:

spec:

A Few other possible values for this field are:

Kind Version
POD v1
Service v1
ReplicaSet apps/v1
Deployment apps/v1

kind

The kind refers to the type of object we are trying to create which in this case happens to be a POD.

So we will set it as a Pod:

apiVersion: v1
kind: Pod
metadata:

spec:

Some other possible values are:

Kind Version
POD v1
Service v1
ReplicaSet apps/v1
Deployment apps/v1

Metadata

The metadata field contains information about the object, such as its name, labels etc.

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

As you can see, unlike the first two where you have specified a string value. The metadata field takes the form of a dictionary.

Everything under metadata is indented to the right such that names and labels are "children" of metadata.

Tip: The number of spaces before the two properties: name and labels, doesn't matter, but they should be the same as they are siblings.

Example:

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

In the previous code snippet, labels has more spaces on the left than name and so it is now a child of the name property instead of a sibling. This is incorrect!

Under metadata, the name is a string value so you can name your POD: myapp-pod.

Labels is a dictionary within the metadata dictionary, it can have any key and value pairs as you wish:

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

You could add other labels as you see fit which will help you identify these objects at a later point in time.

Say for example there are hundreds of PODs running a front end application and hundreds of PODs running a back end application or a database.

It will be difficult for you to group these PODs once they are deployed.

If you label a kubernetes POD as "frontend", "backend" or "database" you will be able to filter the PODs based on this label at a later point in time.

It's important to note that under metadata you can only specify name or labels or anything else that kubernetes expects to be under metadata.

You cannot add any other property as you wish under this.

However, under the labels you can have any kind of key or value pairs depending on your needs.

Spec

The last section in the configuration file is the specification section which is written as "spec".

This is where we would provide additional information to kubernetes as pertaining to that object, which will differ depending on the object we want to create.

Tip: refer to the documentation section to get the right format for each kubernetes object.

Since we are only creating a pod with a single container in it, spec is a dictionary so add a property under it called containers.

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
      app: myapp
      foo: bar
spec:
    containers:
      - name: nginx-container
        image: nginx

Containers is a list or an array. The reason this property is a list is because PODs can have multiple containers within them.

In this case though, we will only add a single item in the list since we plan to have only a single container in the pod.

The dash right before the name indicates that this is the first item in the list.

The item in the list is a dictionary. So add a name and image property the value for image is in "nginx" which is the name of the Docker image in the docker repository.

Once the file is created run the following command to create a kubernetes object based on a file name:

kubectl create -f pod-definition.yaml

Summary

Recall the four top level properties:

  • apiVersion
  • kind
  • metadata
  • spec

Command to see a list of pods available.

kubectl get pods

To see detailed information about a POD.

kubectl describe pod myapp-pod

This will tell you information about the pod, when it was created, what labels are assigned to it, as well as the docker containers that are a part of it and the events associated with that POD.