What is GitOps?
Tags: devops, kubernetes, docker, infrastructureascode
Published: 2024-04-23 14:46:22

Everyone's definitely come across the term "DevOps" but have you also heard about "GitOps"? 

<img width="100%" style="width:100%" src="https://media1.tenor.com/m/Lj5IpK4zW8oAAAAC/gitops.gif" />

Well in this post we will touch the surface on what is GitOps and how is it making our lives easier.

Let us first look at the formal definition :

> GitOps is an operational framework that takes DevOps best practices used for application development such as version control, collaboration, compliance, and CI/CD, and applies them to infrastructure automation.

in simpler terms:

"Imagine you're making a delicious cake. GitOps is like having a recipe book that not only tells you how to make the cake but also keeps track of every change you make to the recipe. So, if you decide to add more sugar or change the baking time, GitOps ensures that everyone who helps with the baking knows about it. It's like having a magical cookbook that keeps everything organized and ensures that your cake turns out perfectly every time, no matter who's in the kitchen!"

<img width="100%" style="width:100%" src="https://media1.tenor.com/m/9axwaN_W9_8AAAAC/baking-cake.gif" />

It is basically a methodoloy implemented to better manage your infrastructure (K8s clusters) and keep track of all changes being made.

GitOps can be either push or pull-based, depending on the specific implementation and the needs of the workflow. In a push-based GitOps approach, changes are directly pushed to the Git repository, triggering automated processes to deploy those changes to the target environment. 

In contrast, a pull-based GitOps approach involves the continuous monitoring of the Git repository by an operator or controller, which then pulls changes from the repository and applies them to the target environment. 

Both approaches have their advantages and are used in different scenarios based on factors like security, scalability, and operational preferences.

Let's consider a real-world example of GitOps in the context of deploying and managing cloud infrastructure using Kubernetes.

Imagine a team responsible for deploying and managing microservices-based applications on Kubernetes clusters. They have their infrastructure defined as code using tools like Terraform or AWS CloudFormation, and their application deployments configured with Kubernetes manifests.

With GitOps:

1. **Infrastructure as Code (IaC) Repository**: The team maintains a Git repository containing the infrastructure code (Terraform or CloudFormation templates) to provision and configure the Kubernetes clusters. This repository serves as the single source of truth for their infrastructure configuration.

2. **Application Code Repositories**: Each microservice team maintains its own Git repository containing the application code along with Kubernetes manifests (e.g., Deployment, Service, Ingress definitions) required to deploy and manage the application on Kubernetes.

3. **Continuous Integration (CI)**: Whenever there's a change in the infrastructure code or application code repositories, CI pipelines are triggered. These pipelines run automated tests, build artifacts, and prepare deployment packages.

4. **Git as a Source of Truth**: Once the CI pipelines are successful, the changes are committed to the respective branches of the Git repositories. The Git repositories act as the source of truth for both infrastructure and application configurations.

5. **GitOps Operator**: A GitOps operator (e.g., Flux, Argo CD) continuously monitors the Git repositories containing the infrastructure and application code. It watches for changes and applies them automatically to the target Kubernetes clusters.

6. **Automated Deployments**: When changes are pushed to the Git repositories, the GitOps operator detects these changes and automatically applies them to the Kubernetes clusters. This includes provisioning or updating infrastructure resources and deploying or updating application workloads.

7. **Version Control and Rollbacks**: Since everything is version-controlled in Git, it's easy to roll back to previous configurations in case of issues or failures. Teams can simply revert the changes in the Git repository to restore the system to a known good state.

This approach ensures that the entire system's configuration is version-controlled, auditable, and automated. It provides visibility into changes, improves traceability, and reduces the risk of configuration drift or manual errors in the deployment process.

GitOps operators like FluxCD, Argo CD, or Jenkins X are used to automate the deployment and management of infrastructure and applications based on Git repositories. These operators continuously monitor Git repositories for changes and apply them to the target environment.

In the fast-paced world of software development, GitOps is a game-changer. By leveraging the power of Git for version control and automation, teams can deploy applications faster, with fewer errors, and greater confidence.