When managing Kubernetes deployments across multiple environments, such as development, staging, and production, keeping track of different configurations can become challenging. This is where Kustomize steps in to make your life easier. Unlike traditional methods that require you to modify original YAML files directly, Kustomize allows you to customize Kubernetes resource definitions without altering the base YAML files. It layers configurations on top of base resources, enabling you to manage multiple environments with a single set of YAML files.
Let's explore some of the essential Kustomize commands that you can use in your Kubernetes workflows:
Generate a Kustomization File
kustomize create --autodetect
This command generates a kustomization.yaml file by automatically detecting the Kubernetes resource files in the current directory. This file will define the resources, patches, and other customizations that Kustomize will manage.
Build Kubernetes Manifests
kustomize build .
This command builds the final Kubernetes manifests by applying the customizations defined in the kustomization.yaml file to the base resources. The output is a fully rendered set of YAML files that can be applied to your Kubernetes cluster.
Apply the Built Manifests to a Kubernetes Cluster
kubectl apply -k .
This command applies the customized resources directly to your Kubernetes cluster. The -k flag tells kubectl to use Kustomize to build the manifests before applying them.
Set a New Image in Your Kustomization
kustomize edit set image image_name=${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPOSITORY:$GITHUB_SHA
This command updates the kustomization.yaml file to set a new image for your Kubernetes deployment. The image name is set dynamically using environment variables or secrets provided in a CI/CD pipeline (e.g., GitHub Actions). Here, image_name is replaced with the Docker image hosted in AWS ECR, tagged with the commit SHA ($GITHUB_SHA) for traceability.
Conclusion
Kustomize is a powerful tool that brings flexibility and simplicity to managing Kubernetes configurations. By using Kustomize, you can efficiently handle multiple environments, reduce configuration complexity, and ensure that your Kubernetes resources are always in sync with your deployment needs.
š If you enjoy these blog posts, please check out my three books on AWS, DevOps, and Machine Learning.
https://pratimuniyal.gumroad.com/l/BuildinganLLMOpsPipelineUsingHuggingFace
https://pratimuniyal.gumroad.com/l/cracking-the-devops-interview
https://www.amazon.com/AWS-System-Administrators-automate-infrastructure/dp/1800201532