Kustomize is a tool that lets you create an entire Kubernetes application out of individual pieces — without touching the YAML for the individual components.
Kustomize traverses a Kubernetes manifest to add, remove or update configuration options without forking. It is available both as a standalone binary and as a native feature of kubectl.
Kustomize enables you to do that by creating a file that ties everything together, or optionally includes “overrides” for individual parameters.
In this example, I setup a Wordpress Kubernetes cluster using Kustomize. The source code is available here.
Start your cluster:
brew install kustomize kustomize version
To run WordPress, it's necessary to connect WordPress with a MySQL database and access the service name of MySQL database from WordPress container.
Set the base directory:
WORDPRESS_HOME=$BASE/wordpress cd $WORDPRESS_HOME kubectl apply -f $WORDPRESS_HOME
MYSQL_HOME=$BASE/mysql cd $MYSQL_HOME kubectl apply -f $MYSQL_HOME
In each of these two directories, you should see three resources YAML files, and they are the elements of each individual deployment:
In the next step, we will use Kustomize to deploy everything by combining these files into a single file. But before we move on, let's clean up the individual deployments:
kubectl delete -f $WORDPRESS_HOME kubectl delete -f $MYSQL_HOME
The way Kustomize connects these two directories is by adding the following in their
commonLabels: app: my-wordpress
Now we can use Kustomize to build the new YAML. The output from Kustomize is the concatenation of YAML documents for all the resources we specified, with the common labels added, which can be piped directly to
kustomize build $BASE | kubectl apply -f -
Now, you can check that the services are up (you should see
kubectl get services
Overlays enable us to take the base YAML and selectively change pieces of it.
In this example, we’re going to create an overlay that includes a patch to change the Services to NodePort type services.
The overlay should not be in the same directory as the base files, and in this case, we are using
From your root directory, run:
OVERLAY_HOME=($pwd)/overlays DEV_HOME=$OVERLAY_HOME/dev cd $DEV_HOME
Now you can apply the overlay changes with:
kubectl apply -k $DEV_HOME
kubectl delete -f $BASE minikube stop