7

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?

CSUNNY
  • 414
  • 1
  • 7
  • 23

1 Answers1

12

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.

Highway of Life
  • 22,803
  • 16
  • 52
  • 80
  • Thanks. I guess this explains. I just wanted to confirm. I am thinking of using yq or code to create the templates. Maybe that will; be a better option for this – CSUNNY May 20 '21 at 18:25
  • I strongly recommend against using Helm with Kustomize as it is a feature only developed to ease transition from Helm to Kustomize and it will greatly increase the complexity of your configurations as well as the knowledge required to maintain them. – 13013SwagR Apr 06 '23 at 16:56
  • 1
    @13013SwagR this answer was not to use helm with kustomize. It is a much more in-depth answer that provides lots of different options, and helm with kustomize as a last resort. And that configuration program does work depending on how your enterprise manages your kubernetes infrastructure. So it is still a valid addition to the overall answer. – Highway of Life Apr 08 '23 at 02:49