Skip to main content

Kustomization


Table of Contents​

  1. Concept
  2. Create a Kustomization
  3. Advanced options
  4. Dependencies
  5. Health Checks
  6. Hands-on exercises

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​

  1. The Source Controller downloads the repo
  2. The Kustomize Controller reads the specified path
  3. It applies the manifests with kubectl apply
  4. It checks the health of the resources
  5. 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
  • sourceRef points to a GitRepository
  • path indicates the path in the repo
  • prune: true to clean up orphans
  • dependsOn for deployment order
  • Variables with postBuild.substitute

← Previous chapter | Next chapter: HelmRelease β†’