Reputation: 2946
Trying to figure out, how to apply new label "app=green" to the pods that are currently marked with "color=green"...
Looks like I could not use "--field-selector" and I do not want to specify name of each pod in "kubectl label" command.
Upvotes: 2
Views: 2127
Reputation: 149
To apply a new label to Pods that are currently labeled with "color=green" without specifying the names of each Pod individually, you can use the kubectl
command in combination with label selectors. Here's how you can do it:
kubectl get pods
command with the --selector
option to list the Pods that match this label selector:kubectl get pods --selector=color=green
This command will list all Pods with the "color=green" label.
"app=green"
label to them. You can pipe the list of Pods from the previous step to the kubectl label command like this:kubectl get pods --selector=color=green --no-headers | awk '{print $1}' | xargs -I {} kubectl label pod {} app=green
Here's what this command does:
kubectl get pods --selector=color=green --no-headers
lists the Pods with the "color=green" label without headers.awk '{print $1}'
extracts the Pod names from the list.xargs -I {} kubectl label pod {} app=green
applies the "app=green" label to each Pod.
This command will apply the new label "app=green" to all Pods that match the label selector "color=green."Please be cautious when applying labels to Pods, especially in a production environment, to avoid unintended consequences.
Upvotes: 0
Reputation: 51652
This should work:
kubectl label pods --selector=color=green app=green
Upvotes: 7