Directory Structure Overview
Assume you have a directory named “K8s” containing two subdirectories: “API” and “database”.-
In the API directory, you will find:
- API deployment YAML file (e.g.,
api-depl.yaml) - API service YAML file (e.g.,
api-service.yaml) kustomization.yaml
- API deployment YAML file (e.g.,
-
In the database directory, you will find:
- Database deployment YAML file (e.g.,
db-depl.yaml) - Database service YAML file (e.g.,
db-service.yaml) - Config YAML file (e.g.,
db-config.yaml) kustomization.yaml
- Database deployment YAML file (e.g.,
kustomization.yaml is configured to import and manage all resources within that directory. For instance, the API directory’s kustomization.yaml might look like this:
kustomization.yaml imports the database configurations:
kustomization.yaml aggregates the API and database configurations:
Applying a Common Label
To add a common label to all resources, modify the rootkustomization.yaml file. For example, to add the label department: engineering to every resource:
- Open the root
kustomization.yaml. - Add the common label configuration.
- Save the file and run the Kustomize build command.
department: engineering. For example, a ConfigMap might appear as follows:
department: engineering label.
Label Application in Subdirectories
When you add a common label within a subdirectory’skustomization.yaml rather than in the root file, the label will only affect resources within that subdirectory. For example, if you apply a label in the API directory like so:
feature: api label is applied only to the resources listed in that file. This means that the API service will have both the root label (department: engineering) and the subdirectory-specific label (feature: api), while resources in the database directory will only have the global label.
A snippet of the resulting output could look like:
kustomization.yaml. For example:
Setting a Specific Namespace
To place all resources into a specific namespace, such as a debugging environment, add thenamespace field to your root kustomization.yaml. For example:
debugging.
Adding Name Prefixes and Suffixes
If you want every object name to have a common prefix (e.g., “KodeKloud-”) with folder-specific suffixes (like-web for API resources and -storage for database resources), follow these steps:
Adding a Global Name Prefix
Edit the rootkustomization.yaml to include the prefix:
KodeKloud-api-deployment (with an additional suffix appended later).
Adding Folder-Specific Name Suffixes
For the API subdirectory, add a name suffix in itskustomization.yaml:
-web while database resource names end with -storage, all prefixed by “KodeKloud-”. For example, the API service might be named:
Adding a Common Annotation
To apply a common annotation across every resource, add the following to your rootkustomization.yaml:
logging: verbose.
Image Transformation
Finally, let’s look at how to update container images using the image transformer. Consider a scenario where a database deployment is set to use the Mongo image, but you want to replace it with Postgres using a specific tag. In the database directory’skustomization.yaml (or in another appropriate configuration file), add an image transformer configuration like this:
Ensure you enclose the new tag in quotes to avoid type conversion issues (e.g.,
newTag: "4.2"). This prevents errors such as:Error: accumulating resources: … json: cannot unmarshal number into Go struct field Image.images.newTag of type stringWrapping Up
This guide demonstrated how to:- Organize Kubernetes configuration files into modular directories.
- Apply common labels, name prefixes, and suffixes globally or specifically in each subdirectory.
- Configure a namespace and common annotations.
- Use an image transformer to update container images seamlessly.