Implementing GitOps
1. Understanding GitOps Principles
| Principle | Detail |
| Declarative | System described by manifests |
| Versioned | Git as single source of truth |
| Pulled | Agents reconcile cluster to Git |
| Continuously reconciled | Drift 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
| Option | Effect |
| automated | Sync on Git changes |
| prune | Delete removed resources |
| selfHeal | Revert cluster drift |
| CreateNamespace=true | Auto 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
| Symptom | Cause |
| OutOfSync | Resources drifted; check diff |
| ComparisonError | Invalid manifests |
| SyncFailed | RBAC / immutable field change |
| Stuck Progressing | Health check timeout |