0
0
Kubernetesdevops~5 mins

Pod definition in YAML in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
A Pod is the smallest unit that runs one or more containers in Kubernetes. Defining a Pod in YAML lets you tell Kubernetes exactly what containers to run and how to run them.
When you want to run a single container or a tightly coupled group of containers together on the same machine.
When you need to test a simple application or service quickly without creating a full deployment.
When you want to specify container images, ports, and environment variables for your app in a clear file.
When you want to manually control the lifecycle of a container without automatic scaling.
When you want to learn Kubernetes basics by creating and managing Pods directly.
Config File - pod.yaml
pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
  labels:
    app: example
spec:
  containers:
  - name: example-container
    image: nginx:1.23.3
    ports:
    - containerPort: 80

apiVersion: Specifies the Kubernetes API version to use.

kind: Defines the resource type, here it is a Pod.

metadata: Contains the Pod's name and labels for identification.

spec: Describes the desired state, including containers to run.

containers: Lists container details like name, image, and ports.

Commands
This command creates the Pod in Kubernetes using the pod.yaml file. It tells Kubernetes to start the container as defined.
Terminal
kubectl apply -f pod.yaml
Expected OutputExpected
pod/example-pod created
This command lists all Pods in the current namespace to check if the Pod is running.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-pod 1/1 Running 0 10s
This command shows detailed information about the Pod, including container status, events, and resource usage.
Terminal
kubectl describe pod example-pod
Expected OutputExpected
Name: example-pod Namespace: default Priority: 0 Node: <node-name>/192.168.1.10 Start Time: Thu, 01 Jun 2023 12:00:00 +0000 Labels: app=example Status: Running IP: 10.244.1.5 Containers: example-container: Container ID: docker://abcdef123456 Image: nginx:1.23.3 Port: 80/TCP State: Running Started: Thu, 01 Jun 2023 12:00:05 +0000 Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 1m default-scheduler Successfully assigned default/example-pod to <node-name> Normal Pulled 1m kubelet Container image "nginx:1.23.3" already present on machine Normal Created 1m kubelet Created container example-container Normal Started 1m kubelet Started container example-container
Key Concept

If you remember nothing else from this pattern, remember: a Pod YAML file tells Kubernetes exactly what containers to run and how to run them.

Common Mistakes
Forgetting to specify the container image in the YAML file.
Without an image, Kubernetes cannot start the container, so the Pod will fail to run.
Always include the 'image' field under each container in the spec section.
Using incorrect indentation or syntax in the YAML file.
YAML is sensitive to spaces; wrong indentation causes parsing errors and prevents Pod creation.
Use consistent two-space indentation and validate YAML syntax before applying.
Not checking Pod status after creation.
The Pod might fail to start or crash, and without checking status, you won't know the problem.
Run 'kubectl get pods' and 'kubectl describe pod <pod-name>' to verify Pod health.
Summary
Create a Pod by writing a YAML file that defines container details like image and ports.
Use 'kubectl apply -f pod.yaml' to create the Pod in Kubernetes.
Check Pod status with 'kubectl get pods' and get detailed info with 'kubectl describe pod example-pod'.