Adding Items to a List with kubectl kustomize
The Problem:
You're working with Kubernetes YAML files, and you need to add items to a list within a specific resource. You want to use kubectl kustomize
to manage these changes, but you're unsure how to modify lists without overwriting existing values.
Understanding the Scenario:
Imagine you have a Kubernetes Deployment resource with a list of containers
. You want to add a new container to the existing list without modifying the existing containers. Here's an example of a deployment.yaml
file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
You want to add a second container for a sidecar like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
- name: sidecar
image: busybox:1.31
command: ["sleep", "infinity"]
Using kubectl kustomize
to Add List Items
kubectl kustomize
provides a powerful way to manage these changes without modifying the original YAML file. Here's how to use it:
-
Create a
kustomization.yaml
file: This file will specify the changes you want to apply. Place it in the same directory as yourdeployment.yaml
. -
Use the
patchesStrategicMerge
option: This allows you to strategically merge patches into your resource. In our case, we'll use it to append a new container to thecontainers
list.
Here's an example of a kustomization.yaml
file:
resources:
- deployment.yaml
patchesStrategicMerge:
- patch.yaml
- Create a
patch.yaml
file: This file will contain the patch you want to apply. In this case, it will add a new container to thecontainers
list.
Here's an example of a patch.yaml
file:
spec:
template:
spec:
containers:
- name: sidecar
image: busybox:1.31
command: ["sleep", "infinity"]
- Apply the changes: Use
kubectl kustomize
to apply the changes to your resource.
kubectl apply -k .
This will create a new Deployment with the added sidecar container.
Important Considerations:
- Strategic Merge:
patchesStrategicMerge
works by taking the existing values in your resource and merging them with the values in the patch file. This ensures that your existing configurations are not overwritten. - Order: The order of the patches in the
kustomization.yaml
file matters. Patches are applied in the order they are listed. - Deep Merging: You can target specific elements within your resource by providing a path to the element you want to modify. For example, you could use
spec.template.spec.containers[0]
to target the first container in the Deployment.
Additional Benefits:
- Version Control:
kubectl kustomize
integrates seamlessly with version control systems, making it easy to track changes and revert to previous versions. - Reusability: You can create multiple
kustomization.yaml
files and patch files, allowing you to easily manage different configurations and apply them to your resources.
Conclusion:
kubectl kustomize
offers a powerful and flexible way to manage Kubernetes resources and add items to lists without overwriting existing values. By utilizing strategic merge patches, you can ensure that your changes are applied in a controlled and predictable manner. This approach promotes code organization, maintainability, and collaboration within your Kubernetes development workflow.