Skip to main content

How to implement GitOps with Flux

Bootstrap Flux into Kubernetes and reconcile workloads from Git using GitRepository and Kustomization resources. Covers bootstrap, sources, pruning, and reconciliation.

Difficulty
Intermediate
Duration
55 minutes
Steps
6

What and why

Flux is a GitOps toolkit for Kubernetes. It watches Git sources and applies their manifests to the cluster on a schedule, keeping the cluster aligned with Git. Unlike a UI-first tool, Flux is configured entirely through Kubernetes custom resources. This tutorial bootstraps Flux and reconciles an app.

Prerequisites

  • A Kubernetes cluster and kubectl access.
  • A Git provider repo plus a personal access token with repo scope.
  • The flux CLI installed.

Steps

1. Install the flux CLI

Install the CLI and confirm:

flux --version

2. Run the pre-flight check

flux check --pre

This verifies your cluster version and permissions before installing anything.

3. Bootstrap Flux

flux bootstrap github \
  --owner=acme \
  --repository=fleet \
  --path=clusters/prod \
  --personal

Bootstrap installs Flux controllers and commits its own config into your repo, so Flux manages itself via GitOps.

4. Define a GitRepository source

apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
  name: app
  namespace: flux-system
spec:
  interval: 1m
  url: https://github.com/acme/app
  ref:
    branch: main

A source tells Flux where to fetch manifests and how often.

5. Add a Kustomization

apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: app
  namespace: flux-system
spec:
  interval: 5m
  sourceRef:
    kind: GitRepository
    name: app
  path: ./deploy
  prune: true

The Kustomization applies a path from the source and prunes removed resources.

Verification

Run flux get kustomizations. The app should show Ready: True. Push a change to the manifest path and run flux reconcile kustomization app; the change appears in the cluster, confirming the loop works.

Next Steps

Add image automation to bump tags from a registry. Use SOPS to encrypt secrets in Git. Separate environments by path or repository to control blast radius.

Prerequisites

  • A Kubernetes cluster
  • A GitHub or GitLab repo and token
  • kubectl and the flux CLI installed

Steps

  • 1
    Install the flux CLI
  • 2
    Run the pre-flight check
  • 3
    Bootstrap Flux
  • 4
    Define a GitRepository source
  • 5
    Add a Kustomization
  • 6
    Verify reconciliation

Category

DevOps