DevOps & Containers
GitOps Explained: Deploying From Git With Argo CD and Flux
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيGitOps is a way of running infrastructure and applications where a Git repository describes exactly what should be running, and an automated agent continuously makes reality match it. Instead of a CI job SSH-ing into servers or running kubectl apply with admin credentials, you merge a pull request and the cluster updates itself. Rollback becomes git revert, and the history of every change lives in your commit log. This guide explains the principles, compares the main tools, and walks through a minimal setup.
The four principles
The OpenGitOps project, under the CNCF, summarizes GitOps in four principles:
- Declarative. The desired state of the system is expressed declaratively, for example as Kubernetes manifests, Helm values or Kustomize overlays.
- Versioned and immutable. Desired state is stored in a way that keeps full history, which in practice means Git.
- Pulled automatically. Software agents pull the desired state from the source instead of having it pushed to them.
- Continuously reconciled. Agents keep observing the actual state and correct any difference from the desired state.
The fourth principle is what separates GitOps from "we keep our YAML in Git". A reconciliation loop also fixes manual changes, which makes GitOps a strong defense against configuration drift.
Push versus pull deployment
| Push (classic CI/CD) | Pull (GitOps) | |
|---|---|---|
| Who applies changes | CI pipeline | Agent running inside the cluster |
| Credentials | CI holds cluster admin credentials | Cluster only needs read access to Git |
| Drift handling | Only fixed on next deploy | Detected and corrected continuously |
| Audit trail | Spread across CI logs | Git history and pull requests |
| Rollback | Re-run an old pipeline | git revert |
Pull-based delivery improves security because no external system needs write access to production. The cluster reaches out to Git; Git never reaches into the cluster.
Repository layout
A common pattern separates application code from deployment configuration:
app-repo/ # source code, Dockerfile, CI builds and pushes images
config-repo/
apps/
web/
base/ # deployment, service, ingress
overlays/
staging/ # kustomization.yaml with staging image tag
production/ # kustomization.yaml with production image tagCI builds an image, pushes it to a registry, and then opens a pull request in the config repository that updates the image tag. Merging that pull request is the deployment.
Argo CD versus Flux
Both are mature CNCF graduated projects.
- Argo CD provides a web UI that visualizes applications, sync status and resource health. It suits teams that want visibility and manual sync controls.
- Flux is a set of composable controllers driven entirely by Kubernetes resources and the CLI. It suits teams that prefer everything as code, including Flux itself.
Either works well. Choose based on how your team prefers to operate.
A minimal Argo CD setup on k3s
A single VPS running k3s is enough to learn GitOps. If you need a cluster first, see what Kubernetes is and how to run k3s on a VPS.
1. Install Argo CD
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yamlGet the initial admin password and access the UI through a port-forward rather than exposing it publicly:
kubectl -n argocd get secret argocd-initial-admin-secret \
-o jsonpath="{.data.password}" | base64 -d; echo
kubectl -n argocd port-forward svc/argocd-server 8080:443Then open https://localhost:8080 through an SSH tunnel and change the password.
2. Define an Application
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: web-production
namespace: argocd
spec:
project: default
source:
repoURL: https://git.example.com/team/config-repo.git
targetRevision: main
path: apps/web/overlays/production
destination:
server: https://kubernetes.default.svc
namespace: web
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=trueprune removes resources you delete from Git, and selfHeal reverts manual changes made in the cluster. Apply it with kubectl apply -f app.yaml, and Argo CD starts reconciling.
3. Deploy by commit
Change the image tag in overlays/production/kustomization.yaml, open a pull request, review, merge. Within the polling interval (or immediately with a Git webhook) the new version rolls out.
Handling secrets
Never commit plaintext secrets to Git. Common options:
- Sealed Secrets: encrypt with a public key; only the controller in the cluster can decrypt.
- SOPS: encrypt values in YAML files with age or PGP keys; Flux supports it natively.
- External Secrets Operator: keep secrets in a dedicated secret manager and sync references from Git.
Practical tips
- Protect the main branch of the config repository and require reviews; merging is deploying.
- Keep environments in separate folders or branches so a staging change cannot reach production by accident.
- Start with manual sync for production until the team trusts the process, then enable automation.
- Monitor the GitOps agent itself and alert when an application is out of sync or degraded.
A VPS with a few vCPUs and enough RAM for k3s plus Argo CD is a comfortable lab for practicing everything above.
FAQ
Does GitOps only work with Kubernetes?
It is most common there because Kubernetes is declarative and has a reconciliation model built in. The principles can apply elsewhere, for example with tools that reconcile Docker Compose stacks or Terraform from Git, but tooling is less standardized.
How do I roll back with GitOps?
Revert the commit that introduced the change and merge it. The agent sees the previous desired state and applies it. Avoid rolling back directly in the cluster, because self-healing will undo your manual change.
Do I still need CI if I use GitOps?
Yes. CI still builds, tests and scans your code and produces images. GitOps replaces only the deployment step, so CI updates the config repository and the agent handles the rollout.