Day 4: Deployments and Scaling in Kubernetes
Welcome to Day 4 of the Zero to Platform Engineer in 30 Days challenge! Now that we’ve covered the basics of Kubernetes, it’s time to explore one of its most powerful features: Deployments and Scaling. These tools let you manage your applications efficiently, ensuring they’re always running and able to handle demand.
What You’ll Learn Today
By the end of this post, you’ll be able to:
- Create a Kubernetes Deployment.
- Scale your application up and down.
- Perform a rolling update to deploy a new version.
- Roll back if something goes wrong.
Let’s get started! 🚀
Step 1: Create Your First Deployment
- Create a file called deployment.yaml with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: zero-to-platform-eng
spec:
replicas: 2
selector:
matchLabels:
app: zero-to-platform-eng
template:
metadata:
labels:
app: zero-to-platform-eng
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- Apply the Deployment to your cluster:
kubectl apply -f deployment.yaml
- Check if your Pods are running:
kubectl get pods
🎯 What’s happening?
Kubernetes creates 2 replicas of your application (as defined in replicas: 2), ensuring your app is always available.
Step 2: Scale Your Deployment
- Scale the Deployment up to 5 replicas:
kubectl scale deployment zero-to-platform-eng --replicas=5
- Verify the new number of Pods:
kubectl get pods
🎯 Pro tip: Scaling down works the same way:
kubectl scale deployment zero-to-platform-eng --replicas=1
Step 3: Perform a Rolling Update
- Update your application to use a new image version:
kubectl set image deployment/zero-to-platform-eng nginx=nginx:1.21.0
- Check the rollout status:
kubectl rollout status deployment/zero-to-platform-eng
🎯 What’s happening?
*Kubernetes performs a rolling update, gradually replacing old Pods with new ones to avoid downtime.
- Confirm the new Pods are running the updated version:
kubectl get pods -o wide
Roll Back a Deployment
- If something goes wrong, undo the update:
kubectl rollout undo deployment/zero-to-platform-eng
- Verify that the Deployment has reverted to the previous version:
kubectl rollout status deployment/zero-to-platform-eng
🎯 Pro tip: You can see the revision history of your Deployment with:
kubectl rollout history deployment/zero-to-platform-eng
Step 5: Clean Up (Optional)
When you’re done experimenting, delete the Deployment:
kubectl delete deployment zero-to-platform-eng
Activity for Today
Here’s your challenge for Day 4:
- Create a Deployment using the steps above.
- Scale it up and down and observe how Kubernetes manages the Pods.
- Perform a rolling update with a new image and roll it back if needed.
- Bonus: Try changing the replicas value directly in deployment.yaml and applying it again.
What’s Next?
You’ve now mastered the basics of Deployments and scaling! 🎉 Tomorrow, we’ll dive into Kubernetes Services, which enable communication between your Pods and the outside world.
👉 Check it out here: Zero to Platform Engineer Repository
Feel free to clone the repo, experiment with the code, and even contribute if you’d like! 🚀
Follow the Series!
🎉 Don’t miss a single step in your journey to becoming a Platform Engineer! 🎉
This post is just the beginning. Here’s what we’ve covered so far and what’s coming up next:
- Day 0: Introduction - What’s Platform Engineering?
- Day 1: Introduction to the CNCF Landscape
- Day 2: Day 2: Containers and Docker - The Building Blocks of Cloud Native
- Day 3: Containers and Kubernetes - The Building Blocks of Cloud Native
- Day 4: Deployments and Scaling in Kubernetes - Let’s Get Practical (You are here!)
👉 Bookmark this blog and check back every day for new posts in the series. 📣 Share your progress on social media with the hashtag #ZeroToPlatformEngineer to connect with other readers!