ArgoCD & GitOps
Git becomes the single source of truth for everything in your cluster.
What you'll learn in this chapter
- What GitOps is and why it is a better model than manual kubectl deployments
- How ArgoCD works: watching Git, detecting drift, and synchronizing the cluster
- How to install ArgoCD and access its UI
- How to write an ArgoCD Application manifest
- The complete GitOps workflow from code change to production deploy
- Kustomize and Helm β two approaches to managing Kubernetes configuration
- The ArgoCD CLI for daily operations
- How to structure a GitOps repository for multiple environments
The Magic Mirror Analogy
Imagine you have a magic mirror in your house. The mirror is connected to a special notebook. Whenever you write something in that notebook β "three blue chairs in the living room," "a plant by the window," "the bookshelf on the left wall" β the mirror makes it so. You never need to physically move furniture. You just write what you want, and the mirror rearranges the house to match exactly. If a chair falls over by itself, the mirror notices the discrepancy, compares it to the notebook, and rights the chair. If someone rearranges the furniture without updating the notebook, the mirror changes it back. The notebook is always the truth. The house always matches the notebook.
That is GitOps. Your Git repository is the notebook. Your Kubernetes cluster is the house. ArgoCD is the magic mirror β it continuously watches the repository and ensures the cluster matches what is written in Git, always.
In GitOps, Git is the single source of truth. If you want to change anything about the cluster β update an image, change a replica count, add an environment variable β you change it in Git. ArgoCD notices and makes the cluster match. If anyone makes a change directly in the cluster (bypassing Git), ArgoCD notices the drift and can automatically revert it.
The Problem GitOps Solves
Before GitOps, deploying to Kubernetes looked like this. A developer (or an automated
pipeline) would run kubectl apply -f deployment.yaml from their laptop or
a CI server. The deployment would happen. The state of the cluster would change.
But that change lived only in the cluster β not necessarily in the Git repository.
Over time, the cluster would accumulate changes made by different people at different
times. Nobody could be completely sure what was actually deployed. "What's running in
production?" became a question that required checking the cluster directly, not the
code repository.
Rollbacks were frightening. "Which YAML file represents what was deployed before the last change?" If nobody tracked that carefully, you might not know. And if a cluster failed completely and had to be rebuilt from scratch, you might not have a reliable source of truth to recreate it from.
GitOps eliminates all of this. Every deployment is a Git commit. Every change to infrastructure is reviewed, approved, and tracked. The entire history of your cluster configuration is the history of your Git repository. Rollback is just reverting a commit. Disaster recovery is just pointing ArgoCD at the repository and letting it sync.
GitOps is an operational model where the desired state of infrastructure and applications is stored in Git as the single source of truth. Automated agents (like ArgoCD) continuously reconcile the actual state of the system with the desired state in Git. All changes flow through Git β enabling version history, code review, rollback, and audit trails for every infrastructure change.
ArgoCD Architecture
ArgoCD itself runs as a set of Pods inside your Kubernetes cluster β it is a Kubernetes application that manages other Kubernetes applications. This is elegant: ArgoCD can manage itself using GitOps principles.
ArgoCD's components:
- argocd-server β the API server and Web UI. You interact with it via the browser or the argocd CLI.
- argocd-repo-server β clones Git repositories and generates Kubernetes manifests (running Helm or Kustomize if needed).
- argocd-application-controller β the reconciliation loop. It continuously compares the live cluster state with the desired state in Git and reports drift.
- argocd-dex-server β handles SSO authentication (GitHub, GitLab, LDAP).
ARGOCD GITOPS FLOW
ββββββββββββββββββββ ββββββββββββββββββββββββ
β Git Repository β β Container Registry β
β (source of truth)β β (image storage) β
β β β β
β k8s/production/ ββββββββββββ myapp:sha-a3f9bc2 β
β deployment.yamlβ polls ββ β
β service.yaml β βββββββββββββββββββββββββ
ββββββββββββββββββββ β
β β
β detects change β
βΌ β
ββββββββββββββββββββββββββββ β
β ArgoCD β β
β ββββββββββββββββββββββ β β
β β app-controller β β β
β β (reconcile loop) β β β
β ββββββββββββββββββββββ β β
βββββββββββββ¬ββββββββββββββββ β
β kubectl apply β
βΌ β
ββββββββββββββββββββββββββββ β
β Kubernetes Cluster ββββββ
β (actual state matches β
β desired state in Git) β
ββββββββββββββββββββββββββββ
Installing ArgoCD
ArgoCD installs into your cluster with a single manifest file. Once running, you access the UI via port-forwarding for local clusters, or via an Ingress for production.
# Create a dedicated namespace for ArgoCD
kubectl create namespace argocd
# Install ArgoCD β this applies all the CRDs, Deployments, Services, RBAC rules
kubectl apply -n argocd -f \
https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# Wait for all ArgoCD Pods to be ready
kubectl wait --for=condition=available deployment argocd-server \
-n argocd --timeout=120s
# Port-forward the ArgoCD web UI to your local machine
# Access it at: https://localhost:8080
kubectl port-forward svc/argocd-server -n argocd 8080:443
# Retrieve the initial admin password (auto-generated at install)
kubectl get secret argocd-initial-admin-secret -n argocd \
-o jsonpath="{.data.password}" | base64 -d && echo
# Log in with username "admin" and the password above
# Then change the password in the UI settings
Once you open the UI at https://localhost:8080, you will see ArgoCD's
dashboard. It shows all your Applications, their sync status (Synced or OutOfSync),
and their health status (Healthy, Degraded, Missing). The UI gives you a visual diff
of what is in Git versus what is in the cluster β a tool that pays for itself the
first time you use it to understand a production discrepancy.
Creating an ArgoCD Application
An ArgoCD Application is a Kubernetes custom resource that tells ArgoCD: "watch this Git repository path and keep this namespace synchronized to match it." You can create Applications through the UI, the CLI, or β following GitOps principles β as YAML manifests stored in Git.
# apps/my-app.yaml β stored in Git, applied to the argocd namespace
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
project: default
# WHERE to look in Git
source:
repoURL: https://github.com/myorg/my-app-config.git
targetRevision: main
path: k8s/production # only watch this subdirectory
# WHERE to deploy in Kubernetes
destination:
server: https://kubernetes.default.svc # the current cluster
namespace: production
# HOW to sync
syncPolicy:
automated:
prune: true # delete resources removed from Git
selfHeal: true # revert manual changes made directly in cluster
syncOptions:
- CreateNamespace=true # create the namespace if it doesn't exist
- ServerSideApply=true # use server-side apply for better conflict handling
The two most important settings in syncPolicy are prune and
selfHeal. With prune: true, if you delete a manifest from
Git, ArgoCD deletes the corresponding resource from the cluster. Without it, deleted
manifests leave orphaned resources behind. With selfHeal: true, if
someone runs kubectl edit deployment my-app and changes something directly
in the cluster, ArgoCD detects the drift and reverts the cluster to match Git. This
is the enforcement mechanism that makes Git truly the source of truth.
The Complete GitOps Workflow
GitOps splits your version control into two repositories. The first is your application repository β it contains your source code. The second is your configuration repository β it contains Kubernetes manifests (or Helm/Kustomize configs) that describe how to deploy the application. This separation is important: you do not want every code change to trigger a cluster sync, and you want the deployment config to be auditable separately from the application code.
THE COMPLETE GITOPS PIPELINE
Developer pushes code to app repo
β
βΌ
ββββββββββββββββββββββ
β GitHub Actions β
β CI Pipeline β
β ββββββββββββββββ β
β β 1. Run tests β β
β β 2. Build img β β
β β 3. Push img ββββββββββββββββΊ Container Registry
β β :sha-abc β β myapp:sha-abc123
β ββββββββββββββββ β
β 4. Update configββββββββββββββΊ Config Repository
β repo with new β k8s/production/
β image tag β deployment.yaml
ββββββββββββββββββββββ (image: myapp:sha-abc123)
β
β ArgoCD detects change
βΌ
ββββββββββββββββββββββ
β ArgoCD β
β syncs cluster to β
β match new config β
ββββββββββββββββββββββ
β
βΌ
Kubernetes cluster updated
(new image deployed, zero
kubectl commands run by humans)
Step 4 deserves special attention. After CI builds and pushes the new image, it updates
the image tag in the configuration repository. How? Typically using a tool like Kustomize's
image transformer or a simple sed command followed by a git commit and push.
Your CI pipeline needs write access to the config repository for this step.
# In your GitHub Actions deploy step, after pushing the image:
# Update the image tag in the config repo
- name: Update image tag in config repo
env:
CONFIG_REPO_TOKEN: ${{ secrets.CONFIG_REPO_TOKEN }}
run: |
git clone https://x-access-token:$CONFIG_REPO_TOKEN@github.com/myorg/my-app-config.git
cd my-app-config
# Update the image tag to the current commit SHA
sed -i "s|image: myapp:.*|image: myapp:${{ github.sha }}|g" \
k8s/production/deployment.yaml
git config user.email "ci@myorg.com"
git config user.name "CI Bot"
git add .
git commit -m "deploy: update my-app to ${{ github.sha }}"
git push
Kustomize β Overlays for Multiple Environments
Kustomize is a configuration management tool built into kubectl. It uses an overlay system: you define a base configuration once, then create environment-specific overlays that patch or extend the base. Staging uses the same Deployment definition as production, but with two replicas instead of five and a different domain name.
A typical Kustomize directory structure:
k8s/
βββ base/
β βββ deployment.yaml β shared base definition
β βββ service.yaml
β βββ kustomization.yaml β lists the base resources
β
βββ overlays/
βββ staging/
β βββ kustomization.yaml β references base + staging patches
β βββ patch-replicas.yaml β replicas: 1
β
βββ production/
βββ kustomization.yaml β references base + prod patches
βββ patch-replicas.yaml β replicas: 5
# k8s/base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
---
# k8s/overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
patches:
- path: patch-replicas.yaml
images:
- name: myapp
newTag: sha-abc123 # override image tag for production
---
# k8s/overlays/production/patch-replicas.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 5 # overrides the base definition's replica count
In your ArgoCD Application, set path: k8s/overlays/production and
ArgoCD will render the Kustomize overlay and apply the merged result to the cluster.
For staging, point a different ArgoCD Application at k8s/overlays/staging.
One base, maintained once, deployed everywhere consistently.
Helm β The Kubernetes Package Manager
Helm is the package manager for Kubernetes, similar to how npm is the package manager
for Node.js. A Helm chart is a package of Kubernetes templates plus a
values.yaml file that provides default configuration. To deploy a chart,
you provide a values file and Helm renders the templates with your values and applies
the result.
Helm is particularly useful for deploying third-party software. Instead of writing
Kubernetes manifests for PostgreSQL yourself, you run helm install postgres
bitnami/postgresql --values my-values.yaml and get a production-ready PostgreSQL
cluster in minutes.
# Install a Helm chart from a repository
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
# Deploy PostgreSQL with custom values
helm install postgres bitnami/postgresql \
--namespace production \
--values postgres-values.yaml
# Upgrade to a new chart version or new values
helm upgrade postgres bitnami/postgresql \
--namespace production \
--values postgres-values.yaml
# List installed releases
helm list -n production
# Roll back to a previous release version
helm rollback postgres 1 -n production
ArgoCD supports Helm natively. In your ArgoCD Application, specify the chart repository, chart name, and your values file path. ArgoCD renders the Helm chart and treats the output as the desired state in Git β giving you Helm's convenience with GitOps's auditability.
ArgoCD CLI β Day-to-Day Operations
The ArgoCD CLI lets you interact with your applications from the terminal β useful in scripts, during incidents, and for developers who prefer the command line to the UI.
# Install the argocd CLI (macOS)
brew install argocd
# Log in to your ArgoCD instance
argocd login localhost:8080 \
--username admin \
--password your-password \
--insecure # skip TLS verification for local testing
# List all applications
argocd app list
# Get detailed status of an application
argocd app get my-app
# Manually trigger a sync (re-apply what's in Git)
argocd app sync my-app
# Sync and wait until it's healthy
argocd app sync my-app --wait
# Roll back to a previous version (by history ID)
argocd app rollback my-app 1
# Show sync history
argocd app history my-app
# Diff what would change in the next sync
argocd app diff my-app
# Pause automated sync temporarily (useful during incidents)
argocd app pause my-app
# Resume automated sync
argocd app resume my-app
App of Apps β Managing Multiple Applications
When you have many services, you do not want to manually apply each ArgoCD Application YAML. The "App of Apps" pattern solves this. You create a single root ArgoCD Application that points to a directory of other ArgoCD Application manifests. When ArgoCD syncs the root, it discovers and creates all child applications automatically.
App of Apps Pattern:
Git: apps/
βββ root-app.yaml β the "App of Apps" β points to apps/ directory
βββ api-service.yaml β ArgoCD Application for the API
βββ frontend.yaml β ArgoCD Application for the frontend
βββ worker.yaml β ArgoCD Application for the background worker
βββ monitoring.yaml β ArgoCD Application for Prometheus/Grafana
ArgoCD syncs root-app
β creates api-service application
β creates frontend application
β creates worker application
β creates monitoring application
Each child app then manages its own Kubernetes resources.
This pattern scales to hundreds of services. It is how platform engineering teams manage large organizations β the entire platform's desired state is expressed as a tree of Git files. Onboarding a new service means adding one YAML file to the apps directory. Decommissioning it means deleting that file.
Keep your application source code repository separate from your Kubernetes configuration repository. The config repo contains only YAML β no application code, no Dockerfiles. This separation has important benefits: the config repo can have stricter access controls, changes to it are always deployment-related (easy to audit), and the CI pipeline that builds images does not need access to production infrastructure credentials.
ArgoCD Image Updater
A companion tool, ArgoCD Image Updater, watches your container registry for new image tags and automatically updates the config repo with the new tag. This closes the loop completely: push code, CI builds image, Image Updater updates config, ArgoCD deploys β all without a human touching a single command.
You configure it with an annotation on your ArgoCD Application:
metadata:
annotations:
# Watch for new tags matching this pattern
argocd-image-updater.argoproj.io/image-list: myapp=ghcr.io/myorg/myapp
argocd-image-updater.argoproj.io/myapp.update-strategy: newest-build
# Write the new tag back to Git
argocd-image-updater.argoproj.io/write-back-method: git
Weaveworks (the company that coined the term "GitOps" in 2017) reports that teams using GitOps see 60-90% reduction in Mean Time to Recovery (MTTR) during incidents β because rolling back is simply reverting a Git commit, not figuring out which kubectl commands to run. The audit trail in Git also means security teams can see exactly who deployed what and when, turning compliance from a burden into a natural byproduct of the deployment process.
Chapter Summary
- GitOps makes Git the single source of truth for cluster configuration. All changes flow through version control, giving you audit trails, rollback by commit revert, and disaster recovery from the repository.
- ArgoCD runs inside Kubernetes and continuously watches Git repositories. When it detects a difference between Git and the cluster, it can auto-sync to reconcile the difference.
- ArgoCD Applications are Kubernetes custom resources that link a Git path to a cluster namespace. Set
prune: trueandselfHeal: truefor full GitOps enforcement. - The GitOps workflow: developer pushes code β CI builds and pushes image β CI updates image tag in config repo β ArgoCD detects change β ArgoCD deploys. No human runs kubectl in production.
- Kustomize uses an overlay system (base + environment patches) to manage config for multiple environments from a single base definition. Helm uses templates and values files for packaging and deploying Kubernetes applications.
- The App of Apps pattern lets ArgoCD manage dozens of services by treating a directory of ArgoCD Application manifests as a deployable unit itself.