GitOps in Practice - ArgoCD and Repository Structure
1. Recap: From Manual kubectl apply to an Automated Flow
In the first two parts of this series, every Deployment was applied with kubectl apply -f file.yaml, run by hand from the machine of whoever was operating the cluster. That works for learning and for small clusters, but it exposes real problems in production: there’s no reliable record of who applied what and when, it’s easy for a manually applied manifest to silently drift from what’s versioned in Git, and every person on the team needs direct access (and credentials) to the cluster to deploy. This article introduces GitOps, the practice that solves exactly this, and ArgoCD, one of the most widely used tools to implement it.
2. What GitOps Is
GitOps takes the idea of infrastructure as code to a specific extreme: Git as the single source of truth for the desired state of a Kubernetes cluster, with an agent running inside the cluster itself that continuously watches the repository and applies any change automatically — without anyone having to run kubectl apply by hand.
The typical flow is:
- Someone opens a pull request changing a YAML manifest (for example, changing
replicas: 2toreplicas: 4, or updating an image tag). - The PR goes through review and CI, like any other code change.
- Once merged into the main branch, a GitOps agent running in the cluster detects the difference between what’s in Git and what’s running, and applies the change automatically.
- The cluster converges to the state described in the repository — without anyone having run a manual command against the cluster.
Pull Request → Review/CI → Merge into Git
│
(GitOps agent watches the repo)
│
▼
The Kubernetes cluster converges
automatically to the state
described in Git
3. Why This Makes Sense for Infrastructure as Code
Compared to applying manifests by hand (even from a CI pipeline that runs kubectl apply), GitOps brings three concrete gains:
- Built-in auditing: the Git history — who proposed the change, who reviewed it, when it was merged — is already the audit log of everything that happened in the cluster, with no need for a separate change-log tool.
- Continuous reconciliation: the GitOps agent doesn’t just apply changes, it keeps comparing the cluster’s actual state with Git at regular intervals. If someone changes something by hand in the cluster (an emergency
kubectl edit, for example), the agent detects the divergence and can automatically revert to what’s declared in Git — or at least alert that there’s “drift”. - Less direct access to the cluster: teams no longer need
kubectlcredentials with write permission on the production cluster in order to deploy — the change goes through Git, and only the GitOps agent (with its own separately managed credentials) has direct write access.
This is a natural extension of what the series has been building: declarative Deployments and Services (Article 1), organized with namespaces and external configuration via ConfigMaps/Secrets (Article 2) — GitOps just formalizes that the source of truth for those YAMLs is a Git repository, and that applying them to the cluster is automatic, not manual.
4. Introduction to ArgoCD
ArgoCD is one of the most widely used GitOps controllers for Kubernetes. It runs as an application inside the cluster itself and introduces a new type of resource, the Application, which points to a path in a Git repository and to a destination cluster/namespace.
Basic installation on a cluster (a local one, like the one created with Minikube/Kind in Article 1, or a real cluster):
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
Once installed, an ArgoCD Application connects a repository to a destination in the cluster:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/my-org/my-app-manifests.git
targetRevision: main
path: k8s/production
destination:
server: https://kubernetes.default.svc
namespace: my-app
syncPolicy:
automated:
prune: true
selfHeal: true
sourcesays where the desired state is: repository, branch/tag and path inside the repo.destinationsays where to apply it: which cluster (https://kubernetes.default.svcis the cluster ArgoCD itself runs in) and namespace.syncPolicy.automatedturns on automatic sync:selfHeal: truemakes ArgoCD revert manual changes made outside Git (the “drift” mentioned above);prune: trueremoves from the cluster resources that were removed from the repository.
Without syncPolicy.automated, ArgoCD still detects divergences and shows the diff between Git and the cluster, but waits for a manual sync (via the UI or argocd app sync) — a more conservative option to start with, before fully trusting automatic mode.
5. Alternative: Flux
Flux is the most widely used alternative to ArgoCD, with the same proposition — watching a Git repository and reconciling the cluster with what’s in it — but with a philosophy more “native” to Kubernetes’ controller model (without a UI as robust as ArgoCD’s, focused on CRDs and composition via kustomize). This article uses ArgoCD as its main example because it has the gentler learning curve (web UI included), but the GitOps concepts — Git as the source of truth, automatic reconciliation, sync policies — apply equally to both tools, and the choice between them usually depends more on team preference than on a decisive technical difference.
6. Structuring a Manifests Repository
A common question when adopting GitOps is how to organize the manifests repository. A common structure, separating applications and environments:
my-app-manifests/
├── base/
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
└── overlays/
├── dev/
│ ├── kustomization.yaml
│ └── patch-replicas.yaml
├── staging/
│ └── kustomization.yaml
└── production/
├── kustomization.yaml
└── patch-replicas.yaml
This structure uses kustomize (built into kubectl and natively supported by ArgoCD and Flux): the base/ folder holds the common manifests, and each overlay applies only that environment’s differences (number of replicas, resource limits, specific variables) on top of the base, avoiding duplicating the whole YAML for each environment. An ArgoCD Application pointing at overlays/production (as in the previous section’s example) always applies the base plus the production-specific tweaks.
For projects that use Helm instead of plain YAML manifests, the same principle applies: the repository versions each environment’s values.yaml, and the ArgoCD Application points at the chart and the values file for the target environment.
7. Conclusion and Next Steps
In this article, we saw what GitOps is and why it solves real problems of auditing, configuration drift and direct cluster access that manual kubectl apply doesn’t, a practical introduction to ArgoCD with a real Application, a mention of Flux as an alternative, and a common way to structure a manifests repository with kustomize for multiple environments. In the next and final article of this series, the examples get more advanced: full deploy automation with GitOps, automatic rollback, more refined sync policies and best practices for running all of this confidently in production.
References: