# GitOps

GitOps is a set of practices to manage infrastructure and application configurations using Git, an open-source version control system. It works by using Git as a single source of truth for declarative infrastructure and applications.

## 1. The Core Concept

GitOps takes the principles of DevOps and applies them to infrastructure automation.
*   **Infrastructure as Code (IaC):** Everything (K8s manifests, Terraform files) is stored in Git.
*   **Single Source of Truth:** If it's not in Git, it doesn't exist. If someone manually changes the cluster, the GitOps agent reverts it back to match Git.

## 2. Push vs. Pull Deployments

### Push-Based (Traditional CI/CD)
The CI pipeline (Jenkins, GitHub Actions) builds the image and then runs a command (like `kubectl apply`) to push the deployment to the cluster.
*   **Risk:** The CI server needs admin access (credentials) to the production cluster.

### Pull-Based (GitOps)
An agent running *inside* the cluster (like ArgoCD or Flux) watches the Git repository. When it sees a change (e.g., a new image tag in a YAML file), it pulls the change and applies it to the cluster.
*   **Security:** The cluster doesn't expose credentials outside. It only needs read access to the Git repo.

## 3. Key Principles

1.  **Declarative:** The entire system is described declaratively (YAML).
2.  **Versioned & Immutable:** The desired state is stored in Git. Changes are made via commits/PRs.
3.  **Automated Delivery:** Approved changes are automatically applied to the system.
4.  **Continuous Reconciliation:** Software agents continuously ensure the actual state matches the desired state.

## 4. Benefits

*   **Easy Rollbacks:** To rollback a deployment, you simply `git revert` the commit. The agent sees the old state and applies it.
*   **Drift Detection:** If someone manually edits a Kubernetes resource (Configuration Drift), the GitOps tool detects it and can automatically overwrite the manual change to restore the state from Git.
*   **Audit Trail:** Git history provides a perfect audit log of who changed what and when.

## 5. Common Tools

*   **ArgoCD:** A declarative, GitOps continuous delivery tool for Kubernetes. Provides a nice UI to visualize sync status.
*   **Flux:** A set of continuous and progressive delivery solutions for Kubernetes that are open and extensible.

## 6. Progressive Delivery (Flagger)

Progressive Delivery is an advanced deployment strategy that builds upon GitOps and Continuous Delivery. It allows you to release new features to a small subset of users first, automatically validating them before a full rollout.

*   **Flagger:** A popular Kubernetes operator that automates the promotion of canary deployments. It works with service meshes (Istio, Linkerd) or ingress controllers (NGINX) to shift traffic.
*   **Workflow:**
    1.  GitOps tool (ArgoCD/Flux) syncs the new deployment.
    2.  Flagger detects the change and starts a **Canary Analysis**.
    3.  It routes a small % of traffic to the new version.
    4.  It checks metrics (Prometheus) like error rate and latency.
    5.  If healthy, it increases traffic. If not, it rolls back automatically.

[[programming/kubernetes-basics]]
[[programming/ci-cd-pipelines]]
[[programming/cloud-native-concepts]]
[[programming/devops]]