2025-10-06

Kubernetes Declarative Config: apply, diff, and Review

Understand declarative configuration in Kubernetes, why desired state matters, and how apply, diff, and reconciliation shape safe operations.

Kubernetes uses declarative configuration to record desired state. Keeping that configuration in YAML files lets a team review changes, apply manifests repeatedly, and roll back to an earlier version when needed.

What declarative config records

Declarative manifests record desired state in a reviewable, repeatable form:

  • the desired state is written down
  • the source of truth is reviewable
  • changes are repeatable
  • the cluster can be reconciled back to that known state

kubectl apply submits this desired state to the API; controllers then reconcile live resources toward it.

A minimal multi-object example

apiVersion: v1
kind: Namespace
metadata:
  name: demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  namespace: demo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: nginx:1.25

The normal workflow

kubectl diff -f app.yaml
kubectl apply -f app.yaml
kubectl get -f app.yaml

That order matters. Look at the change, apply the change, then observe the resulting state.

Why this approach is safer

  • changes are reviewable in git
  • reapplying is usually safe and idempotent
  • rollback can be tied to a previous commit or tag
  • drift becomes easier to reason about

This also makes handover easier. Another operator can understand what you intended without replaying your shell history.

Common declarative habits worth building

  • keep namespace explicit
  • keep labels consistent
  • split manifests by concern
  • use kubectl diff before apply
  • define a rollback path in advance

Small habits here reduce a surprising number of production mistakes.

apply vs patch

Use apply when you are managing the whole desired state of a resource.

Use patch when you truly need a narrow targeted edit.

kubectl patch deploy api -p '{"spec":{"replicas":3}}'

In day-to-day production work, it is usually healthier to let the desired state live in files and use patch sparingly.

Drift: why manual edits hurt later

Manual changes make live cluster state and the Git manifest two competing sources of truth, leaving future operators unsure which one to follow.

Once that happens, operators stop knowing whether the manifest or the live cluster is the real answer.

That is why declarative configuration connects naturally to:

Those pages explain why Kubernetes keeps reconciling state and why drift becomes so expensive.

Useful commands beyond apply

Diff before write

kubectl diff -f app.yaml

Dry-run on the server

kubectl apply -f app.yaml --dry-run=server

Apply a directory

kubectl apply -f ./manifests

Apply a Kustomize overlay

kubectl apply -k overlays/prod

Ordering matters more than people think

A clean order often looks like:

  1. namespaces
  2. RBAC and policy
  3. config and secrets
  4. workloads
  5. services and exposure

If the order is messy, the resulting errors often look more mysterious than they really are.

What to review before apply

  • labels and selectors still line up
  • namespace is correct
  • immutable fields are not being changed accidentally
  • resource requests exist where needed
  • secrets are handled separately and safely
  • rollback target is clear

Three apply decisions

Q: Is declarative config only useful if I do full GitOps? A: No. GitOps is one strong form of declarative operations, but even without a controller, reviewed manifests and repeatable apply flows are a major improvement.

Q: Why does drift matter so much? A: Because once manifests and live state disagree, recovery, rollback, and review all become slower and less trustworthy.

Q: When should I worry about server-side apply? A: When multiple tools or teams manage the same resources and field ownership starts causing conflicts.

Add change governance next

Config check before merge

Keeping desired state in version control lets the team review changes, reproduce configuration, and restore a known version when needed.

References