Skip to main content
Hello, and welcome to this tutorial on creating a Pod using a YAML-based configuration file. In this guide, you will learn how to write and apply YAML files specifically tailored for Kubernetes. Kubernetes leverages YAML files to create various objects like Pods, ReplicaSets, Deployments, and Services. Each definition file adheres to a common structure containing four top-level fields: apiVersion, kind, metadata, and spec.

Understanding the Key Fields

Every Kubernetes YAML definition file must include the following properties:
  1. The four main properties in every file are: apiVersion, kind, **metad…
The four main properties in every file are: apiVersion, kind, metadata, and spec. These are essential for Kubernetes to correctly parse and create objects.

1. apiVersion

This property specifies the version of the Kubernetes API you are using to create your object. For Pods, you typically specify "v1". For other objects like Deployments, you might use "apps/v1" or "extensions/v1beta1".

2. kind

The kind field defines the type of object being created. In this lesson, we will focus on creating a Pod, so you would set kind as "Pod". Other common values include ReplicaSet, Deployment, or Service.

3. metadata

The metadata section contains identifying information about the object, such as its name and labels. It is a dictionary rather than a string. Ensure all keys (like name and labels) have proper and consistent indentation. For example:
In this snippet:
  • name uniquely identifies the Pod.
  • labels are key-value pairs used to categorize and manage Pods based on roles such as front-end, back-end, or database.

4. spec

The spec section provides detailed configuration for the object. For a Pod, this involves specifying one or more containers that will run within it. The key containers holds a list of container definitions; each item in the list is a dictionary containing that container’s configuration. Below is a complete example of defining a Pod using YAML:
In this configuration:
  • The containers key defines a list of container specifications.
  • The dash before name indicates the first (and only) container in the list.
  • The container is named nginx-container and it uses the Docker image nginx.

Deploying Your Pod

After creating your YAML configuration file (e.g., pod-definition.yml), you can deploy the Pod using the following command:
Kubernetes will then create the Pod as defined in your file.

Checking Pod Status

To verify that your Pod is running, use:
The output should resemble:
To retrieve detailed information about the Pod, execute:
This command displays comprehensive details including creation time, labels, container status, and events. An example output might look like:

Summary

In summary, when creating a YAML file for Kubernetes, always include the four top-level properties:
  • apiVersion: Specifies the API version.
  • kind: Defines the type of Kubernetes object.
  • metadata: Holds essential information such as name and labels.
  • spec: Details the configuration specifics, such as container settings in a Pod.
This lesson demonstrated how to create a Pod using YAML. The same principles apply when defining other Kubernetes objects such as Deployments, Services, and ReplicaSets. For more information, explore the Kubernetes Documentation.
Consider experimenting with more complex configurations and exploring labels and selectors for robust application management in Kubernetes.

Watch Video