Implementing Declarative Infrastructure Configuration
In this lesson, you will learn how to shift from imperative "how-to-do-it" scripting to declarative "what-it-should-be" configuration management for your infrastructure.
Moving Beyond Imperative Commands
When managing Kubernetes and cloud environments, it is tempting to use CLI commands like kubectl scale deployment my-app --replicas=3 to achieve a goal. However, this is an imperative approach. It tells the system the steps to perform a change, which makes it difficult to audit, track in version control, or reproduce accurately if the cluster state deviates.
Declarative configuration reverses this pattern. Instead of giving instructions, you define the desired state of your environment in static files (like YAML manifests). The system—orchestrated by your CI/CD pipeline—then compares this "source of truth" against the actual running state of the cluster and performs the necessary actions to reconcile the difference.
Practical Application
Rather than executing a series of shell commands, you define the desired state in a version-controlled manifest. When you update the replicas field in your deployment.yaml, the pipeline applies the manifest to the cluster.
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
When you run kubectl apply -f deployment.yaml, Kubernetes performs a "three-way merge" patch. If the cluster already has two replicas, it adds one; if it has five, it deletes two. You never need to know the current state to reach the destination; the manifest defines the destination itself.
By treating these manifests as your primary interface, you gain consistency across development, staging, and production environments. This eliminates "configuration drift," where manual tweaks on a server or cluster make the environment diverge from what is stored in your repository. By grounding your deployments in static, declarative state, you ensure your infrastructure remains predictable, auditable, and easily recoverable.
Key Takeaway: Declarative configuration focuses on defining the end state, allowing the system to handle the reconciliation logic, which ensures your environment consistently matches your version-controlled source of truth.