Phase 5: Platform Engineering

GitOps workflows for infrastructure changes

Intermediate ~3 min read
Think of it this way A friendly analogy. Read this if the technical version feels dense. Show Hide

Imagine you're building a gigantic LEGO city, one so big that many people are helping to put it together, and it's always growing! If everyone just added blocks wherever they wanted, it would get messy, confusing, and pieces might go missing. That's why we use a special way to build called "GitOps." Think of it like having one super important, master instruction book for your entire LEGO city. This book doesn't just list steps; it perfectly describes exactly what your finished city should look like – every building, every road, every tiny tree, right down to the color of each block.

Now, let's say someone wants to add a brand new, taller tower to the city. Instead of just grabbing blocks and building it, they first update the relevant pages in this master instruction book. These updated pages are like a new "draft" plan for the tower. They then share this draft plan with the other city builders. Everyone can look at the draft, check if the tower will fit, if it looks good, and if it might cause any other buildings to fall over. This way, everyone agrees on the changes before they're actually built, making sure everything is correct and follows the city's rules.

Once everyone gives their thumbs-up, those new tower plans become official and are added to the main master instruction book. Here's where the magic of GitOps happens: we have a super smart, helpful LEGO robot builder. This robot constantly reads the official instruction book. If the book says there should be a 10-story red tower in a certain spot, and the robot sees only an 8-story blue one, it automatically goes and fixes the city to match the book perfectly. The robot makes sure the real LEGO city always looks exactly like the most up-to-date, approved plans in the book.

So, instead of people trying to remember what goes where or manually moving blocks around, everyone just updates the master instruction book. This means you can build incredibly complex and reliable LEGO cities because you always know they're built exactly as planned. It also means you can quickly undo mistakes by just going back to an older version of the plans in the book, and the robot will rebuild it that way, helping you create amazing and perfectly managed digital worlds.

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

yaml
# 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: 80

How 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.