Implementing GitOps

1. Understanding GitOps Principles

PrincipleDetail
DeclarativeSystem described by manifests
VersionedGit as single source of truth
PulledAgents reconcile cluster to Git
Continuously reconciledDrift auto-corrected

2. Installing Flux

brew install fluxcd/tap/flux
flux bootstrap github \
  --owner=org --repository=gitops \
  --branch=main --path=clusters/prod --personal

3. Installing ArgoCD

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath='{.data.password}' | base64 -d

4. Creating GitRepository Source

apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata: { name: app, namespace: flux-system }
spec:
  interval: 1m
  url: https://github.com/org/app
  ref: { branch: main }
  secretRef: { name: git-creds }

5. Creating Kustomization

apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata: { name: app, namespace: flux-system }
spec:
  interval: 5m
  path: ./overlays/prod
  prune: true
  sourceRef: { kind: GitRepository, name: app }
  targetNamespace: prod
  wait: true
  timeout: 5m

6. Creating ArgoCD Application

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata: { name: web, namespace: argocd }
spec:
  project: default
  source:
    repoURL: https://github.com/org/app
    path: overlays/prod
    targetRevision: main
  destination: { server: https://kubernetes.default.svc, namespace: prod }
  syncPolicy:
    automated: { prune: true, selfHeal: true }
    syncOptions: [CreateNamespace=true]

7. Configuring Auto-Sync

OptionEffect
automatedSync on Git changes
pruneDelete removed resources
selfHealRevert cluster drift
CreateNamespace=trueAuto create destination ns

8. Managing Helm Releases

apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata: { name: nginx, namespace: web }
spec:
  interval: 10m
  chart:
    spec:
      chart: nginx
      version: "18.x"
      sourceRef: { kind: HelmRepository, name: bitnami, namespace: flux-system }
  values: { service: { type: LoadBalancer } }

9. Monitoring Sync Status

flux get kustomizations -A
argocd app list
argocd app get web

10. Troubleshooting Sync Issues

SymptomCause
OutOfSyncResources drifted; check diff
ComparisonErrorInvalid manifests
SyncFailedRBAC / immutable field change
Stuck ProgressingHealth check timeout