Bird
Raised Fist0
Kubernetesdevops~5 mins

Pod definition in YAML in Kubernetes - Commands & Configuration

Choose your learning style10 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'.

Practice

(1/5)
1. What is the purpose of the spec section in a Kubernetes Pod YAML definition?
easy
A. It declares the Kubernetes API version used
B. It defines the containers and their settings inside the Pod
C. It specifies the Pod's name and labels
D. It sets the namespace for the Pod

Solution

  1. Step 1: Understand the YAML structure

    The spec section in a Pod YAML defines the desired state, including containers and their configurations.
  2. Step 2: Identify the role of other sections

    metadata holds name and labels, apiVersion declares the API version, and namespace is set elsewhere.
  3. Final Answer:

    It defines the containers and their settings inside the Pod -> Option B
  4. Quick Check:

    Pod spec = container details [OK]
Hint: Remember: spec section = container setup inside Pod [OK]
Common Mistakes:
  • Confusing metadata with spec
  • Thinking apiVersion defines containers
  • Mixing namespace with spec
2. Which of the following is the correct way to specify a container image in a Pod YAML?
easy
A. containers: - name: myapp image: myimage:latest
B. containers: - imageName: myimage:latest containerName: myapp
C. container: name: myapp image: myimage:latest
D. containers: - myapp: image: myimage:latest

Solution

  1. Step 1: Check correct YAML keys for containers

    The correct keys are containers (list), each with name and image.
  2. Step 2: Validate each option's syntax

    containers: - name: myapp image: myimage:latest uses correct keys and indentation. Others use wrong keys or structure.
  3. Final Answer:

    containers: - name: myapp image: myimage:latest -> Option A
  4. Quick Check:

    Container image key = image [OK]
Hint: Use 'name' and 'image' keys inside 'containers' list [OK]
Common Mistakes:
  • Using wrong keys like imageName or containerName
  • Incorrect indentation or singular 'container'
  • Nesting keys incorrectly under container name
3. Given this Pod YAML snippet, what will be the container's name and image?
spec:
  containers:
  - name: frontend
    image: nginx:1.21
medium
A. Container name is 'nginx' and image is 'frontend:1.21'
B. Container name is 'frontend' and image is 'nginx:latest'
C. Container name is 'frontend' and image is 'nginx:1.21'
D. Container name and image are not defined

Solution

  1. Step 1: Read the container name

    The YAML shows name: frontend, so container name is 'frontend'.
  2. Step 2: Read the container image

    The image is nginx:1.21, exactly as specified.
  3. Final Answer:

    Container name is 'frontend' and image is 'nginx:1.21' -> Option C
  4. Quick Check:

    Name and image match YAML exactly [OK]
Hint: Match keys 'name' and 'image' exactly as YAML shows [OK]
Common Mistakes:
  • Swapping name and image values
  • Assuming default image tag 'latest'
  • Ignoring YAML indentation and keys
4. Identify the error in this Pod YAML snippet:
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: app
    image: nginx
  - name: app
    image: redis
medium
A. Duplicate container names are not allowed in a Pod
B. Missing apiVersion field
C. Incorrect indentation under containers
D. Pod kind should be Deployment

Solution

  1. Step 1: Check container names uniqueness

    Both containers have the name 'app', which is not allowed because container names must be unique within a Pod.
  2. Step 2: Verify other fields

    apiVersion and kind are present and indentation is correct. Pod kind is valid.
  3. Final Answer:

    Duplicate container names are not allowed in a Pod -> Option A
  4. Quick Check:

    Unique container names required [OK]
Hint: Check container names are unique inside Pod spec [OK]
Common Mistakes:
  • Ignoring duplicate container names
  • Confusing Pod with Deployment kind
  • Misreading indentation as error
5. You want to create a Pod with two containers: one running nginx and another running redis. Which YAML snippet correctly defines this Pod?
hard
A. apiVersion: v1 kind: Pod metadata: name: multi-container-pod spec: containers: - nginx - redis
B. apiVersion: v1 kind: Pod metadata: name: multi-container-pod spec: container: - name: nginx image: nginx - name: redis image: redis
C. apiVersion: v1 kind: Pod metadata: name: multi-container-pod spec: containers: name: nginx image: nginx name: redis image: redis
D. apiVersion: v1 kind: Pod metadata: name: multi-container-pod spec: containers: - name: nginx-container image: nginx - name: redis-container image: redis

Solution

  1. Step 1: Verify correct key for multiple containers

    The key must be containers (plural) with a list of container objects.
  2. Step 2: Check each container has unique name and image

    apiVersion: v1 kind: Pod metadata: name: multi-container-pod spec: containers: - name: nginx-container image: nginx - name: redis-container image: redis correctly lists two containers with unique names and images. Other options have syntax errors or wrong keys.
  3. Final Answer:

    apiVersion: v1 kind: Pod metadata: name: multi-container-pod spec: containers: - name: nginx-container image: nginx - name: redis-container image: redis -> Option D
  4. Quick Check:

    Use 'containers' list with unique names [OK]
Hint: Use 'containers' plural with list of named containers [OK]
Common Mistakes:
  • Using singular 'container' instead of 'containers'
  • Not giving unique names to containers
  • Incorrect YAML list syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard