Kustomize — use patches to add or override resources
In my 7 years of experience with Kubernetes, I’ve worked with both Helm and Kustomize.
As a DevOps consultant — hit me up here if you need help — one of my duty is to give my customer the solutions I think are the best
The fact that Helm uses templates and you have to make any configuration while writing the helm chart always bored me!
Yeah ok it’s cool from some of you, but I don’t care, I don’t like it anyway.
I prefer to have an application that manages overlay instead. I don’t say that is the better tool on the market I like to use it.
That being said, let’s see how to override some values using patches.
Let’s see how to use it
Ok, let’s see how to use this system to patch a Kubernetes object.
Let’s assume we respected the project structure suggested by Kustomize team
|-- base
ingress.yaml
deployment.yaml
kustomization.yaml
|-- overlays
|-- prod
ingress-patch.yaml
kustomization.yaml
|-- dev
ingress-patch.yaml
kustomization.yaml
Now, we want to change the ingress hostname for dev adding the prefix dev- in front of the hostname having this ingress object
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
spec:
ingressClassName: alb
rules:
- host: app.devopscons.com #it will be dev-app.devopscons.com
http:
paths:
- backend:
service:
name: web
port:
number: 80
pathType: ImplementationSpecific
- Create a new patch file like this one named ingress-patch.yaml
- op: replace #action
path: /spec/rules/0/host #resouirce we want to change
value: dev-app.devopscons.com #value we want to use for patching
2. add the patch section to the kustomization.yaml
patches:
- target:
group: networking.k8s.io
version: v1
kind: Ingress
name: ingress
path: ingress-patch.yaml
That's it!
If I want to add resources 🤔 ?
Well, glad you asked.
You only need to create the patch file with the add action
- create the patch file with add action
- op: add #action
path: "/spec/template/spec/nodeSelector" #resouirce we want to change
value: #value we want to use for patching
env: prod
type: spot
version: "3"
2. the kustomization.yaml file will be the same as the patch section
So this is the end. Hope this helped you.
See you next time! 👋