Skip to main content

Implementing GitOps with ArgoCD on Kubernetes

Implement GitOps with ArgoCD: Git as the source of truth, automated sync with self-heal and prune, a Kustomize layout, and the app-of-apps pattern.

22 Terms

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 Application with automated, prune and selfHeal
  • 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:

flowchart LR G[Git commit
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:

flowchart LR U([UI and CLI]) --> API[API server] API --> AC[Application
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:

YAML
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: order-gateway
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
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=true

In 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.

flowchart TD R[root Application
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:

Bash
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=300s
kubectl -n argocd get pods

Step 2 - Log In with the CLI

Forward the API server port, read the generated admin password, and log in:

Bash
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" --insecure
Security

The 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:

Bash
cat << 'EOF' | kubectl apply -f -
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: guestbook
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
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=true
EOF
argocd app wait guestbook --health --timeout 180
argocd app get guestbook

The 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:

Bash
kubectl -n guestbook scale deployment guestbook-ui --replicas=5
sleep 30
kubectl -n guestbook get deployment guestbook-ui

The replica count returns to the value Git declares. Manual drift does not persist.

Remember

With selfHeal on, an emergency kubectl scale during 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:

Bash
kubectl -n argocd delete application guestbook

Step 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:

Bash
export REPO_URL=https://github.com/YOUR_ORG/platform-config.git
mkdir platform-config && cd platform-config
mkdir -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" << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: $svc
spec:
replicas: 1
selector:
matchLabels:
app: $svc
template:
metadata:
labels:
app: $svc
spec:
containers:
- name: $svc
image: nginx:1.27-alpine
ports:
- containerPort: 80
EOF
cat > "services/$svc/base/service.yaml" << EOF
apiVersion: v1
kind: Service
metadata:
name: $svc
spec:
selector:
app: $svc
ports:
- port: 80
targetPort: 80
EOF
cat > "services/$svc/base/kustomization.yaml" << EOF
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
EOF
cat > "services/$svc/overlays/production/kustomization.yaml" << EOF
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: $svc-prod
resources:
- ../../base
replicas:
- name: $svc
count: $replicas
EOF
}
make_service order-gateway 3
make_service notification-svc 2
kubectl kustomize services/order-gateway/overlays/production

The 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:

Bash
make_app() {
local svc="$1"
cat > "apps/$svc.yaml" << EOF
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: $svc
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
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=true
EOF
}
make_app order-gateway
cat > bootstrap/root-app.yaml << EOF
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: root
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
project: default
source:
repoURL: $REPO_URL
targetRevision: main
path: apps
destination:
server: https://kubernetes.default.svc
namespace: argocd
syncPolicy:
automated:
prune: true
selfHeal: true
EOF
git init -b main
git add .
git commit -m "Add platform config with order-gateway"
git remote add origin "$REPO_URL"
git push -u origin main

The 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:

Bash
kubectl apply -f bootstrap/root-app.yaml
argocd app list
kubectl -n order-gateway-prod get deployment order-gateway

ArgoCD 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:

Bash
make_app notification-svc
git add apps/notification-svc.yaml
git commit -m "Add notification-svc to the platform"
git push
argocd app get root --refresh
argocd app list

Within 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:

Bash
argocd app set root --sync-policy none
argocd app set order-gateway --sync-policy none
kubectl -n order-gateway-prod scale deployment order-gateway --replicas=6

When the incident is over, commit the change you want to Git, then turn automation back on:

Bash
argocd app set order-gateway --sync-policy automated --self-heal --auto-prune
argocd app set root --sync-policy automated --self-heal --auto-prune

Step 9 - Clean Up

Bash
kubectl -n argocd delete application root
kubectl delete namespace argocd

Deleting the root Application removes the child Applications and, through their finalizers, the workloads they created.

Production Best Practices & Common Pitfalls

Common Mistake

Treating 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.

Tip

Have 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.

Security

Never 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 prune with care. With prune: true, deleting a file from Git deletes the live resource, including namespaces and persistent volume claims. Protect critical resources with the argocd.argoproj.io/sync-options: Prune=false annotation.
  • Pin production to a tag or commit. targetRevision can be a branch, tag or SHA, and pinning stops a force-push to main from silently changing what runs.
  • Ignore fields other controllers own. An HPA changes replicas and a webhook can inject fields, so use ignoreDifferences to stop an Application from staying OutOfSync.
  • 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

Resources

Explore More in Deployment Strategies and GitOps

All 6 Topics

Frequently Asked Questions

Is Implementing GitOps with ArgoCD on Kubernetes free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Implementing GitOps with ArgoCD on Kubernetes topic cover?

Implement GitOps with ArgoCD: Git as the source of truth, automated sync with self-heal and prune, a Kustomize layout, and the app-of-apps pattern.

Found a mistake, an outdated step or a broken link?