GitOps with Flux CD

GitOps with Flux CD

GitOps is revolutionizing the way teams deploy and manage applications on Kubernetes. This guide will walk you through implementing a GitOps workflow using Flux CD, an open-source continuous delivery tool.

What is GitOps?

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.

With GitOps:

  • Git is the single source of truth for the desired state of your infrastructure
  • Changes to the desired state are declarative and version controlled
  • Approved changes are automatically applied to your infrastructure

Why Flux CD?

Flux CD is a GitOps tool that ensures that your Kubernetes cluster matches the desired state specified in a Git repository. Key features include:

  • Automated sync between your Git repository and cluster state
  • Support for Kustomize, Helm, and plain Kubernetes manifests
  • Multi-tenancy via RBAC
  • Strong security practices, including image verification

Installation

Prerequisites

  • A Kubernetes cluster (K3s, Kind, or any other distribution)
  • kubectl configured to access your cluster
  • A GitHub (or GitLab/Bitbucket) account and repository

Installing Flux

  1. Install the Flux CLI:
curl -s https://fluxcd.io/install.sh | sudo bash
  1. Export your GitHub personal access token:
export GITHUB_TOKEN=<your-token>
  1. Bootstrap Flux:
flux bootstrap github \
  --owner=<your-github-username> \
  --repository=<repository-name> \
  --path=clusters/my-cluster \
  --personal

Setting Up Your First Application

  1. Create a basic directory structure in your Git repository:
└── clusters/
    └── my-cluster/
        ├── flux-system/  # Created by bootstrap
        └── apps/
            └── podinfo/
                ├── namespace.yaml
                ├── deployment.yaml
                └── service.yaml
  1. Create a Flux Kustomization to deploy your app:
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
  name: podinfo
  namespace: flux-system
spec:
  interval: 5m0s
  path: ./clusters/my-cluster/apps/podinfo
  prune: true
  sourceRef:
    kind: GitRepository
    name: flux-system
  1. Commit and push your changes, and Flux will automatically deploy your application!

Advanced Features

Automated Image Updates

Flux can automatically update your deployments when new images are available:

apiVersion: image.toolkit.fluxcd.io/v1beta1
kind: ImageRepository
metadata:
  name: podinfo
  namespace: flux-system
spec:
  image: ghcr.io/stefanprodan/podinfo
  interval: 1m0s
---
apiVersion: image.toolkit.fluxcd.io/v1beta1
kind: ImagePolicy
metadata:
  name: podinfo
  namespace: flux-system
spec:
  imageRepositoryRef:
    name: podinfo
  policy:
    semver:
      range: 6.x.x

Working with Helm Charts

Flux makes it easy to manage Helm releases:

apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: HelmRepository
metadata:
  name: bitnami
  namespace: flux-system
spec:
  interval: 30m
  url: https://charts.bitnami.com/bitnami
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: redis
  namespace: flux-system
spec:
  interval: 5m
  chart:
    spec:
      chart: redis
      version: "16.x"
      sourceRef:
        kind: HelmRepository
        name: bitnami
  values:
    architecture: standalone

Conclusion

Flux CD provides a powerful, secure, and flexible platform for implementing GitOps workflows. By following this guide, you’ll be well on your way to managing your Kubernetes infrastructure using GitOps principles.

Stay tuned for more advanced GitOps patterns and best practices!