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:

  1. Create a Kubernetes Deployment.
  2. Scale your application up and down.
  3. Perform a rolling update to deploy a new version.
  4. Roll back if something goes wrong.

Let’s get started! 🚀

Step 1: Create Your First Deployment

  1. 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
  1. Apply the Deployment to your cluster:
kubectl apply -f deployment.yaml
  1. 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

  1. Scale the Deployment up to 5 replicas:
kubectl scale deployment zero-to-platform-eng --replicas=5
  1. 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

  1. Update your application to use a new image version:
kubectl set image deployment/zero-to-platform-eng nginx=nginx:1.21.0
  1. 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.

  1. Confirm the new Pods are running the updated version:
kubectl get pods -o wide

Roll Back a Deployment

  1. If something goes wrong, undo the update:
kubectl rollout undo deployment/zero-to-platform-eng
  1. 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:

  1. Create a Deployment using the steps above.
  2. Scale it up and down and observe how Kubernetes manages the Pods.
  3. Perform a rolling update with a new image and roll it back if needed.
  4. 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:

👉 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!

Subscribe to Alex Parra Newsletter

One update per month. No spam.