Skip to main content
In this lesson, we will create a Kubernetes Pod using a YAML definition file instead of the “kubectl run” command. This method offers more control by allowing you to define pod specifications explicitly in a file. You can choose any text editor for this task; for instance, Windows users may prefer Notepad++ over Notepad, while Linux users might opt for vim. In future sections, we will explore additional IDEs and tools to streamline YAML editing, but we will stick with the basics for now.

Step 1: Creating the YAML File

Open your terminal and use vim to create a file named pod.yaml:
Inside the file, define the following key elements:
  • apiVersion: Should be set to v1 for a Pod.
  • kind: Must be Pod (case-sensitive).
  • metadata: A dictionary that includes the pod’s name and any labels used for grouping.
  • spec: Contains the pod specifications, including a list of containers.
Be sure to follow proper indentation rules. Use two spaces per level (avoid tabs), as misalignment can lead to errors.
Below is a complete example configuration for a single-container Pod using the nginx image:
To add additional containers, insert another block within the containers list with the appropriate name and image.

Step 2: Saving and Verifying the YAML File

After editing the file, exit vim and save your changes by typing:
Verify the contents of your YAML file with:
The output should match the YAML configuration shown above.

Step 3: Creating the Pod in the Cluster

Create the Pod on your Kubernetes cluster using your YAML file. You can use either the kubectl create or kubectl apply command. Here’s an example with kubectl apply:
To check the status of your Pod, run:
Initially, you might see an output similar to this:
After a short while, re-running the command should show the Pod in a running state:
Example output:

Step 4: Inspecting the Pod Details

For a detailed overview of your Pod, use the kubectl describe command:
This command provides comprehensive details about the Pod, including container statuses, event logs, volumes, and node assignments. Below is an example of typical output:

Conclusion

This demonstration has guided you through creating a Kubernetes Pod using a YAML configuration file. This approach not only reinforces good configuration practices but also provides enhanced flexibility compared to command-based object creation. In our next lesson, we will cover advanced IDEs and tools to further ease YAML file management. For additional reading and resources, check out:

Watch Video