
Module 3 Exercises: Working with Objects
From YAML to YAML. Build your first full stack on Kubernetes by creating Pods, Deployments, Services, and persistent storage.
Module 3 Exercises: Working with Objects
In Module 3, we moved from architecture to action. You now understand how to define the core building blocks of any Kubernetes application. These exercises will guide you through the process of creating a simple, stateful application stack.
Exercise 1: The "Simple AI" Pod
Create a YAML manifest for a Pod named ai-hello-world.
- Image: Use
python:3.11-slimor your own test image. - Environment Variable: Add a variable
MODEL_TYPEwith valueexperimental. - Resources: Set a CPU request of
100mand a memory request of128Mi. - Verification: Once applied, run
kubectl get pod ai-hello-world -o yamland find theuidof the pod.
Exercise 2: Scaling the AI Engine
Convert your single Pod into a Deployment.
- Deployment Name:
ai-engine-deployment - Replicas: 3
- Update Strategy: RollingUpdate with 0 unavailable pods.
- Labels: Use
app: ai-enginefor both the selector and the pod template. - Scaling: Use
kubectl scaleto increase the replicas to 5, then back to 3.
Exercise 3: Exposing the Internal Service
Create a Service to allow other pods to talk to your ai-engine-deployment.
- Name:
ai-engine-service - Type:
ClusterIP - Ports: Map port
80to the target port8000. - Verification: Run
kubectl get endpoints ai-engine-service. Do you see multiple IP addresses? Why?
Exercise 4: Persistent Data Storage
Create a PersistentVolumeClaim for a database.
- Name:
db-storage-pvc - Size:
1Gi - Access Mode:
ReadWriteOnce - Integration: Add this PVC as a volume to a new Deployment named
db-deployment. Mount it at/var/lib/mysqlor similar.
Solutions (Self-Check)
Exercise 1 Strategy:
Your YAML should look like this in the spec section:
containers:
- name: hello
image: python:3.11-slim
env:
- name: MODEL_TYPE
value: experimental
resources:
requests:
cpu: 100m
memory: 128Mi
Exercise 2 Hint:
The kubectl scale commands are:
kubectl scale deployment ai-engine-deployment --replicas=5kubectl scale deployment ai-engine-deployment --replicas=3
Exercise 3 Solution:
Yes, you should see multiple IPs in the Endpoints list! One IP for each "Ready" pod managed by the deployment. The Service uses these to load balance traffic.
Exercise 4 Hint:
Inside the spec.template.spec of your Deployment:
volumes:
- name: db-data
persistentVolumeClaim:
claimName: db-storage-pvc
containers:
- name: db-container
image: mysql
volumeMounts:
- name: db-data
mountPath: /var/lib/mysql
Summary of Module 3
Congratulations! You are no longer just a theorist; you are a Practitioner.
- You can define Pods and understand their lifecycle.
- You can manage Deployments and update them with zero downtime.
- You can build stable gateways using Services.
- You can manage Secrets and ConfigMaps.
- You can give your apps a permanent memory with PVCs.
In Module 4: Deploying Applications, we will learn how to automate this process and manage complex, multi-container applications in the real world.