Back to Articles
Kubernetes

Implementing GitOps CI/CD Pipelines for Kubernetes using Terraform

June 15, 20269 Min Read|By Sanjeev Kumar
Implementing GitOps CI/CD Pipelines for Kubernetes using Terraform

Key Takeaways

  • Git acts as the single source of truth for both infrastructure and application configurations.
  • Terraform provisions the base cluster, while GitOps controllers (ArgoCD) maintain application consistency.
  • Automated reconciliation loops prevent manual configuration adjustments (drift) from surviving.

In traditional cloud operations, infrastructure is provisioned by operations teams, while application deployment code is managed by developers. This separation often results in mismatched configurations, deployment bottlenecks, and difficulty auditing who changed what.

By applying **GitOps** principles, we treat infrastructure and deployment configurations identically to source code.

The GitOps Architecture Pattern

The GitOps workflow follows three core rules:

  • **Declarative Descriptions**: The entire system state (VPC, clusters, namespaces, services, and replicas) is declared in git files (YAML/HCL).
  • 2. **Single Source of Truth**: Git is the only authority. No developer or administrator changes live resources via a GUI or CLI manually.

    3. **Software Agents**: Active agents in the cluster constantly pull target configurations from git, comparing them with the live state and auto-syncing differences.


    Phase 1: Provisioning with Terraform

    We use Terraform to define the VPC network, Subnets, and IAM roles, plus the Kubernetes cluster itself (like AWS EKS or Azure AKS):

    # main.tf
    module "eks" {
      source  = "terraform-aws-modules/eks/aws"
      version = "~> 20.0"
    
      cluster_name    = "sanjeev-prod-cluster"
      cluster_version = "1.29"
    
      vpc_id     = module.vpc.vpc_id
      subnet_ids = module.vpc.private_subnets
    
      eks_managed_node_groups = {
        general = {
          min_size     = 2
          max_size     = 5
          desired_size = 3
          instance_types = ["t3.medium"]
        }
      }
    }

    Phase 2: GitOps sync with ArgoCD

    Once the cluster is running, we deploy ArgoCD into the cluster. ArgoCD watches a specific repository folder containing Kubernetes manifests (Helm charts or Kustomize files) and applies them.

    When a developer merges code:

  • GitHub Actions builds a Docker image and pushes it to Amazon ECR.
  • 2. The pipeline updates the image tag inside the git deployment manifest.

    3. ArgoCD detects the change in the manifest and rolls out the new image to Kubernetes.

    This process eliminates the need to expose Kubernetes cluster credentials to CI pipelines, making the setup exceptionally secure.