Hands-on Lab·Certified Kubernetes Administrator
Declarative Versus Imperative Object Management
Create the same Deployment twice, once with a command and once with a file, then look at what each left behind on the object. One remembers what you asked for; the other does not. That difference is why apply can diff and create cannot.
Foundations Guide 5 of 103 Beginner
- Kubernetes1.36.4
- Cluster4 nodes
- Runtimecontainerd 2.2.6
- CNICalico v3.32.1
- TimeAbout 25 min
Temporary paths and timestamps in the diff output are from this run and will differ on yours.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| CKA1001 | 192.168.0.175 | Ubuntu 26.04 LTS | Control Plane Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE01 | 192.168.0.176 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE02 | 192.168.0.177 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE03 | 192.168.0.178 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when you are choosing between kubectl create and kubectl apply, or when a later apply overwrites something you set by hand. This matters because only one of them records what you asked for, and that record is what makes diffs and safe re-applies possible.
- generating YAML with
create --dry-run=client -o yamlrather than writing it from memory - creating imperatively and finding no last-applied annotation left behind
- applying declaratively and reading the annotation that records your intent
- running
kubectl diffto see a change before you make it - seeing the trap in mixing the two approaches on one object
Before you start
- A working cluster and
kubectl. - The kubectl session guide, mainly for
kubectl explain, which is how you find field names without looking them up. - The Pods guide, so the Deployment spec below is recognisable.
-
The imperative shortcut, and how to make it write YAML for you
-
Run it for real, then ask what it remembered
-
The declarative form, and the annotation it leaves behind
-
Why that annotation matters: kubectl diff
-
Which to use, and the trap in mixing them