GitOps
Learn how to use Git as the single source of truth for infrastructure and application configuration.
GitOps Workflowโ
GitOps Architecture and Flowโ
What is GitOps?โ
GitOps is a developer-centric approach to continuous delivery where Git is the single source of truth for both application code and infrastructure configuration. Changes go through Git workflows (pull requests, code review, merge) rather than direct deployments.
Core Principlesโ
- Git as Source of Truth โ All desired state is declared in Git
- Declarative Configuration โ Describe desired state, not commands
- Version Control โ All changes tracked with full history
- Automated Reconciliation โ Controllers continuously sync actual state to desired state
- Human Review โ All changes go through pull request review
Traditional Deployment vs GitOpsโ
Traditional Deployment (Push-based)โ
Developer commits code
โ
CI Pipeline builds artifact
โ
CI Pipeline pushes to production
โ
Application runs
Issues:
- Credentials stored on CI/CD server
- Limited visibility into what's deployed
- Difficult to replicate deployments
- Hard to track configuration changes
GitOps Deployment (Pull-based)โ
Developer commits code + config to Git
โ
CI Pipeline validates and builds artifact
โ
Developer merges to main branch
โ
GitOps Controller detects change in Git
โ
GitOps Controller pulls latest from Git
โ
Controller syncs cluster to match Git
Advantages:
- Git has full audit trail
- Configuration as code
- Easy rollback (revert commit)
- Self-healing (controller fixes drift)
- Better security (credentials on cluster, not CI/CD)
Key Componentsโ
1. Git Repository Structureโ
Typical GitOps repo layout:
gitops-repo/
apps/
app-1/
base/
deployment.yaml
service.yaml
kustomization.yaml
overlays/
dev/
kustomization.yaml
staging/
kustomization.yaml
prod/
kustomization.yaml
infrastructure/
networking/
network-policies.yaml
storage/
storage-classes.yaml
README.md
2. Application Manifestsโ
Kubernetes manifests describing desired state:
# deployment.yaml
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: myapp:v1.2.3
ports:
- containerPort: 8080
3. GitOps Controllerโ
Continuously monitors Git repository and reconciles cluster state:
- ArgoCD โ Kubernetes-native GitOps controller
- Flux โ Lightweight GitOps controller
- Jenkins X โ Kubernetes-native CI/CD
- GitKube โ Kubernetes-native Git push deployment
4. Synchronizationโ
Controller detects differences and applies changes:
Git Desired State: app:v2.0
โ
Diff Check
โ
Cluster State: app:v1.0
When diff detected, controller reconciles cluster to match Git.
GitOps Workflowโ
Step 1: Update Configurationโ
Developer updates deployment configuration in Git:
git checkout -b update-app-version
# Edit app deployment version in deployment.yaml
git commit -am "Update app to v1.2.3"
git push origin update-app-version
Step 2: Code Reviewโ
Create pull request for review:
Pull Request: Update app to v1.2.3
- Changes app image from v1.2.2 to v1.2.3
- Approved by lead engineer
- CI/CD validates YAML syntax
Step 3: Mergeโ
Reviewer approves and merges to main:
git checkout main
git merge --no-ff update-app-version
git push origin main
Step 4: Automatic Syncโ
GitOps controller detects change and syncs cluster:
Controller polling Git repository...
Latest commit: Update app to v1.2.3
Desired state: app:v1.2.3
Current state: app:v1.2.2
Status: OUTOFSYNCH
โ Applying changes to cluster
โ Rolling out new deployment
โ Waiting for pods to become healthy
Status: SYNCHED
Step 5: Verificationโ
Monitor deployment status:
argocd app get myapp
โ Status: Synced
โ Desired commit: abc123def
โ Live commit: abc123def
โ Replicas: 3/3 running
ArgoCDโ
Popular GitOps tool for Kubernetes.
Installationโ
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# Get initial password
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
Creating an Applicationโ
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: myapp
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/user/gitops-repo.git
targetRevision: main
path: apps/myapp/overlays/prod
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: true
Key Featuresโ
- Multi-cluster โ Deploy to multiple clusters from single Git source
- Kustomize/Helm support โ Use templating tools
- Progressive sync โ Sync waves, hooks for ordered deployment
- Diff visualization โ See exactly what will change
- Rollback โ Revert to previous Git commit
- Notifications โ Slack, email on sync events
Fluxโ
Lightweight GitOps toolkit for Kubernetes.
Installationโ
curl -s https://fluxcd.io/install.sh | sudo bash
flux install --version=latest
Creating Sourcesโ
Define where to get configuration:
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: GitRepository
metadata:
name: myapp-repo
namespace: flux-system
spec:
interval: 1m0s
url: https://github.com/user/gitops-repo.git
ref:
branch: main
Creating Kustomizationsโ
Define what to deploy:
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: myapp
namespace: flux-system
spec:
interval: 10m
sourceRef:
kind: GitRepository
name: myapp-repo
path: ./apps/myapp
prune: true
wait: true
Push vs Pull Deploymentโ
Push Deployment (Traditional)โ
- CI/CD pushes directly to cluster
- Faster feedback (immediate)
- Credentials on CI/CD server
- Hard to manage drift
- Difficult for multiple clusters
Pull Deployment (GitOps)โ
- Controller pulls from Git
- Slight delay (polling interval)
- Credentials on cluster only
- Self-healing from drift
- Consistent multi-cluster management
Repository Structure Patternsโ
Pattern 1: Kustomize-basedโ
repo/
apps/
myapp/
base/
deployment.yaml
kustomization.yaml
overlays/
dev/kustomization.yaml
prod/kustomization.yaml
Pattern 2: Helm-basedโ
repo/
releases/
myapp-dev.yaml # HelmRelease for dev
myapp-prod.yaml # HelmRelease for prod
charts/
myapp/
templates/
values.yaml
Pattern 3: Environment-per-directoryโ
repo/
development/
namespace.yaml
apps/
app1/deployment.yaml
app2/deployment.yaml
staging/
namespace.yaml
apps/
app1/deployment.yaml
production/
namespace.yaml
apps/
app1/deployment.yaml
Pattern 4: Mono-repoโ
repo/
infrastructure/
networking/
storage/
applications/
app-1/
src/
charts/
deployments/
app-2/
src/
charts/
deployments/
GitOps Best Practicesโ
1. Keep Git Repository Cleanโ
- Only store declarative configuration
- Don't store binary files or large assets
- Organize logically
- Clear naming conventions
2. Environment Parityโ
- Keep dev, staging, prod configs as similar as possible
- Use overlays for differences
- Minimize custom per-environment logic
3. Version Control Everythingโ
- Application code in Git
- Infrastructure code in Git
- Configuration in Git
- Deployment manifests in Git
4. Implement Proper Code Reviewโ
Feature branch โ Pull request โ Review โ Approval โ Merge โ Auto-deploy
5. Use RBAC for Git Accessโ
- Developers can propose changes
- Team leads approve changes
- Automated tools can't bypass review
6. Monitor Sync Statusโ
- Watch for OutOfSync applications
- Alert when sync fails
- Track sync frequency and duration
- Monitor controller health
7. Plan Rollback Strategyโ
Rollback = Revert Git commit
git revert abc123def
git push origin main
โ Controller automatically syncs cluster
8. Secrets Managementโ
Don't store secrets in Git directly!
Options:
- Sealed Secrets โ Encrypt secrets in Git
- External Secrets Operator โ Fetch from vault
- ArgoCD Vault โ Store secrets in separate system
- CI/CD integration โ Inject at deployment time
Exercisesโ
Exercise 1: Basic GitOps Setupโ
Set up ArgoCD and create an application that:
- Syncs from a Git repository
- Deploys a simple web application
- Automatically syncs when Git changes
- Shows application status in ArgoCD UI
Exercise 2: Multi-environment Deploymentโ
Create a GitOps structure for dev, staging, and prod that:
- Uses Kustomize overlays
- Has different replica counts per environment
- Uses different resource limits
- Shares common base manifests
Exercise 3: Implement Self-healingโ
Configure ArgoCD to:
- Detect drift between Git and cluster
- Automatically sync when drift detected
- Prune resources not in Git
- Prevent manual changes by overwriting them
Exercise 4: Secrets Managementโ
Implement secret management where:
- Database passwords not stored in Git
- Secrets encrypted in Git (Sealed Secrets)
- Secrets rotated without Git changes
- Different secrets per environment
Comparison: ArgoCD vs Fluxโ
| Aspect | ArgoCD | Flux |
|---|---|---|
| UI Dashboard | Yes (excellent) | No |
| Complexity | More features, steeper learning curve | Lightweight, simpler |
| GitOps Model | Pull-based | Pull-based |
| Multi-cluster | Excellent support | Good support |
| Community | Large, very active | Growing, active |
| RBAC | Built-in | Via Kubernetes RBAC |
| Notifications | Built-in | Via controllers |
| Use Case | Large teams, complex deployments | Smaller teams, lightweight |
Common Pitfallsโ
1. Manual Changes Outside GitOpsโ
Problem: Team makes manual changes directly on cluster Solution: Enforce GitOps for all changes, educate team
2. Credentials in Gitโ
Problem: Database passwords stored in repository Solution: Use secrets management (Sealed Secrets, Vault)
3. Git Branch Strategy Issuesโ
Problem: Developers push to main without review Solution: Implement branch protection, require PRs
4. Sync Strategy Too Aggressiveโ
Problem: Controller constantly syncs, causes noise Solution: Configure appropriate sync intervals and policies
5. Not Monitoring Sync Statusโ
Problem: Changes don't apply but nobody notices Solution: Monitor ArgoCD/Flux, set up alerts
Key Takeawaysโ
- GitOps makes Git the single source of truth for infrastructure and application configuration
- Pull-based deployment is more secure than push-based (credentials on cluster, not CI/CD)
- Controllers continuously reconcile actual state to desired state in Git
- Self-healing automatically fixes drift without manual intervention
- Git history provides full audit trail of all infrastructure changes
- Code review workflow applies to infrastructure changes as well
- Rollback is as simple as reverting a Git commit with automatic cluster sync
- ArgoCD and Flux are popular GitOps tools with different trade-offs
- Secrets must be handled separately, not stored directly in Git