Overview and What You Will Learn
GitOps is a deployment model in which the desired state of a system is stored in Git and an agent inside the cluster continuously pulls that state and reconciles the cluster to match it. ArgoCD is the most widely used agent for Kubernetes. In this lab, you will install ArgoCD, deploy a sample app with automated sync and self-healing, build a Kustomize-based config repository, and use the app-of-apps pattern so that a single Git commit adds a whole service to the platform.
- Stating the four GitOps principles and how push and pull deployment differ
- Naming ArgoCD's components and what each does
- Installing ArgoCD and logging in with the CLI
- Writing an
Applicationwithautomated,pruneandselfHeal - Structuring a config repository with Kustomize bases and overlays
- Using app-of-apps, and pausing automation safely during an incident
Why This Matters in Production
In push-based deployment, a CI job runs kubectl apply against production, so CI needs network access and credentials for every cluster, and the only record of what is running is whatever the last pipeline applied. A platform team at a company like Zerodha prefers pull-based GitOps for two practical reasons. Every change to production is a Git commit with an author and a pull request review, and if someone runs a manual kubectl edit in a panic, ArgoCD reverts it to what Git says.
Core Principles
GitOps rests on four principles, and ArgoCD implements each of them.
- Declarative: the whole system is described as data, such as Kubernetes YAML, Helm values or Kustomize overlays, not as a sequence of commands.
- Versioned and immutable: the desired state lives in Git, which gives history, review and
git revert. - Pulled automatically: software inside the cluster pulls the desired state, so no external system needs cluster credentials.
- Continuously reconciled: the agent keeps comparing live state to desired state and corrects drift, not only at deploy time.
The two deployment models compare like this:
| Aspect | Push, CI applies changes | Pull, ArgoCD reconciles |
|---|---|---|
| Cluster credentials | Held by the CI system | Stay inside the cluster |
| Drift from manual edits | Persists until the next deploy | Detected and corrected |
| Record of what runs | The last pipeline run | The Git history |
The reconciliation loop runs continuously:
merged] --> D[Repo server
detects and renders] D --> F{Controller
diff live vs Git} F -->|Different| S[Sync
apply changes] F -->|Same| H[Healthy
nothing to do] S --> H classDef git fill:#F97316,color:#FFFFFF classDef argo fill:#326CE5,color:#FFFFFF classDef ok fill:#10B981,color:#FFFFFF class G git class D,F,S argo class H ok
ArgoCD runs as a set of components in the argocd namespace:
controller] GIT[(Git config repo)] --> RS[Repo server
renders manifests] RS --> AC AC --> K[(Cluster API)] R[(Redis
cache)] -.-> AC classDef user fill:#D1FAE5,color:#1E293B classDef comp fill:#326CE5,color:#FFFFFF classDef store fill:#475569,color:#FFFFFF class U user class API,RS,AC comp class GIT,K,R store
- API server: the gRPC and REST entry point used by the UI, the CLI and webhooks.
- Repo server: clones the Git repository and renders the final manifests by running Helm or Kustomize.
- Application controller: the reconciliation engine that compares live state with the rendered desired state and syncs it.
- Redis: caches rendered manifests and cluster state to keep reconciliation fast.
By default ArgoCD polls Git about every three minutes, and a webhook from your Git host makes changes appear within seconds.
The central object is the Application custom resource. prune: true deletes cluster resources that were removed from Git, and selfHeal: true reverts manual changes made directly in the cluster. The finalizer makes deleting the Application also delete the resources it created:
apiVersion: argoproj.io/v1alpha1kind: Applicationmetadata: name: order-gateway namespace: argocd finalizers: - resources-finalizer.argocd.argoproj.iospec: project: default source: repoURL: https://github.com/example-org/platform-config.git targetRevision: main path: services/order-gateway/overlays/production destination: server: https://kubernetes.default.svc namespace: order-gateway-prod syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=trueIn the app-of-apps pattern, a root Application points at a directory that contains child Application manifests. Adding a file to that directory adds a service, and removing the file removes it.
path: apps] --> A1[order-gateway
Application] R --> A2[notification-svc
Application] A1 --> W1[(Deployment,
Service)] A2 --> W2[(Deployment,
Service)] classDef root fill:#8B5CF6,color:#FFFFFF classDef app fill:#326CE5,color:#FFFFFF classDef res fill:#D1FAE5,color:#1E293B class R root class A1,A2 app class W1,W2 res
Keep application source code and deployment manifests in separate repositories. CI builds and pushes an image from the app repository, then opens a pull request against the config repository that bumps the image tag. ArgoCD only ever watches the config repository.
Detailed Step-by-Step Practical Lab
This lab needs a Kubernetes cluster you can use for testing, kubectl, the argocd CLI, and a Git repository you can push to, such as an empty repository on GitHub.
Step 1 - Install ArgoCD
Server-side apply avoids an annotation size error that client-side apply can hit with ArgoCD's larger CRDs. For production, replace stable with an exact release tag:
kubectl create namespace argocd kubectl apply -n argocd --server-side \ -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml kubectl -n argocd wait --for=condition=available deployment --all --timeout=300skubectl -n argocd get podsStep 2 - Log In with the CLI
Forward the API server port, read the generated admin password, and log in:
kubectl -n argocd port-forward svc/argocd-server 8080:443 > /dev/null 2>&1 & ARGOCD_PASSWORD=$(kubectl -n argocd get secret argocd-initial-admin-secret \ -o jsonpath="{.data.password}" | base64 -d) argocd login localhost:8080 --username admin --password "$ARGOCD_PASSWORD" --insecureSecurityThe initial admin password is for first login only. Change it, then delete the
argocd-initial-admin-secret, and set up SSO and RBAC before sharing the instance with a team.
Step 3 - Deploy a Sample App with Automated Sync
Use ArgoCD's public example repository to see sync and self-heal work before you build your own repository:
cat << 'EOF' | kubectl apply -f -apiVersion: argoproj.io/v1alpha1kind: Applicationmetadata: name: guestbook namespace: argocd finalizers: - resources-finalizer.argocd.argoproj.iospec: project: default source: repoURL: https://github.com/argoproj/argocd-example-apps.git targetRevision: HEAD path: guestbook destination: server: https://kubernetes.default.svc namespace: guestbook syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=trueEOF argocd app wait guestbook --health --timeout 180argocd app get guestbookThe app should show Synced and Healthy.
Step 4 - Test Self-Healing
Change the cluster by hand, then watch ArgoCD put it back. The replica count in Git is the source of truth:
kubectl -n guestbook scale deployment guestbook-ui --replicas=5sleep 30kubectl -n guestbook get deployment guestbook-uiThe replica count returns to the value Git declares. Manual drift does not persist.
RememberWith
selfHealon, an emergencykubectl scaleduring an incident is reverted within seconds. Know this before your first incident, and use the pause procedure in Step 8.
Remove the sample before building your own repository:
kubectl -n argocd delete application guestbookStep 5 - Build the Config Repository
Create the layout for two services with a Kustomize base and a production overlay each. The bootstrap directory holds the root Application, which you apply once by hand, and the apps directory holds the child Applications that ArgoCD manages. Set REPO_URL to your own repository first:
export REPO_URL=https://github.com/YOUR_ORG/platform-config.git mkdir platform-config && cd platform-configmkdir -p bootstrap apps make_service() { local svc="$1" replicas="$2" mkdir -p "services/$svc/base" "services/$svc/overlays/production" cat > "services/$svc/base/deployment.yaml" << EOFapiVersion: apps/v1kind: Deploymentmetadata: name: $svcspec: replicas: 1 selector: matchLabels: app: $svc template: metadata: labels: app: $svc spec: containers: - name: $svc image: nginx:1.27-alpine ports: - containerPort: 80EOF cat > "services/$svc/base/service.yaml" << EOFapiVersion: v1kind: Servicemetadata: name: $svcspec: selector: app: $svc ports: - port: 80 targetPort: 80EOF cat > "services/$svc/base/kustomization.yaml" << EOFapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources: - deployment.yaml - service.yamlEOF cat > "services/$svc/overlays/production/kustomization.yaml" << EOFapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationnamespace: $svc-prodresources: - ../../basereplicas: - name: $svc count: $replicasEOF} make_service order-gateway 3make_service notification-svc 2 kubectl kustomize services/order-gateway/overlays/productionThe last command renders the production overlay locally, which is the same thing ArgoCD's repo server does. Check that it shows three replicas in the order-gateway-prod namespace.
Step 6 - Add the Root and First Child Application
Write the child Application for order-gateway and the root Application that watches the apps directory:
make_app() { local svc="$1" cat > "apps/$svc.yaml" << EOFapiVersion: argoproj.io/v1alpha1kind: Applicationmetadata: name: $svc namespace: argocd finalizers: - resources-finalizer.argocd.argoproj.iospec: project: default source: repoURL: $REPO_URL targetRevision: main path: services/$svc/overlays/production destination: server: https://kubernetes.default.svc namespace: $svc-prod syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=trueEOF} make_app order-gateway cat > bootstrap/root-app.yaml << EOFapiVersion: argoproj.io/v1alpha1kind: Applicationmetadata: name: root namespace: argocd finalizers: - resources-finalizer.argocd.argoproj.iospec: project: default source: repoURL: $REPO_URL targetRevision: main path: apps destination: server: https://kubernetes.default.svc namespace: argocd syncPolicy: automated: prune: true selfHeal: trueEOF git init -b maingit add .git commit -m "Add platform config with order-gateway"git remote add origin "$REPO_URL"git push -u origin mainThe root Application lives in bootstrap, outside the directory it watches. If it sat inside apps, it would manage itself. For a private repository, register read-only credentials first with argocd repo add "$REPO_URL" --username git --password YOUR_TOKEN.
Step 7 - Bootstrap and Watch the Platform Appear
Apply the root Application once. Everything after this comes from Git:
kubectl apply -f bootstrap/root-app.yaml argocd app listkubectl -n order-gateway-prod get deployment order-gatewayArgoCD creates the order-gateway Application from the apps directory, which then deploys three replicas to the order-gateway-prod namespace. Now add a second service with a single commit and no kubectl apply:
make_app notification-svcgit add apps/notification-svc.yamlgit commit -m "Add notification-svc to the platform"git push argocd app get root --refreshargocd app listWithin a few minutes, or at once with a webhook, notification-svc appears and deploys.
Step 8 - Pause Automation During an Incident
Because the root Application self-heals, it would revert a change you make to a child Application. Pause both levels before making a manual emergency change, then restore the state in Git afterwards:
argocd app set root --sync-policy noneargocd app set order-gateway --sync-policy none kubectl -n order-gateway-prod scale deployment order-gateway --replicas=6When the incident is over, commit the change you want to Git, then turn automation back on:
argocd app set order-gateway --sync-policy automated --self-heal --auto-pruneargocd app set root --sync-policy automated --self-heal --auto-pruneStep 9 - Clean Up
kubectl -n argocd delete application rootkubectl delete namespace argocdDeleting the root Application removes the child Applications and, through their finalizers, the workloads they created.
Production Best Practices & Common Pitfalls
Common MistakeTreating ArgoCD as Jenkins for Kubernetes, and still letting CI push to the cluster while ArgoCD merely watches. GitOps only works when Git is the single path to changing cluster state. A side door means you have a dashboard, not GitOps.
TipHave CI open the image-tag pull request against the config repository automatically, but require a human review before merging production overlays. You keep a record of who approved each deploy, and nobody has to run
kubectl apply.
SecurityNever put plaintext secrets in the config repository. Use Sealed Secrets, External Secrets or SOPS so that Git holds only encrypted values, and scope each Application with an ArgoCD Project that limits the repositories, namespaces and cluster resources it may touch.
- Treat
prunewith care. Withprune: true, deleting a file from Git deletes the live resource, including namespaces and persistent volume claims. Protect critical resources with theargocd.argoproj.io/sync-options: Prune=falseannotation. - Pin production to a tag or commit.
targetRevisioncan be a branch, tag or SHA, and pinning stops a force-push tomainfrom silently changing what runs. - Ignore fields other controllers own. An HPA changes
replicasand a webhook can inject fields, so useignoreDifferencesto stop an Application from stayingOutOfSync. - Use overlays, not copies. One base with small per-environment overlays is easier to review than duplicated manifests.
- Order dependent resources with sync waves. Annotations and hooks let CRDs, namespaces and databases apply before the workloads that need them.
- Scope permissions narrowly. Use ArgoCD Projects and RBAC so a team can only deploy to its own namespaces.
- Add a webhook. Without one, changes can take about three minutes to appear because ArgoCD polls Git.
Quick Reference & Troubleshooting Commands
| Symptom | Command | What to look for |
|---|---|---|
| Application stuck OutOfSync | argocd app diff APP |
A field changed by another controller such as an HPA |
| Manual fix keeps reverting | kubectl -n argocd get application APP -o jsonpath='{.spec.syncPolicy}' |
selfHeal: true working as designed |
| New child app not appearing | argocd app get root --refresh |
Root has not synced since the push |
| Sync fails with a render error | argocd app manifests APP |
A bad overlay or missing file in the path |
| Repo not accessible | argocd repo list |
A connection status other than Successful |
| Resources not deleted on removal | kubectl -n argocd get application APP -o yaml |
Missing finalizer or prune disabled |
| Command | Description |
|---|---|
argocd app list |
List Applications and their sync and health status |
argocd app sync APP |
Trigger a sync manually |
argocd app history APP |
Show deployed revisions |
argocd app rollback APP ID |
Roll back, which needs automated sync turned off |
argocd app set APP --sync-policy none |
Pause automated sync |