GitOps has revolutionized how we manage Kubernetes deployments by treating Git as the single source of truth for declarative infrastructure and applications. In this post, we’ll explore how to get started with Flux CD, one of the most popular GitOps tools.

What is GitOps?

GitOps is a modern approach to continuous delivery that uses Git repositories as the source of truth for defining infrastructure and application code. The key principles include:

  • Declarative configuration: Everything is described declaratively in Git
  • Version control: All changes are tracked and auditable
  • Automated synchronization: Changes in Git trigger automatic updates to your cluster
  • Continuous reconciliation: The system constantly ensures the cluster state matches Git

Why Flux CD?

Flux CD is a CNCF graduated project that brings GitOps to Kubernetes. Here’s why it’s a great choice:

  1. Native Kubernetes integration: Built specifically for Kubernetes
  2. Helm support: First-class support for Helm charts
  3. Multi-tenancy: Supports multiple teams and repositories
  4. Progressive delivery: Built-in support for canary deployments

Setting Up Flux

Prerequisites

Before you begin, ensure you have:

  • A Kubernetes cluster (v1.20 or newer)
  • kubectl configured to access your cluster
  • A GitHub personal access token

Installation

Install the Flux CLI:

curl -s https://fluxcd.io/install.sh | sudo bash

Bootstrap Flux in your cluster:

flux bootstrap github \
  --owner=<github-username> \
  --repository=<repo-name> \
  --branch=main \
  --path=clusters/production \
  --personal

This command will:

  • Create a GitHub repository (if it doesn’t exist)
  • Install Flux components in your cluster
  • Configure Flux to monitor the repository
  • Commit Flux manifests to the repository

Creating Your First GitOps Deployment

Create a HelmRelease manifest:

apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: nginx
  namespace: default
spec:
  interval: 5m
  chart:
    spec:
      chart: nginx
      version: "15.x"
      sourceRef:
        kind: HelmRepository
        name: bitnami
        namespace: flux-system
  values:
    replicaCount: 2

Commit this to your Git repository, and Flux will automatically deploy it to your cluster!

Best Practices

  1. Use separate repositories: Keep infrastructure and application code separate
  2. Implement proper RBAC: Limit Flux’s permissions appropriately
  3. Enable notifications: Get alerts on reconciliation failures
  4. Use Kustomize overlays: Manage environment-specific configurations
  5. Implement health checks: Monitor your deployments actively

Conclusion

GitOps with Flux provides a robust, auditable, and automated approach to Kubernetes deployments. By treating Git as the source of truth, you gain version control, easy rollbacks, and a clear audit trail of all changes.

Start small, perhaps with a single namespace or application, and gradually expand your GitOps practices across your infrastructure.