GitOps with Flux on Kubernetes Part 2: Advanced Kustomization and the Kustomize Controller
Deep dive into Flux's Kustomize Controller, advanced kustomization techniques, multi-environment management, and configuration overlays for GitOps workflows
GitOps with Flux on Kubernetes Part 2: Advanced Kustomization and the Kustomize Controller
Introduction
In part 1 of this series, we covered the basics of installing Flux and setting up your first GitOps workflow. Now, we’ll dive deeper into one of Flux’s most powerful components: the Kustomize Controller and advanced kustomization techniques.
Kustomization is a powerful tool for managing Kubernetes configurations across multiple environments without duplicating YAML files. When combined with Flux’s GitOps capabilities, it provides a robust solution for managing complex, multi-environment deployments declaratively.
Understanding the Flux Kustomize Controller
The Kustomize Controller is one of the core components in Flux v2. It watches for Kustomization resources and applies the Kubernetes manifests from Git repositories, Helm repositories, or Bucket sources.
Key Features of the Kustomize Controller
Declarative Configuration: Define your desired state in Git using Kustomization resources
Dependency Management: Control the order of resource deployment and updates
Health Assessment: Monitor the health of deployed resources
Pruning: Automatically remove resources that are no longer defined in Git
Variable Substitution: Inject values dynamically into your manifests
Multi-tenancy: Isolate deployments across different namespaces and teams
Let’s create a practical example that demonstrates advanced kustomization techniques. We’ll set up a repository structure that supports multiple environments (dev, staging, production) for a sample application. The sample repository is available at TalhaJuikar/flux-demo-website
Here’s a quick demonstration of the deployed website across different environments:
Now let’s create overlays for different environments, each with their specific configurations. You can customize replicas, resource limits, environment variables, and more. In dev I have split the replicas and resource limits into separate patches for clarity. In staging and production, I have combined them into a single patch for simplicity. This demonstrates how you can structure your overlays based on your team’s preferences and the complexity of your configurations.
Since the repository where the kustomization files are stored is the same as the one where the Flux configuration is stored, you can skip creating a separate GitRepository resource. However, if you want to keep your Flux configuration and application manifests in separate repositories, you would need to create a GitRepository resource as shown below. Then push the GitRepository resource to the git repository that Flux is monitoring.
In my case, I have a single cluster with different namespaces for each environment, so there will be only one apps.yaml file in the clusters/flux-demo directory.
Verifying Deployment
Wait for Flux to reconcile the kustomizations. Once flux has reconciled the kustomizations, you can verify that the resources have been created in the respective namespaces:
1
2
3
4
5
# Check the status of git repositories
flux get source git -A# Check the status of kustomizations
flux get kustomizations -n flux-system
You should see the kustomizations for each environment listed with their respective statuses.
If everything is set up correctly, you should see the resources created in the respective namespaces.
1
2
3
4
5
6
# Check resources in the dev namespace
kubectl get all -n dev
# Check resources in the staging namespace
kubectl get all -n staging
# Check resources in the prod namespace
kubectl get all -n prod
You can now access your application in each environment using the respective hostnames configured in the Ingress resources.
Advanced Kustomization Features
Variable Substitution
Flux supports variable substitution in your manifests using the postBuild.substitute field:
1
2
3
4
5
6
7
8
9
10
11
12
# In your Kustomizationspec:postBuild:substitute:cluster_name:"my-cluster"app_version:"v1.2.3"replica_count:"3"substituteFrom:-kind:ConfigMapname:cluster-vars-kind:Secretname:cluster-secrets
In complex deployments, you may need to control the order in which resources are applied. The Kustomization controller allows you to specify dependencies using the dependsOn field.
You can define dependencies between kustomizations to ensure that certain resources are applied before others. This is particularly useful for managing infrastructure components like databases, message queues, or ingress controllers that must be ready before your application is deployed.
1
2
3
4
5
6
7
8
9
10
apiVersion:kustomize.toolkit.fluxcd.io/v1kind:Kustomizationmetadata:name:my-appspec:dependsOn:-name:infrastructure-name:cert-manager-name:ingress-controller# ... rest of spec
Health Checks and Wait Conditions
Configure health checks to ensure resources are ready before proceeding:
# List all kustomizations
flux get kustomizations
# Get detailed status
kubectl describe kustomization flux-demo-website-prod -n flux-system
# Check events
flux events
# View logs
flux logs --kind=Kustomization --name=flux-demo-website-prod
Common Issues and Solutions
Reconciliation Failures: Check source repository access and path correctness
Dependency Issues: Ensure dependent resources are healthy before deploying
Resource Conflicts: Use proper namespacing and unique naming
Timeout Issues: Adjust timeout values based on resource complexity
Conclusion
The Flux Kustomize Controller provides a powerful foundation for managing complex, multi-environment Kubernetes deployments. By combining Kustomize’s configuration management capabilities with Flux’s GitOps automation, you can create robust, scalable deployment pipelines that maintain consistency across environments while allowing for necessary customizations.
In the next part of this series, we’ll explore Image Automation with Flux, which will enable you to automate the deployment of container images and manage image updates seamlessly.
Have you implemented multi-environment deployments with Flux and Kustomize? Share your experiences and challenges in the comments below!