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.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Key Components of a Kubernetes YAML File
-
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. -
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. -
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.
- 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: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:Review the YAML configuration structure and verify command outputs to ensure your Kubernetes objects are correctly defined and running as expected.