Kahibaro
Discord Login Register

3.6 Declarative configuration model

Imperative vs Declarative in Kubernetes

In Kubernetes, the declarative configuration model means you describe what the desired state of the system should be, and the system figures out how to make it so.

This contrasts with imperative usage, where you directly tell Kubernetes what action to perform step by step.

Kubernetes’ controllers then work continuously to match the actual state of the cluster to the desired state you declared.

Desired vs Actual State

The declarative model is built around the idea of desired state:

Conceptually:

This model makes Kubernetes:

Resource Manifests as Source of Truth

In the declarative model, Kubernetes resources are usually defined in manifest files (often YAML):

A Kubernetes manifest typically includes:

Example (simplified) Deployment manifest:

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

In this file you’re not giving step-by-step instructions; you are declaring:

Idempotence and Repeated Application

A key property of the declarative model is idempotence:

This enables workflows like:

Using `kubectl` Declaratively

Kubernetes supports a declarative workflow through commands like:

kubectl apply:

Common patterns:

Partial vs Full Specifications

In declarative manifests, you typically:

Over-specifying everything can:

Common practice:

Patching and Declarative Updates

While kubectl apply is the main declarative tool, Kubernetes also supports patching resources:

Example:

kubectl patch deployment myapp \
  -p '{"spec":{"replicas":5}}'

This is somewhat between imperative and declarative:

In a strictly declarative practice, you’d usually:

Declarative Model and GitOps

The declarative configuration model is a foundation for GitOps practices:

This builds on Kubernetes’ native reconciliation:

Advantages and Trade-Offs

Advantages:

Trade-offs:

Typical mitigation:

Practical Guidelines

When working with Kubernetes in a declarative way:

These practices let you fully benefit from Kubernetes’ declarative configuration model and its reconciliation-driven behavior.

Views: 123

Comments

Please login to add a comment.

Don't have an account? Register now!