31.7 Using Kustomize with kubectl apply -k

Right, so you’ve got your kustomization.yaml file looking sharp, and you’re ready to deploy. You could run kustomize build ./your-dir/ | kubectl apply -f -, piping the rendered YAML to kubectl. It works, but it feels a bit like using a Rube Goldberg machine to butter your toast. There’s a better way, and it’s built right into kubectl itself: kubectl apply -k. This flag is kubectl’s native hook into Kustomize. When you run apply -k, kubectl calls the Kustomize library internally to build the configuration from the specified directory and then immediately applies it. It’s a single, clean, atomic command for your deployment workflow. The -k flag is your signal that you’re not just applying a file, you’re applying a customization.

31.6 Kustomize vs Helm: When to Use Each

Alright, let’s settle this. The “Kustomize vs. Helm” debate is the tech equivalent of “Vim vs. Emacs” or “Tabs vs. Spaces”—it generates more heat than light because people treat it like a religious war. It’s not. They are different tools designed for different jobs, and the only wrong choice is using one when you should have used the other. Let’s break it down so you can stop arguing and start deploying.

31.5 Component Reuse with Kustomize Components

Right, so you’ve got your kustomization.yaml files. They’re great. You’ve mastered patching a service’s name here, adding a common label there. But then you look across your repository and see the same three strategic merge patches, the same configMap generator, the same annoying tweaks copy-pasted across a dozen slightly different environments. Your DRY (Don’t Repeat Yourself) senses are tingling. This is where Kustomize Components come in, and frankly, they’re the feature that transforms Kustomize from a neat trick into a genuinely powerful architecture tool. Think of them as reusable, composable blocks of configuration. You define the common stuff once and then just mix it into your various overlays.

31.4 ConfigMapGenerator and SecretGenerator

Right, let’s talk about the two workhorses that will save you from a life of endless copy-pasting: ConfigMapGenerator and SecretGenerator. Forget manually crafting individual ConfigMaps and Secrets for every environment; that way lies madness and a carpal tunnel diagnosis. These generators are Kustomize’s way of saying, “Give me your raw data, some rules, and I’ll handle the boilerplate for you.” It’s configuration management, not configuration data entry. The core idea is beautifully simple. You define a generator in your kustomization.yaml that points to a source of data—be it a file, a literal value, or even an entire .env file. When you run kustomize build (or kubectl apply -k), Kustomize reads that source, creates the appropriate Kubernetes resource manifest (ConfigMap or Secret), and injects it into your final output. The magic is that it also automatically appends a content hash to the name, which is the key to forcing rolling updates when the data changes. We’ll get to that glorious trick in a second.

31.3 Strategic Merge Patches and JSON Patches

Right, so you’ve got your pristine, beautifully structured YAML files. And now someone—maybe even future you—wants to make a “tiny” change. You could just copy-paste the whole manifest and tweak one field, but congratulations, you’ve just invented a maintenance nightmare. You now have two sources of truth, and they will inevitably drift. This is why we have patches. Kustomize gives you two primary ways to do this: Strategic Merge Patches (SMPs) and JSON Patches. They are fundamentally different tools for different jobs, and using the wrong one will make you question your life choices. Let’s fix that.

31.2 kustomization.yaml: resources, patches, images, namePrefix

Alright, let’s get our hands dirty with the kustomization.yaml file. This is your single source of truth for a Kustomize overlay, the control panel from which you direct the entire symphony of YAML mangling. Forget templates; we’re layering and patching like a digital archaeologist, carefully brushing away the generic to reveal the environment-specific. The file is essentially a manifest of manifests. It tells Kustomize: “Here are the raw materials, here’s how I want you to change them, now go build me something I can actually kubectl apply.”

31.1 Kustomize Concepts: Base, Overlays, and Patches

Right, let’s get our hands dirty with the core of Kustomize: Bases, Overlays, and Patches. Forget everything you’ve heard about templating engines with their curly braces and logic-less nonsense. Kustomize’s approach is so brilliantly simple it feels like cheating. You’re not generating YAML from templates; you’re customizing existing, valid YAML. It’s the difference between baking a cake from a recipe and expertly decorating one that’s already baked. The latter is faster, less error-prone, and you get to taste-test immediately.

— joke —

...