GitOps with FluxCD
Source and Kustomize Controller
DEMO Source Controller Git Manifest in Flux Repo
In this guide, you’ll learn how Flux’s Source Controller pulls manifests from a Git repository and how the Kustomize Controller applies them to a Kubernetes cluster, showcasing a GitOps-driven workflow.
Note
Ensure you’ve bootstrapped Flux CD with SSH credentials so the secretRef
in your GitRepository
can authenticate to GitHub.
See Flux CD installation for details.
1. Define GitRepository and Kustomization
Create a GitRepository
to tell Flux where to fetch your manifests, then reference it in a Kustomization
:
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: GitRepository
metadata:
name: flux-system
namespace: flux-system
spec:
interval: 10s
url: ssh://[email protected]/sidd-harth-2/block-buster
ref:
branch: main
secretRef:
name: flux-system
---
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: flux-system
namespace: flux-system
spec:
interval: 10s
path: ./flux-clusters/dev-cluster
prune: true
sourceRef:
kind: GitRepository
name: flux-system
Key fields explained:
interval
: reconciliation frequencyurl
/ref
: Git source and branchsecretRef
: SSH key for cloningpath
: directory in the repo to applyprune
: deletes resources removed from Git
Flux Controllers Overview
Flux Controller | Resource | Purpose | Example CLI |
---|---|---|---|
Source Controller | GitRepository | Fetches manifests from Git | flux get sources git |
Kustomize Controller | Kustomization | Applies overlays and syncs resources | flux get kustomizations |
2. Verify Flux Sources and Kustomizations
Confirm that Flux has registered your sources and kustomizations:
# List all registered sources
flux get sources
# Filter for Git sources
flux get sources git
# NAME REVISION READY MESSAGE
# flux-system main@sha1:edf2288f True stored artifact for revision 'main@sha1:edf2288f'
# List Kustomizations
flux get kustomizations
# NAME REVISION READY MESSAGE
# flux-system main@sha1:edf2288f True Applied revision: main@sha1:edf2288f
3. Import and Clone the Demo Application
We’ll use a repository named bb-app-source
containing both app code and Kubernetes manifests.
Import into your GitHub account
Clone locally and switch to the demo branch
git clone https://github.com/your-account/bb-app-source cd bb-app-source git checkout 1-demo
4. Examine Application Source and Manifests
- src/: PHP app reporting its Pod and namespace.
- manifests/1-demo/: Contains three YAML definitions:
deployment.yml
namespace.yml
service.yml
Here’s the Deployment
snippet (manifests/1-demo/deployment.yml
):
apiVersion: apps/v1
kind: Deployment
metadata:
name: block-buster
namespace: 1-demo
spec:
replicas: 1
selector:
matchLabels:
app: block-buster
version: "7.1.0"
env: dev
template:
metadata:
labels:
app: block-buster
version: "7.1.0"
env: dev
spec:
containers:
- name: app
image: siddharth67/block-buster-dev:7.1.0
imagePullPolicy: Always
resources:
requests:
memory: "10Mi"
cpu: "10m"
limits:
memory: "20Mi"
cpu: "50m"
5. Add Manifests to Your Flux Cluster Repo
Copy the demo manifests into your Flux cluster repository under flux-clusters/dev-cluster/1-demo
:
cp manifests/1-demo/*.yml \
../block-buster/flux-clusters/dev-cluster/1-demo/
cd ../block-buster/flux-clusters/dev-cluster/
git add 1-demo/*.yml
git commit -m "Add 1-demo manifests"
git push
Flux will detect these new files and begin applying them automatically.
6. Verify the GitOps Deployment
- Check Kubernetes namespaces
kubectl get ns # NAME STATUS AGE # 1-demo Active 30s # default Active 49m # flux-system Active 42m
- Confirm Flux has the latest revision
flux get sources git flux get kustomizations
7. Inspect the Source Controller Cache
Dive into the Source Controller pod to view the cached repository archive:
kubectl -n flux-system exec -it deploy/source-controller -- sh
cd data/gitrepository/flux-system/flux-system
ls -ltr
# latest.tar.gz -> ...cf1664a0b9...tar.gz
tar -tf latest.tar.gz
# flux-clusters/
# flux-clusters/dev-cluster/1-demo/deployment.yaml
# flux-clusters/dev-cluster/1-demo/namespace.yaml
# flux-clusters/dev-cluster/1-demo/service.yaml
exit
8. Access the Deployed Application
- List resources in the demo namespace
kubectl -n 1-demo get all # pod/block-buster-... 1/1 Running 0 2m # service/block-buster-svc NodePort 10.110.102.187 <none> 80:30001/TCP 2m
- Open the game in your browser
http://localhost:30001
This PHP-based game displays live pod metadata, demonstrating a full GitOps flow with Flux CD.
Links and References
- Flux CD Source Controller
- Flux CD Kustomize Controller
- Kubernetes Documentation
- GitHub Repository Importing
Watch Video
Watch video content
Practice Lab
Practice lab