Skip to main content
In this lesson, we explore how to create a Kubernetes Pod using a YAML configuration file. YAML files are fundamental to Kubernetes and are used to define various objects such as Pods, ReplicaSets, Deployments, and Services. Every Kubernetes definition file follows a standard structure with four top-level fields: apiVersion, kind, metadata, and spec. These properties are required for your configuration file to be valid.

Key Components of a Kubernetes YAML File

  1. apiVersion
    This field specifies the version of the Kubernetes API that is used to create the object. For a Pod, you’ll typically use “v1”. Different objects may require other versions such as apps/v1beta or extensions/v1beta, but for our purpose, “v1” is used.
  2. kind
    The kind denotes the type of object you’re creating. In this lesson, we’re creating a Pod. Other valid values include ReplicaSets, Deployments, or Services.
  3. metadata
    Metadata provides essential information about the object, including its name, labels, and other identifying data. It is important that all properties under metadata (like name and labels) are indented consistently. This indentation ensures that these properties are recognized as siblings rather than nested incorrectly.
Ensure that all sibling properties under metadata share the same indentation level to avoid YAML parsing errors.
Below is an example YAML snippet demonstrating proper indentation:
  1. spec
    The spec section contains the detailed configuration for the object. In the case of a Pod, this is where you specify one or more containers and their properties. Although a Pod can run multiple containers, in this example, we define a single container.

Complete YAML Example for a Pod

Below is a complete YAML configuration for creating a Pod with one container that runs the nginx image:
After saving this configuration to a file (for example, pod-definition.yaml), you can create the Pod in Kubernetes with the following command:

Verifying Your Pod

Once you have created the Pod, you can check its status using:
This command lists all the Pods currently running. A sample output might look like this:
To view detailed information about the Pod—including node placement, container details, and recent events—use the following command:
An example excerpt from the detailed output is shown below:
This detailed output provides crucial insights into the Pod’s specifications, such as its running state, the container image details, and the events that occurred—from scheduling to container initiation.
Review the YAML configuration structure and verify command outputs to ensure your Kubernetes objects are correctly defined and running as expected.
Happy coding and enjoy exploring Kubernetes with YAML!

Watch Video