0
0
Kubernetesdevops~7 mins

FluxCD for continuous delivery in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
FluxCD helps automatically update your Kubernetes apps when you change files in a Git repository. It solves the problem of manually applying changes to your cluster by syncing your app state from Git.
When you want your Kubernetes apps to update automatically after you change code or configs in Git.
When you want to keep your cluster state consistent with a Git repository as the single source of truth.
When you want to avoid manual kubectl commands for deploying app updates.
When you want to track and audit changes to your Kubernetes apps through Git history.
When you want to enable safe rollbacks by reverting Git commits.
Config File - flux-system/gotk-sync.yaml
flux-system/gotk-sync.yaml
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: GitRepository
metadata:
  name: my-app-repo
  namespace: flux-system
spec:
  interval: 1m0s
  url: https://github.com/example/my-app-configs.git
  ref:
    branch: main
---
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 5m0s
  path: ./deploy
  prune: true
  sourceRef:
    kind: GitRepository
    name: my-app-repo
  validation: client
  suspend: false

This file tells FluxCD where your Git repository is and how often to check it for changes.

The GitRepository resource points to your app's Git repo and branch.

The Kustomization resource tells FluxCD which folder in the repo to apply to the cluster and how often to sync.

prune: true means Flux will remove resources no longer in Git.

Commands
Installs FluxCD components into your Kubernetes cluster to enable GitOps continuous delivery.
Terminal
flux install
Expected OutputExpected
► installing components ✔ components installed ► verifying installation ✔ installation verified
Applies the GitRepository and Kustomization resources to tell Flux where your app configs live and how to sync them.
Terminal
kubectl apply -f flux-system/gotk-sync.yaml
Expected OutputExpected
gitrepository.source.toolkit.fluxcd.io/my-app-repo created kustomization.kustomize.toolkit.fluxcd.io/my-app created
Checks the status of your app sync to see if Flux has applied the configs from Git to your cluster.
Terminal
flux get kustomizations
Expected OutputExpected
NAME READY STATUS AGE my-app True Applied revision main/abc1234 1m
Verifies that your app pods are running in the cluster after Flux applied the changes.
Terminal
kubectl get pods -n my-app-namespace
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-deployment-abc123 1/1 Running 0 2m
Key Concept

FluxCD continuously monitors your Git repo and automatically applies changes to your Kubernetes cluster to keep them in sync.

Common Mistakes
Not installing FluxCD components before applying GitRepository and Kustomization resources.
Flux commands and resources won't work because the Flux controllers are not running in the cluster.
Always run 'flux install' first to set up FluxCD before applying your Git sync configs.
Using incorrect Git repo URL or branch in the GitRepository resource.
Flux cannot fetch your app configs, so no changes are applied to the cluster.
Double-check the Git URL and branch name are correct and accessible from the cluster.
Not waiting or checking Flux sync status after applying configs.
You might think your app is deployed but Flux may still be syncing or have errors.
Use 'flux get kustomizations' to verify the sync status and troubleshoot if needed.
Summary
Install FluxCD components in your Kubernetes cluster using 'flux install'.
Create GitRepository and Kustomization resources to tell Flux where your app configs are and how to sync.
Apply these resources with kubectl to start continuous delivery from Git to your cluster.
Check Flux sync status with 'flux get kustomizations' to confirm your app is deployed.
Verify your app pods are running with 'kubectl get pods' in the app namespace.