Kustomization
Table of Contentsβ
1 - Conceptβ
What is a Flux Kustomization?β
A Flux Kustomization applies Kubernetes manifests from a source (GitRepository).
warning
Do not confuse it with Kustomize's kustomization.yaml. The Flux Kustomization is a CRD that uses the Kustomize Controller.
Workflowβ
- The Source Controller downloads the repo
- The Kustomize Controller reads the specified path
- It applies the manifests with
kubectl apply - It checks the health of the resources
- It loops according to the
interval
π Back to table of contentsβ
2 - Create a Kustomizationβ
Basic YAMLβ
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: my-app
namespace: flux-system
spec:
interval: 5m
path: ./apps/my-app
prune: true
sourceRef:
kind: GitRepository
name: my-repo
targetNamespace: default
Via CLIβ
flux create kustomization my-app \
--source=GitRepository/my-repo \
--path="./apps/my-app" \
--prune=true \
--interval=5m \
--target-namespace=default
With Kustomize overlaysβ
# Structure du repo
apps/my-app/
βββ base/
β βββ kustomization.yaml
β βββ deployment.yaml
β βββ service.yaml
βββ overlays/
βββ dev/
β βββ kustomization.yaml
βββ prod/
βββ kustomization.yaml
# Flux Kustomization pour prod
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: my-app-prod
namespace: flux-system
spec:
interval: 5m
path: ./apps/my-app/overlays/prod
prune: true
sourceRef:
kind: GitRepository
name: my-repo
π Back to table of contentsβ
3 - Advanced optionsβ
Prune (automatic deletion)β
spec:
prune: true # Supprime les ressources orphelines
Force (recreate if necessary)β
spec:
force: true # RecrΓ©er au lieu de patcher
Variable substitutionβ
spec:
postBuild:
substitute:
ENVIRONMENT: production
REPLICAS: "5"
substituteFrom:
- kind: ConfigMap
name: cluster-config
- kind: Secret
name: cluster-secrets
# Dans le manifest
apiVersion: apps/v1
kind: Deployment
spec:
replicas: ${REPLICAS}
template:
spec:
containers:
- name: app
env:
- name: ENV
value: ${ENVIRONMENT}
Inline patchesβ
spec:
patches:
- patch: |
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
target:
kind: Deployment
name: my-app
Imagesβ
spec:
images:
- name: nginx
newName: my-registry/nginx
newTag: v1.2.3
π Back to table of contentsβ
4 - Dependenciesβ
dependsOnβ
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: app
namespace: flux-system
spec:
dependsOn:
- name: infrastructure # DΓ©ployer infra d'abord
- name: secrets
interval: 5m
path: ./apps/my-app
sourceRef:
kind: GitRepository
name: my-repo
Deployment orderβ
# infrastructure.yaml
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: infrastructure
spec:
path: ./infrastructure
...
---
# database.yaml
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: database
spec:
dependsOn:
- name: infrastructure
path: ./apps/database
...
---
# backend.yaml
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: backend
spec:
dependsOn:
- name: database
path: ./apps/backend
...
π Back to table of contentsβ
5 - Health Checksβ
healthChecksβ
spec:
healthChecks:
- apiVersion: apps/v1
kind: Deployment
name: my-app
namespace: default
timeout: 3m
waitβ
spec:
wait: true # Attendre que toutes les ressources soient prΓͺtes
timeout: 5m
Retryβ
spec:
retryInterval: 1m # RΓ©essayer si Γ©chec
π Back to table of contentsβ
6 - Hands-on exercisesβ
Exercise 1: Deploy podinfoβ
# 1. CrΓ©er la source
flux create source git podinfo \
--url=https://github.com/stefanprodan/podinfo \
--branch=master
# 2. CrΓ©er la Kustomization
flux create kustomization podinfo \
--source=GitRepository/podinfo \
--path="./kustomize" \
--prune=true \
--interval=5m \
--target-namespace=default
# 3. VΓ©rifier
flux get kustomizations
kubectl get pods
Exercise 2: Declarative YAMLβ
# podinfo.yaml
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
name: podinfo
namespace: flux-system
spec:
interval: 1m
url: https://github.com/stefanprodan/podinfo
ref:
branch: master
---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: podinfo
namespace: flux-system
spec:
interval: 5m
path: ./kustomize
prune: true
sourceRef:
kind: GitRepository
name: podinfo
targetNamespace: default
Quizβ
Q1. What does prune: true do?
Answer
prune: true automatically deletes the Kubernetes resources that are no longer present in Git. If you delete a file from the repo, the corresponding resource will be deleted from the cluster.
Q2. How do you ensure that an app is deployed after the database?
Answer
Use dependsOn:
spec:
dependsOn:
- name: database
The Kustomization waits for database to be Healthy before deploying.
π Back to table of contentsβ
Key takeawaysβ
- Flux Kustomization β Kustomize's kustomization.yaml
sourceRefpoints to a GitRepositorypathindicates the path in the repoprune: trueto clean up orphansdependsOnfor deployment order- Variables with
postBuild.substitute