In this lesson, you will learn how to shift from telling Kubernetes how to perform tasks to telling it what you want the final state to look like.
In your previous work with Docker Swarm and early Kubernetes interactions, you might have felt like a technician issuing direct commands, such as "create this container" or "scale this service." This is known as an imperative approach. While it works for simple tasks, it becomes difficult to manage as systems grow.
The Declarative API Model flips this relationship. Instead of issuing commands, you write a "blueprint"—a YAML manifest—that describes the state you want the cluster to be in. You submit this file to the Kubernetes API server, and Kubernetes takes responsibility for making it happen.
Think of it like ordering a meal at a restaurant.
- Imperative: You walk into the kitchen, tell the chef to chop the onions, sear the steak, and boil the water, hovering over them to ensure they follow your exact steps.
- Declarative: You simply hand the chef a menu order. You define the desired outcome (a medium-rare steak), and the chef uses their expertise to decide the best way to get there.
If you want two replicas of an application, you don't run a "start container" command twice. You define a manifest with replicas: 2:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 2
template:
# ... container details
Once applied, the Kubernetes Control Plane (the "chef") constantly compares this desired state against the actual state of the cluster. If one container crashes, the controller notices the mismatch and automatically creates a new one to match your original declaration. You don’t need to manually intervene or issue "restart" commands; the system maintains the state for you. By using this model, you create a "source of truth" that you can version control, share, and audit, ensuring your infrastructure stays exactly as you intended.
Key Takeaway: The Declarative API model allows you to define your infrastructure as a persistent "desired state" that Kubernetes continuously reconciles, removing the need for manual, step-by-step operational commands.