In brief
GitOps is a way of running deployments in which a Git repository holds the description of the system's desired state, and software running in the environment pulls that description and brings the actual state in line with it. A deployment is a merge into the repository, and a rollback is a reverted commit. The OpenGitOps project defines GitOps through four principles: desired state that is declarative, versioned and immutable, pulled automatically and continuously reconciled. It is most often used with Kubernetes and the Argo CD or Flux tools.
The four OpenGitOps principles
OpenGitOps is a project of the GitOps Working Group within CNCF TAG App Delivery. It has published version 1.0.0 of the principles that define what can be called GitOps:
- Declarative. A system managed by GitOps has its desired state expressed declaratively: what should run, not which commands to execute.
- Versioned and immutable. The desired state is stored in a way that enforces immutability and versioning and keeps a complete version history.
- Pulled automatically. Software in the environment pulls the desired state declarations from the source on its own.
- Continuously reconciled. The same software continuously observes the actual system state and tries to bring it to the desired state.
The principles do not name Git. Git is the most common source because it gives you history, change review through pull requests and signed commits, but the source can also be an artifact registry.
The pull model versus a classic pipeline
In a classic CI/CD pipeline, the build server has permissions on the cluster and pushes changes with `kubectl apply` or `helm upgrade`. GitOps reverses the direction: a controller running in the cluster pulls the description from the repository and applies the changes itself.
| Aspect | Push pipeline | GitOps (pull) |
|---|---|---|
| Who has cluster permissions | the CI server | the controller in the cluster |
| Detecting manual changes | none, the change stays until the next deployment | the controller detects drift and can revert it |
| Rolling back a version | rerunning the pipeline with the old version | reverting a commit |
| Audit trail | CI server logs | repository and pull request history |
| Multiple clusters | the pipeline has to know every cluster | each cluster pulls its own description |
For teams in regulated industries, the audit trail row matters. Every production change has an author, a review and a date in the repository history, so answering the auditor's question “who changed the configuration and when” does not require collecting logs from several tools.
CI does not go away. It still builds the image, runs tests and scans dependencies. Only the last step changes: instead of deploying, the pipeline updates the image version in the configuration repository.
Argo CD and Flux
The two most widely used GitOps tools are Argo CD and Flux. Both are CNCF graduated projects: Flux since November 30, 2022, and Argo since December 6, 2022.
- Argo CD has a rich web UI with a view of the application's resources and the differences between the state in the repository and in the cluster. Its unit is the `Application` resource, which points to a repository, a path and a target cluster.
- Flux is a set of controllers driven by Kubernetes resources, with no central UI out of the box. It suits teams that prefer to manage everything through manifests and the command line.
A sample Argo CD application with automated reconciliation:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: payments-api
namespace: argocd
spec:
project: default
source:
repoURL: https://git.example.com/platform/config.git
targetRevision: main
path: environments/production/payments-api
destination:
server: https://kubernetes.default.svc
namespace: payments
syncPolicy:
automated:
prune: true
selfHeal: trueThe `selfHeal` option means a manual change in the cluster is reverted to the state from the repository. The `prune` option deletes resources that disappeared from the repository. Turn both on deliberately, on test environments first.
Repositories, environments and secrets
A few decisions have to be made at the start, because they are hard to change later:
- A separate configuration repository. Keeping application code and deployment descriptions in separate repositories decouples the code change cycle from the release cycle and limits who can change production.
- Environments as directories, not branches. Directories such as `testy`, `przedprodukcja` and `produkcja` with Kustomize overlays or Helm values are easier to compare and merge than long-lived environment branches.
- Promotion through a pull request. Moving a version from pre-production to production means changing the image version in one file, with review and approval.
- Never plain-text secrets in the repository. The repository holds either encrypted secrets or only references to a vault from which the controller fetches values in the cluster. Details are in the article on secrets in applications.
- Main branch protection. Required review, green tests and signed commits, because whoever can merge a change can change production.
GitOps pairs well with infrastructure as code: Terraform creates clusters and cloud services, and GitOps manages what runs in them.
When GitOps makes sense and how to start
GitOps delivers the most where there are many environments and clusters, changes are frequent and the company has to show who changed production and when. For a single application on a single server without Kubernetes, a regular pipeline is enough. When Kubernetes pays off is covered in the article Kubernetes in the enterprise.
A starting plan for a few weeks:
- Pick one low-risk application and one test environment.
- Move its manifests to a configuration repository and install Argo CD or Flux.
- Remove the CI server's write permissions on that cluster.
- Turn on automated reconciliation and check what happens on a manual change in the cluster.
- Add promotion to the next environment through a pull request, then further applications.
In larger organizations GitOps becomes part of a shared platform, as we describe in the article on platform engineering. An example of a Kubernetes platform for network functions and IT systems is described in the Kubernetes platform at a telecom operator case study.
Questions and answers
How is GitOps different from CI/CD?
CI/CD is the whole process from a code change to a running version: build, tests and deployment. GitOps is a way of carrying out the deployment itself: desired state in a repository and a controller in the environment that pulls and reconciles it. CI still builds and tests, and instead of deploying, it updates the version in the configuration repository.
Does GitOps only work with Kubernetes?
The OpenGitOps principles do not require Kubernetes, but tools such as Argo CD and Flux are built for it, because Kubernetes has a declarative API and reconciliation loops. For virtual machines, networks or cloud services, a similar approach comes from infrastructure as code that runs automatically after a merge, though usually without continuous reconciliation.
How do you roll back a faulty deployment with GitOps?
Revert the commit or merge a pull request that restores the previous image version. The controller pulls the change and brings the cluster back to the previous state. In an emergency you can also pause automated reconciliation and fix the state manually, but then you have to record the fix in the repository, or the controller will revert it on the next reconciliation.
Does GitOps help with audits and regulatory compliance?
Yes, because the repository history is a log of production changes: author, review, approval and date. That makes it easier to demonstrate the change control required by DORA and ISO 27001. The conditions are a protected main branch, no manual changes in the cluster and secrets stored outside the repository or encrypted.