GitOps with ArgoCD: From Commit to Automated Deploy in Kubernetes

How to implement GitOps in your Kubernetes cluster using ArgoCD. From installation to a full pipeline with auto-sync and custom health checks.

What is GitOps and why does it matter?

GitOps is an operational paradigm where Git is the single source of truth for both your infrastructure and applications. Instead of applying changes manually with kubectl apply, every change goes through a pull request.

The advantages are clear:

  • Full audit trail: every change is recorded in Git history
  • Instant rollbacks: git revert and you’re done
  • Consistency: cluster state always reflects what is declared in Git
  • Collaboration: the same development workflows apply to infrastructure

Installing ArgoCD

# Create namespace
kubectl create namespace argocd

# Install ArgoCD
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

# Retrieve initial admin password
kubectl -n argocd get secret argocd-initial-admin-secret \
  -o jsonpath="{.data.password}" | base64 -d

# Port-forward to access the UI
kubectl port-forward svc/argocd-server -n argocd 8080:443

GitOps Repository Structure

A clean repository structure is essential:

gitops-repo/
├── apps/
│   ├── production/
│   │   ├── api-gateway.yaml
│   │   ├── payment-service.yaml
│   │   └── notification-service.yaml
│   └── staging/
│       ├── api-gateway.yaml
│       └── payment-service.yaml
├── base/
│   ├── api-gateway/
│   │   ├── deployment.yaml
│   │   ├── service.yaml
│   │   ├── hpa.yaml
│   │   └── kustomization.yaml
│   └── payment-service/
│       ├── deployment.yaml
│       ├── service.yaml
│       └── kustomization.yaml
└── overlays/
    ├── production/
    │   └── kustomization.yaml
    └── staging/
        └── kustomization.yaml

Creating an ArgoCD Application

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: api-gateway
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/your-org/gitops-repo.git
    targetRevision: main
    path: overlays/production
  destination:
    server: https://kubernetes.default.svc
    namespace: production
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
    retry:
      limit: 5
      backoff:
        duration: 5s
        factor: 2
        maxDuration: 3m

Custom Health Checks

ArgoCD includes built-in health checks for native Kubernetes resources, and you can also define your own for Custom Resources:

-- custom-health-check.lua
hs = {}
if obj.status ~= nil then
  if obj.status.health ~= nil then
    hs.status = obj.status.health.status
    if obj.status.health.message ~= nil then
      hs.message = obj.status.health.message
    end
    return hs
  end
end

hs.status = "Progressing"
hs.message = "Waiting for health status"
return hs

Conclusion

GitOps with ArgoCD turns your Kubernetes cluster into a declarative, auditable, and reproducible system. Once you get used to this workflow, going back to manual deployments feels like returning to the stone age.


Already using GitOps in your organization? I’d love to hear about your experience on X.