LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Kubernetes

Declarative Deployments & Scaling

When most developers first learn Kubernetes or Docker, they start by typing commands directly into the terminal. They might type something like kubectl run my-app --image=nginx. This is called the imperative approach. You are manually telling the system exactly how to do its job, step by step.

Declarative Deployments & Scaling: Mastering Kubernetes YAML

When most developers first learn Kubernetes or Docker, they start by typing commands directly into the terminal. They might type something like kubectl run my-app --image=nginx. This is called the imperative approach. You are manually telling the system exactly how to do its job, step by step.

However, in modern production environments, typing manual commands is dangerous. If a server crashes, there is no record of what command you typed three months ago to get it running. To solve this, we use declarative deployments. Instead of typing commands, you write a YAML file that declares the final state you want (e.g., "I want 3 copies of this application running"). You hand this file to Kubernetes, and the system figures out the rest. Stop writing CLI commands. Write YAML-based manifests, apply them to your cluster, and handle scaling and rolling updates seamlessly.

What you will learn
  • The difference between imperative commands and declarative YAML manifests.
  • How to write a standard Kubernetes Deployment manifest from scratch.
  • How to scale applications instantly by changing a single number in your file.
  • How Kubernetes processes these files using its Control Plane to ensure zero-downtime updates.

1. The Shift: Imperative vs. Declarative

To truly understand cloud-native system design, you must understand the difference between imperative and declarative systems. Think of imperative as driving a manual car—you shift every gear. Declarative is like giving an address to a taxi driver. You just state your destination, and the driver figures out the route, traffic, and turns.

Approach How It Works Real-World Analogy Best Used For
Imperative Running step-by-step CLI commands (e.g., kubectl scale, kubectl run). Baking a cake using a strict, step-by-step recipe. Quick local testing or debugging a broken pod.
Declarative Writing a YAML manifest and applying it (e.g., kubectl apply -f app.yaml). Ordering a cake from a bakery. You describe what you want, they make it happen. Production deployments, CI/CD pipelines, and version control (GitOps).

By keeping your infrastructure as code (YAML files) stored in Git, your entire team can track changes over time. If a deployment breaks, you simply revert to the previous YAML file.

2. Writing Your First Deployment Manifest

In Kubernetes, a Deployment is a higher-level concept that manages identical copies (called Replicas) of your application. Let's write a standard YAML manifest to deploy a web application.

Example 1: web-app-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-web-app
  labels:
    app: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.21.0
        ports:
        - containerPort: 80

Expected output after running `kubectl apply -f web-app-deployment.yaml`

deployment.apps/my-web-app created
Important: Notice the replicas: 3 line. This is the magic of declarative scaling. If a huge spike in traffic hits your site, you don't run a complex sequence of commands. You simply open this file, change 3 to 10, and run kubectl apply again. Kubernetes detects the difference and automatically spins up 7 new instances.

3. Architecture / Workflow Visualized

When you submit a declarative YAML file, it kicks off a highly automated, self-healing workflow inside the Kubernetes cluster. Let's look at how the data flows from your laptop into the actual worker nodes running your code.

The Declarative Deployment Workflow

Developer (YAML File) kubectl apply K8s API Server (Control Plane) Pod 1 (Running) Pod 2 (Running) Pod 3 (Running)

The declarative workflow: You submit the desired state (YAML) to the API Server. Kubernetes continuously checks the actual state against your desired state and spins up Pods to match it.

4. Step-by-Step Execution Breakdown

Understanding what happens after you hit Enter on your terminal is what separates junior developers from senior architects. Here is exactly how Kubernetes handles your YAML file.

Step 1: Submission & Validation You run kubectl apply -f manifest.yaml. The CLI sends the YAML payload to the Kubernetes API Server. The API Server authenticates your request, validates the YAML syntax, and stores this "Desired State" in its internal database (etcd).
Step 2: The Controller Manager Takes Over The Deployment Controller (a background loop monitoring the cluster) notices a new Desired State. It sees you asked for 3 replicas, but currently, 0 exist. It immediately creates a ReplicaSet to manage these pods.
Step 3: Scheduling & Execution The ReplicaSet requests 3 new Pods. The Kubernetes Scheduler looks at all the virtual machines (Nodes) in your cluster, finds the ones with enough CPU and RAM, and assigns the Pods to those machines. The container runtime (like containerd or Docker) then pulls the image and starts your application.
Step 4: Continuous Reconciliation This is the most crucial part. The Controller Manager doesn't just run once. It runs in a continuous loop. If one of your servers suddenly catches fire and a Pod dies, the Controller notices that the Actual State (2 pods) no longer matches the Desired State (3 pods). It will instantly spin up a new Pod on a healthy server to replace the lost one.
Watch out: Do not mix imperative scaling with declarative files! If your YAML says replicas: 3, but you manually run kubectl scale deployment my-web-app --replicas=5 in the terminal, the next time you apply your YAML file, Kubernetes will instantly kill 2 of your pods to force the state back down to 3. The YAML file must always be the single source of truth.

5. Hands-On Practice

To master declarative concepts, you need to write and modify manifests. Try these exercises in a local environment like Minikube or Docker Desktop.

Beginner Exercise 1: Scale Up Your App

You have a sudden influx of users. Modify the YAML file provided in Example 1 so that the application handles more traffic by running 5 concurrent instances.

Hint: Look for the `replicas` field under the `spec` block. Change the number, save the file, and run `kubectl apply -f` again.

Intermediate Exercise 2: Perform a Rolling Update

Your team just released a new version of the web app. Update your deployment to use version 1.22.0 of the nginx image without causing any downtime for your users.

Expected result: You change the `image: nginx:1.21.0` line to `nginx:1.22.0`. When you apply it, Kubernetes will start terminating old pods one by one, replacing them with new pods running the updated image, ensuring the site never goes offline.

Advanced Exercise 3: Rollback a Bad Deployment

Oh no! The new 1.22.0 image contains a critical bug and your app is crashing. Revert the cluster to the previous stable state declaratively.

Hint: You can simply undo the change in your Git repository (changing the image back to 1.21.0 in the YAML) and re-apply, OR use the built-in command `kubectl rollout undo deployment/my-web-app` if you are in a desperate rush.

6. Interview Questions

Hiring managers love asking about Kubernetes design philosophies. If you understand declarative state, you will breeze through infrastructure interviews.

Q1. Explain the difference between imperative commands and declarative configuration in Kubernetes.

Answer: Imperative commands instruct the system exactly *how* to perform an action step-by-step (e.g., "create a pod, now create another"). Declarative configuration allows the engineer to declare the *desired end state* in a YAML manifest (e.g., "I want 3 pods running"). Kubernetes takes this desired state, compares it to the actual state of the cluster, and automatically figures out the steps required to reconcile the two. Declarative is preferred for production because it is version-controllable and self-healing.

Q2. How do Kubernetes Deployments achieve zero-downtime (Rolling Updates) when a new image version is deployed?

Answer: When you update a Deployment manifest with a new container image and apply it, Kubernetes does not shut down all the old pods at once. Instead, it spins up a new ReplicaSet. It starts a new pod with the new image, waits for it to become healthy and ready to accept traffic, and only then terminates one of the old pods. It repeats this process one by one until all old pods are replaced, ensuring there is always at least one healthy pod serving user requests.

Conclusion

Moving from imperative terminal commands to declarative YAML manifests is the most critical mindset shift when adopting Kubernetes. By treating your infrastructure as code, you gain the ability to version control your deployments, scale massively by changing a single integer, and rely on Kubernetes' built-in control plane to keep your system healthy. Embrace the YAML—it is the language of modern cloud architecture.

Official Documentation

Upgradix by Shrash Studio – Learn Technology. Build Your Future.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.