I am evaluating Kustomize as a templating solution for my Project. I want an option to replace specific key-value pairs.
ports:
- containerPort: 8081
resources:
limits:
cpu: $CPU_LIMIT
memory: $MEMORY_LIMIT
requests:
cpu: $CPU_REQUESTS
memory: $MEMORY_REQUESTS
In the above example, I want to replace CPU_LIMIT with a config-driven value. What options do I have to do this with Kustomize?
Kustomize doesn't do direct variable replacement like a templating engine. But there are some solutions depending on what attributes you need to variabalize.
Usually variables in deployments, statefulsets, daemonset, pod, job, etc, attributes allow you to use variables powered by a configmap, so you don't necessarily have to use a variable at compile time. However, this doesn't work when controlling values like resource limits and requests, as those would be processed before configmaps would be mounted.
Kustomize isn't designed to be a templating engine, it's designed as a purely declarative approach to configuration management, this includes the ability to use patches for overlays (overrides) and reference resources to allow you to DRY (Do-Not Repeat Yourself) which is especially useful when your configuration powers multiple Kubernetes clusters.
For Kustomize, maybe consider if patching might meet your needs. There are several different ways that Kustomize can patch a file. If you need to change individual attributes, you can use the patchesJSON6902, although when you have to change a lot of values in a deployment, changing them one at a time this way is cumbersome, instead use something like patchesStrategicMerge
Consider the following way to use a patch (overlay):
.
├── base
│ └── main
│ ├── kustomization.yaml
│ └── resource.yaml
└── cluster
├── kustomization.yaml
└── pod_overlay.yaml
Contents of base/main/resource.yaml
:
---
apiVersion: v1
kind: Pod
metadata:
name: site
labels:
app: web
spec:
containers:
- name: front-end
image: nginx
ports:
- containerPort: 8081
resources:
requests:
cpu: 100m
memory: 4Gi
limits:
cpu: 200m
memory: 8Gi
Contents of cluster/pod_overlay.yaml
:
---
apiVersion: v1
kind: Pod
metadata:
name: site
spec:
containers:
- name: front-end
resources:
requests:
cpu: 200m
memory: 8Gi
limits:
cpu: 400m
memory: 16Gi
Note that we only included the selectors (kind
, metadata.name
, spec.containers[0].name
) and the values we wanted to replace, in this case the resource requests and limits. You don't have to duplicate the entire resource for the patch to apply.
Now to apply the patch with kustomize, the contents of cluster/kustomization.yaml
:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base/main
patchesStrategicMerge:
- pod_overlay.yaml
Another option to consider if you really need templating power is to use Helm.
Helm is a much more robust templating engine that you may want to consider, and you can use a combination of Helm for templating and the Kustomize for resource management, patches for specific configuration, and overlays.