GitOps is a methodology for continuous delivery and operation of infrastructure and applications, using Git as the single source of truth for the desired state of your systems. Instead of manually making changes on servers or relying solely on imperative scripts, GitOps ensures all infrastructure configurations—from Kubernetes manifests to cloud resource definitions (via Terraform, Pulumi, etc.)—are declared, version-controlled, and stored in a Git repository. This approach treats infrastructure changes with the same rigor as application code, leveraging familiar development practices like pull requests, code reviews, and automatic versioning to manage your operational environments.
In a practical GitOps workflow, an SRE or developer initiates an infrastructure change by modifying the declarative configuration files within the designated Git repository. For example, updating a resource count in a Kubernetes deployment manifest or adjusting a cloud security group rule. This change is then proposed via a Pull Request (PR), where it undergoes review and approval, ensuring correctness and adherence to policies. Once merged into the main branch, a specialized GitOps agent (like Flux CD or Argo CD) continuously monitors the repository. Upon detecting a change in the desired state in Git, this agent automatically pulls the new configuration and applies it to the live infrastructure, reconciling any differences to ensure the actual state matches the declared state in Git.
This pull-based reconciliation model offers significant advantages. It provides a full audit trail of every infrastructure change, making rollbacks simple and transparent by reverting a Git commit. Security is enhanced because the deployment agents pull changes rather than being pushed credentials, reducing the attack surface. Furthermore, GitOps inherently promotes environment consistency, as development, staging, and production environments are all driven from the same version-controlled definitions, diverging only by specific, declared parameters. It significantly reduces manual errors, accelerates deployments, and fosters a more reliable and observable infrastructure management practice.
Key Takeaways
- Git is the single source of truth for all infrastructure and application configurations.
- Infrastructure changes are made via Git (Pull Requests), reviewed, and merged, just like application code.
- Automated GitOps agents continuously reconcile live infrastructure with the desired state declared in Git.
- Provides a complete audit trail, easy rollbacks, and enhanced security through a pull-based deployment model.
- Ensures environment consistency and reduces manual errors by automating infrastructure management.
Code Example
# myapp-deployment.yaml - A Kubernetes Deployment managed by GitOps
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-frontend
namespace: production
labels:
app: myapp
spec:
replicas: 3 # GitOps agent will ensure 3 instances are running
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: frontend
image: mycompany/myapp-frontend:1.2.0 # Update this version in Git, GitOps deploys it
ports:
- containerPort: 80How this code works
This YAML file defines the desired state for a myapp-frontend application within a Kubernetes cluster. In a GitOps workflow, this declaration in Git is the single source of truth for the production environment. A GitOps agent continuously monitors this file; any committed changes, such as updating the application's version, trigger an automatic deployment to ensure the live infrastructure always precisely matches what's described here.
The kind: Deployment tells Kubernetes to manage a set of identical application instances. The metadata.name and metadata.namespace identify this specific application within the production environment. spec.replicas: 3 ensures three instances of the application are always running, a key aspect of GitOps maintaining the desired state. The spec.template section describes the pods that will be created, including the image: mycompany/myapp-frontend:1.2.0 which specifies the exact software version. A subtle but critical point is that the spec.selector.matchLabels must exactly match the spec.template.metadata.labels (app: myapp here) for the Deployment to correctly find and manage its pods. If these labels mismatch, the Deployment will fail to control the application's instances, preventing proper deployments or updates.