We have launched a free Basic Kubernetes Course. Click here to enrol now! Kubernetes is a container orchestration platform used by enterprises worldwide and Kubernetes developers are in high demand!
In the previous article we saw an introduction to the Kubernetes kustomize tool and how it can be used to configure YAML file used for deploying applications to Kubernetes. In this article we will look at how to use Kubernetes Kustomize for multiple environments. In a production setting , you might have multiple environments and each deployment would need separate configuration. For example, if you have a spring boot application and multiple environments such as dev, testing and production; you might want the same YAML file configured such that it deploys to separate namespaces i.e. use the same YAML to deploy to a development or a production namespace. Kustomize makes it easy to do that.
The idea is to create a base directory that has all the YAML files and then a directory per enviroment where you add environment specific information such as a namespace or labels. You can also overwrite specific parts of the YAML. For example, If you want to overwrite a port in a service, you create a YAML inside the environment directory with just the port information and then customize would merge the environment YAML with the base YAML.
Lets see an example of that. We will again deploy the nginx server, but now we will use kustomize to deploy the server in two different environments. We will see how to configure three things
- Configuring namespaces
- Configuring the targetPort for the service in development.
- Configure production to use only SSD by setting up NodeSelector.
Lets see how we can do all of that. Here’s a screenshot of the final folder. This project is hosted at
The base folder contains the common parts of the yaml files. Each folder contains a file called kustomization.yaml. This file is used by kustomize to create the final manifest. Lets look at the customization file from the dev directory
There are a few things going on in this file. The first line gives the base yaml used by the dev folder. Note that we are building the dev version, so it needs to explicitly specify the base. We will build this using
cd kubernetes_kustomize_multiple_environment kustomize build env/dev
Line 2 adds namespace: dev to all objects created by the command. Similarly namePrefix adds a prefix to all names. configMapGenerator at line 11 creates configMap. Note how the configuration files are now part of each environment. The most interesting section though is patchesJson6902. This allows use to change part of the base YAML file. In this case we want to change the targetPort. Here’s how the service_patch.yaml file looks
The file is of type JsonPatch. For more information see here-
https://tools.ietf.org/html/rfc6902 . The patch is applied to the nginx service and it replaces the value at spec/ports/0/targetPort.
Lets now look at the kustomization folder from the prod directory
The only difference in this file is line 4. patchesStrategicMerge is used to merge the production YAML file with the base file. Here’s how the production yaml file looks
This adds the nodeSelector to the base YAML file. This figure explains it
This finishes our tutorial on using Kustomize to create Kubernetes YAML for multiple environments.