Welcome to this comprehensive guide on creating a Kubernetes pod using a YAML definition file. In this article, you will learn how to write a YAML file from scratch and deploy it into a Kubernetes cluster. You can create YAML files using any text editor, such as Notepad, Notepad++, or Atom, or even an integrated development environment (IDE) like PyCharm. I recommend PyCharm for its excellent YAML support. If you’re interested, visit PyCharm by JetBrains to download the Professional or free Community edition.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.


Creating the Pod Definition File
1. Create a New Project and File
Begin by creating a new project calledpod. Inside this project, create a file named pod-definition.yaml. Every Kubernetes YAML file typically includes the following root-level properties: apiVersion, kind, metadata, and spec. For a pod, set the apiVersion to v1 and kind to Pod.
2. Define the Metadata
In themetadata section, provide essential details such as the pod’s name and associated labels for grouping. For instance:
apiVersion, kind, metadata, and spec are at the same root level. Make sure the name and labels entries have identical indentation. Incorrect indentation might mistakenly nest labels under name, leading to errors.
3. Adding Optional Labels
Enhance your pod’s metadata by adding extra labels, such as cost center or geographical location, to better organize your resources. Here’s an example:4. Define the Pod Spec
Under thespec section, specify the container details. The containers field accepts an array; each container is introduced by a dash (-) followed by its configuration, such as name and image. The example below defines a pod with a single container running an Nginx image:
For this demonstration, we are only using one container.
Deploying the Pod
After finalizing yourpod-definition.yaml, follow these instructions to deploy it to your Kubernetes cluster.
1. Setup Folder Structure on the Kubernetes Master Node
Log in to your Kubernetes master node and create a directory structure for your demos:2. Create the YAML File
Paste the contents of yourpod-definition.yaml into a new file on the master node. For instance, execute:
cat command.
3. Clean Up Existing Pods
Before deploying your new pod, ensure there are no conflicting pods already running. If a pod was previously deployed with a command likekubectl run, delete it by executing:
4. Create the Pod
Now, create the pod with the following command:That concludes our guide on creating a Kubernetes pod using a YAML file. In the next article, we’ll explore additional tips for managing YAML files in PyCharm. Happy coding and deployment!