This article provides a guide on creating a Kubernetes pod using a YAML definition file.
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.
Below is a step-by-step walkthrough to guide you in creating a pod definition file and deploying it on your Kubernetes environment.
Begin by creating a new project called pod. 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.
When using PyCharm, you’ll notice a tree-view panel that displays the YAML file structure. This visualization confirms that 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.
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:
Under the spec 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:
1. Setup Folder Structure on the Kubernetes Master Node
Log in to your Kubernetes master node and create a directory structure for your demos:
Copy
Ask AI
root@kubemaster:/home/osboxes# mkdir demosroot@kubemaster:/home/osboxes# cd demosroot@kubemaster:/home/osboxes/demos# mkdir podroot@kubemaster:/home/osboxes/demos# cd pod
Before deploying your new pod, ensure there are no conflicting pods already running. If a pod was previously deployed with a command like kubectl run, delete it by executing:
Copy
Ask AI
root@kubemaster:/home/osboxes/demos/pod# kubectl get podsNAME READY STATUS RESTARTS AGEnginx-8586cf59-wf5r4 1/1 Running 0 59mroot@kubemaster:/home/osboxes/demos/pod# kubectl delete deployment nginx
Confirm the deletion by listing pods again:
Copy
Ask AI
root@kubemaster:/home/osboxes/demos/pod# kubectl get podsNo resources found.
root@kubemaster:/home/osboxes/demos/pod# kubectl create -f pod-definition.ymlpod "myapp-pod" created
You can monitor the pod’s creation by running:
Copy
Ask AI
root@kubemaster:/home/osboxes/demos/pod# kubectl get podsNAME READY STATUS RESTARTS AGEmyapp-pod 0/1 ContainerCreating 0 8s
After a few moments, check the pod status again:
Copy
Ask AI
root@kubemaster:/home/osboxes/demos/pod# kubectl get podsNAME READY STATUS RESTARTS AGEmyapp-pod 1/1 Running 0 19s
This confirms the pod has been successfully created and is now running in your cluster.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!