📖 Scenario: You are working as a DevOps engineer. Your team needs a simple Kubernetes Pod to run a web server container. You will create a YAML file that defines this Pod.
🎯 Goal: Create a Kubernetes Pod definition in YAML with a single container running the nginx image on port 80.
📋 What You'll Learn
Create a YAML file with a Pod definition
Set the Pod name to web-server
Use the nginx image for the container
Expose container port 80
💡 Why This Matters
🌍 Real World
Kubernetes Pods are the smallest deployable units in Kubernetes. Defining Pods in YAML is essential for deploying applications in cloud environments.
💼 Career
DevOps engineers and cloud engineers often write and maintain Pod YAML files to manage application deployments and scaling.
Progress0 / 4 steps
1
Create the basic Pod structure
Create a YAML file with the root keys apiVersion, kind, and metadata. Set apiVersion to v1, kind to Pod, and metadata.name to web-server.
Kubernetes
Hint
Use indentation for metadata and name keys.
2
Add the container specification
Add a spec section with a containers list. Inside the list, add a container with name set to nginx-container and image set to nginx.
Kubernetes
Hint
Remember to use a dash - for list items under containers.
3
Expose the container port
Inside the container definition, add a ports list with one item that sets containerPort to 80.
Kubernetes
Hint
Use a dash - for the port item and indent properly.
4
Display the complete Pod YAML
Print the complete YAML content you created to verify the Pod definition.
Kubernetes
Hint
Use a print statement with the full YAML content as a string.
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
Step 1: Understand the YAML structure
The spec section in a Pod YAML defines the desired state, including containers and their configurations.
Step 2: Identify the role of other sections
metadata holds name and labels, apiVersion declares the API version, and namespace is set elsewhere.
Final Answer:
It defines the containers and their settings inside the Pod -> Option B
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
Step 1: Check correct YAML keys for containers
The correct keys are containers (list), each with name and image.
Step 2: Validate each option's syntax
containers:
- name: myapp
image: myimage:latest uses correct keys and indentation. Others use wrong keys or structure.
Final Answer:
containers:
- name: myapp
image: myimage:latest -> Option A
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?