This article demonstrates how to create a Kubernetes Pod using a YAML definition file for better control over pod specifications.
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.
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:
Copy
Ask AI
kubectl apply -f pod.yaml# Output:# pod/nginx created
To check the status of your Pod, run:
Copy
Ask AI
kubectl get pods
Initially, you might see an output similar to this:
Copy
Ask AI
NAME READY STATUS RESTARTS AGEnginx 0/1 ContainerCreating 0 7s
After a short while, re-running the command should show the Pod in a running state:
Copy
Ask AI
kubectl get pods
Example output:
Copy
Ask AI
NAME READY STATUS RESTARTS AGEnginx 1/1 Running 0 9s
For a detailed overview of your Pod, use the kubectl describe command:
Copy
Ask AI
kubectl describe pod nginx
This command provides comprehensive details about the Pod, including container statuses, event logs, volumes, and node assignments. Below is an example of typical output:
Copy
Ask AI
Initialized TrueReady TrueContainersReady TruePodScheduled TrueVolumes: default-token-f5ntk: Type: Secret (a volume populated by a Secret) SecretName: default-token-f5ntk Optional: falseQoS Class: BestEffortNode-Selectors: <none>Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300sEvents: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 21s default-scheduler Successfully assigned default/nginx to minikube Normal Pulling 20s kubelet, minikube Pulling image "nginx" Normal Pulled 14s kubelet, minikube Successfully pulled image "nginx" Normal Created 14s kubelet, minikube Created container nginx Normal Started 14s kubelet, minikube Started container nginx
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: