Skip to main content
Welcome to this lesson on creating a Pod in Kubernetes using a YAML configuration file. In this guide, you’ll learn how to structure your YAML file, create the Pod with kubectl, and verify its status. Kubernetes leverages YAML files to define objects such as Pods, ReplicaSets, Deployments, and Services. These definitions adhere to a consistent structure, with four essential top-level properties: apiVersion, kind, metadata, and spec.

Top-Level Fields in a Kubernetes YAML File

Every Kubernetes definition file must include the following four fields:
  1. apiVersion
    This field indicates the version of the Kubernetes API you are using. For a Pod, set apiVersion: v1. Depending on the object you define, you might need different versions such as apps/v1, extensions/v1beta1, etc.
  2. kind
    This specifies the type of object being created. In this lesson, since we’re creating a Pod, you’ll define it as kind: Pod. Other objects might include ReplicaSet, Deployment, or Service.
  3. metadata
    The metadata section provides details about the object, including its name and labels. It is represented as a dictionary. It is essential to maintain consistent indentation for sibling keys to ensure proper YAML nesting. For example:
Make sure that the properties under metadata (like name and labels) are indented to the same level. This is crucial for correct YAML parsing.
  1. spec
    The spec section provides specific configuration details for the object. For a Pod, this is where you define its containers. Since a Pod can run multiple containers, the containers field is an array. In our example, with a single container, the array has just one item. The dash (-) indicates a list item, and each container must be defined with at least name and image keys.
Below is the complete YAML configuration for our Pod:

Creating and Verifying the Pod

After you have saved your configuration (for example, as pod-definition.yaml), use the following command to create the Pod:
Once the Pod is created, you can verify its status by listing all Pods:
You should see output similar to this:
To view detailed information about the Pod, run:
This command provides extensive details, including metadata, node assignment, container specifics, and event history such as scheduling, volume mounting, and container start-up. Here is an example output:
Using kubectl describe helps you gain detailed insights into the internal state of your Pod, which can be invaluable for troubleshooting.

Conclusion

In this lesson, you learned how to structure a Kubernetes YAML file for a Pod, create it using kubectl, and verify its status. This hands-on approach equips you to manage and troubleshoot your Kubernetes resources effectively. Happy Kubernetes-ing! For more information, refer to the following resources:

Watch Video