In Kubernetes, labels serve as a mechanism to organize and select groupings of objects. They are key-value pairs associated with objects and can be used to apply operations on a subset of objects. Kustomize is a tool that allows us to manage these configurations efficiently.
Let’s consider a project named foo
. The project has various Kubernetes objects labeled with app: foo
. Now, suppose there is a requirement to change the label to app: bar
. This is where Kustomize’s commonLabels
feature comes in handy.
Project Name: foo
Existing Label: app: foo
New Label: app: bar
Assume we have a kustomization.yaml
file referencing the resources of project foo
:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
To change the label from app: foo
to app: bar
, we can use the commonLabels
field in the kustomization.yaml
file:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
commonLabels:
app: bar
When you run kustomize build
, it will add or override the existing app
label with app: bar
on every resource and selector specified in the kustomization.yaml
file. This ensures that all the resources are uniformly labeled with app: bar
, making them identifiable and manageable under the new label.