codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☸️

Kubernetes

30 / 82 topics
28Kubernetes in DevOps Pipelines29Using Jenkins with Kubernetes30GitOps in Kubernetes
Tutorials/Kubernetes/GitOps in Kubernetes
☸️Kubernetes

GitOps in Kubernetes

Updated 2026-05-15
10 min read

GitOps in Kubernetes

Introduction

GitOps is a set of practices that treats infrastructure as code and uses Git as the single source of truth for declarative management of applications. In the context of Kubernetes, GitOps allows you to manage your cluster's configuration and state through Git repositories, enabling continuous deployment and infrastructure as code (IaC) principles.

By adopting GitOps, teams can achieve better collaboration, traceability, and reliability in managing their Kubernetes clusters. This tutorial will guide you through implementing GitOps for declarative management of Kubernetes clusters, focusing on integration with CI/CD pipelines.

Concept

At the core of GitOps is the idea that all changes to your infrastructure should be made through a version-controlled system like Git. The desired state of your cluster is defined in YAML files stored in a Git repository. A tool like Argo CD or Flux watches this repository and automatically applies changes to the Kubernetes cluster to match the desired state.

Key Components

  1. Git Repository: Stores all Kubernetes manifests (YAML files) that define the desired state of the cluster.
  2. CI/CD Pipeline: Automates the process of building, testing, and deploying applications.
  3. GitOps Operator: Monitors the Git repository for changes and applies them to the Kubernetes cluster.

Examples

Step 1: Set Up a Git Repository

First, create a new Git repository to store your Kubernetes manifests. You can use any Git hosting service like GitHub, GitLab, or Bitbucket.

Terminal
$ git init my-k8s-config
$ cd my-k8s-config
$ git remote add origin https://github.com/yourusername/my-k8s-config.git

Step 2: Define Kubernetes Manifests

Create a simple deployment and service manifest for a sample application.

YAML
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4name: nginx-deployment
5spec:
6replicas: 3
7selector:
8 matchLabels:
9 app: nginx
10template:
11 metadata:
12 labels:
13 app: nginx
14 spec:
15 containers:
16 - name: nginx
17 image: nginx:1.14.2
18 ports:
19 - containerPort: 80
20
21---
22apiVersion: v1
23kind: Service
24metadata:
25name: nginx-service
26spec:
27selector:
28 app: nginx
29ports:
30 - protocol: TCP
31 port: 80
32 targetPort: 80
33type: LoadBalancer

Commit these files to your Git repository.

Terminal
$ git add .
$ git commit -m "Initial Kubernetes manifests"
$ git push origin master

Step 3: Set Up a CI/CD Pipeline

For this example, we'll use GitHub Actions as our CI/CD tool. Create a .github/workflows/deploy.yml file in your repository.

YAML
1name: Deploy to Kubernetes
2on:
3push:
4 branches:
5 - master
6jobs:
7deploy:
8 runs-on: ubuntu-latest
9 steps:
10 - name: Checkout code
11 uses: actions/checkout@v2
12 - name: Set up kubectl
13 uses: azure/setup-kubectl@v1
14 with:
15 version: 'latest'
16 - name: Deploy to Kubernetes
17 run: |
18 kubectl apply -f .

This workflow will automatically deploy your Kubernetes manifests whenever changes are pushed to the master branch.

Step 4: Monitor and Manage Changes

With GitOps, you can monitor changes through your Git repository. Any modifications to the YAML files in the repository will trigger a new deployment via the CI/CD pipeline.

Terminal
$ git pull origin master
$ git checkout -b feature/update-nginx
# Edit nginx-deployment.yaml to update the image version
$ git add .
$ git commit -m "Update Nginx image version"
$ git push origin feature/update-nginx

Once the changes are pushed, GitHub Actions will automatically apply them to your Kubernetes cluster.

What's Next?

After setting up GitOps for your Kubernetes clusters, you can explore more advanced topics such as:

  • Upgrading and Maintaining Kubernetes Clusters: Learn how to manage upgrades, rollbacks, and maintenance tasks in a GitOps environment.
  • Securing Your Git Repository: Implement best practices for securing your Git repository to prevent unauthorized changes.
  • Advanced CI/CD Integrations: Integrate with other tools like Helm, Argo CD, or Flux for more advanced GitOps workflows.

By following these steps, you can effectively implement GitOps in your Kubernetes environment, ensuring a declarative and reliable management of your clusters.


PreviousUsing Jenkins with KubernetesNext Upgrading and Maintaining Kubernetes Clusters

Recommended Gear

Using Jenkins with KubernetesUpgrading and Maintaining Kubernetes Clusters