How Does Kubernetes Turn YAML into Live Objects? A Deep Dive into Declarative APIs
This article explains how Kubernetes stores objects via the API and etcd, compares imperative and declarative management with kubectl commands and YAML files, describes the spec/status model, outlines the API server processing pipeline, and details the controller‑based reconciliation loop that drives desired state convergence.
Kubernetes Objects and API Overview
Kubernetes (K8s) stores all resource objects in etcd through the K8s API. Objects are created, modified, or deleted via the API, whether using the
kubectlCLI or programmatic clients.
Imperative vs Declarative Management
Three ways to create a Deployment running an Nginx container in the
testnamespace and scale it to five replicas are demonstrated:
Imperative command
<code>kubectl create deployment nginx-deployment --image nginx -n test</code> <code>kubectl scale deployment nginx-deployment --replicas 5 -n test</code>Imperative object configuration Create a nginx.yaml file, then run:
<code>kubectl create -f nginx.yaml</code>Modify the replicas field and apply:
<code>kubectl replace -f nginx.yaml</code>Declarative object configuration
<code>kubectl apply -f nginx.yaml</code>Declarative configuration stores the desired state in a YAML manifest, enabling audit trails and version control.
K8s Object Structure
Each object contains
spec(desired state) and
status(current state). The API server validates, stores, and persists objects in etcd. Core fields include
apiVersion,
kind,
metadata, and
spec.
API Server Processing Pipeline
When a YAML manifest is submitted, the API server performs:
Submission: POST request conversion.
Filtering & pre‑processing: authentication, authorization, audit.
Routing: match Group, Version, and Resource (e.g.,
/apis/batch/v2alpha1/cronjobs).
Creation: convert to a super‑version object, admit, validate, and store in the registry.
Persistence: serialize and write to etcd.
Controller Pattern and Reconciliation Loop
Kubernetes uses a controller pattern where controllers continuously compare
specand
status. If they differ, the controller takes actions to drive the system toward the desired state.
Sensors (Reflector, Informer, Indexer) watch the API server, cache objects, and feed events to controllers.
Example: Scaling a Deployment
The Reflector watches for Deployment changes, the Informer updates the cache and enqueues the key, and the worker compares
spec.replicaswith the actual pod count, creates new Pods if needed, and updates
statusuntil convergence.
Summary
Declarative APIs provide built‑in state tracking, making them the primary interaction model for Kubernetes.
In production, objects are defined in YAML;
specand
statusare the critical sections.
The controller‑based reconciliation loop, driven by declarative specifications, enables automated, self‑healing operations.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.