Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔍 Introduction
One of the foundational pillars of GitOps—and modern
DevOps practices in general—is declarative management. Instead of
manually scripting commands or modifying systems imperatively, you declare
what you want the system to look like, and an automated engine ensures that
reality matches your desired configuration.
In this chapter, you'll learn:
Let’s explore how declarative thinking supercharges GitOps
workflows.
🧠 What Does
"Declarative Management" Mean?
In declarative management, you describe the
desired state of your system, and a controller or automation tool
ensures that the system converges to that state.
You don't tell the system how to reach the state —
only what the state should be.
📋 Declarative vs
Imperative Comparison
Aspect |
Declarative |
Imperative |
Approach |
Define the desired
final state |
Specify step-by-step
commands |
Focus |
What the
system should look like |
How to
achieve the desired state |
Example |
YAML manifest
declaring 3 replicas |
kubectl scale
deployment myapp --replicas=3 |
Fault Tolerance |
High (system
ensures state) |
Low (manual
corrections needed) |
🔥 Why Declarative is
Better for GitOps
🧱 Writing Declarative
Infrastructure Configurations
Let's start with the basics: Infrastructure as Code (IaC).
🔹 Popular Declarative IaC
Tools
Tool |
Purpose |
Terraform |
Cloud infrastructure
(AWS, GCP, Azure) |
Pulumi |
Cloud infra
using programming languages |
Crossplane |
Kubernetes-native
cloud management |
AWS CDK |
Infrastructure
using code in languages like TypeScript, Python |
📋 Sample Terraform for an
AWS EC2 Instance
h
CopyEdit
resource
"aws_instance" "web" {
ami
= "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
}
}
This defines what infrastructure you want, not how to
create it.
Apply it:
bash
CopyEdit
terraform
apply
Terraform will calculate the steps needed and execute
them.
🧰 Writing Declarative
Application Configurations
Applications, especially containerized apps, are declared
using Kubernetes manifests.
📄 Basic Kubernetes
Deployment Manifest
yaml
CopyEdit
apiVersion:
apps/v1
kind:
Deployment
metadata:
name: my-nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
Kubernetes will create or update the system to match this
declaration.
🛠️ Managing Configuration
Complexity
When managing multiple apps and environments, raw YAML files
quickly become messy. To avoid that:
🔹 1. Use Kustomize
Base:
yaml
CopyEdit
#
deployment.yaml
apiVersion:
apps/v1
kind:
Deployment
metadata:
name: app
spec:
replicas: 1
template:
spec:
containers:
- name: app
image: my-app:v1
Overlay for production:
yaml
CopyEdit
#
overlays/prod/kustomization.yaml
bases:
-
../../base
patchesStrategicMerge:
-
replica-patch.yaml
replica-patch.yaml:
yaml
CopyEdit
spec:
replicas: 5
Command to apply:
bash
CopyEdit
kubectl
apply -k overlays/prod/
🔹 2. Use Helm Charts
Example:
yaml
CopyEdit
#
values.yaml
replicaCount:
3
image:
repository: nginx
tag: 1.25
Install:
bash
CopyEdit
helm
install my-release my-chart/
Helm makes upgrades, rollbacks, and parameterized
deployments easy.
🔹 3. Split Configurations
by Environment
Organize Git repos like this:
bash
CopyEdit
environments/
dev/
app-config.yaml
staging/
app-config.yaml
prod/
app-config.yaml
Environment-specific overlays allow tuning replicas,
secrets, and resource limits separately.
🔐 Declarative Management
of Secrets
Managing secrets declaratively is tricky but essential.
🔒 Popular Options
Tool |
Approach |
Sealed-Secrets |
Encrypt Kubernetes
Secrets for safe Git storage |
SOPS + Git-crypt |
Encrypt YAML
files inside Git repo |
External Secrets
Operator |
Fetch secrets from AWS
Secrets Manager, Vault |
Example: Sealed-Secret YAML
yaml
CopyEdit
apiVersion:
bitnami.com/v1alpha1
kind:
SealedSecret
metadata:
name: db-password
spec:
encryptedData:
password: AgA3Jv0b5...
SealedSecret is safe to store and commit to Git!
📡 Applying Declarative
GitOps Workflows
Bringing everything together:
🔥 Example: Full GitOps
Folder Layout
bash
CopyEdit
gitops/
├── apps/
│ ├── base/
│ │ ├── deployment.yaml
│ │ └── service.yaml
│ └── overlays/
│ ├── dev/
│ ├──
staging/
│ └── prod/
├── infrastructure/
│ ├── vpc/
│ ├──
eks-cluster/
│ └── s3-buckets/
├── secrets/
│ ├── db-sealedsecret.yaml
│ └──
api-keys-sealedsecret.yaml
📚 Declarative Management
in Multi-Cluster, Multi-Cloud Environments
Organizations often need to manage:
With declarative configs:
🚀 Summary: What You
Learned in Chapter 3
Answer: GitOps is a set of practices that use Git
repositories as the single source of truth for managing infrastructure and
application configurations. Changes are made by updating Git, and automated
systems then synchronize the live system to match the Git repository.
Answer: While both GitOps and IaC involve defining
infrastructure using code, GitOps emphasizes automated synchronization, continuous
reconciliation, and operations managed entirely through Git workflows—including
deployments, rollbacks, and drift detection.
Answer: Popular GitOps tools include:
Answer: Yes. While GitOps originated with Kubernetes,
the principles can be applied to any system that supports declarative
infrastructure (e.g., cloud resources using Terraform, databases, serverless
deployments, and even networking configurations).
Answer: Rollbacks in GitOps are simple—just revert
the Git commit (or use Git history to reset configurations) and the GitOps
controller will automatically reconcile the live environment back to that
previous, stable state.
Answer: GitOps enhances security by:
Answer: Common challenges include:
Answer: GitOps tools like ArgoCD or Flux continuously reconcile the live environment against the Git state. If drift is detected, they can either:
Answer: No. GitOps can be beneficial for small
startups, medium businesses, or large enterprises alike. Whether you're
managing a handful of services or hundreds, GitOps provides automation,
reliability, and clear operational visibility at all scales.
Answer: You can (and should) implement GitOps incrementally. Start with:
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)