Sphere
Log in Join
☆ 15

kubectl apply rejected my deployment with field is immutable

Can't change the selector on a Deployment, obviously, but terraform keeps trying. Error: The Deployment web is invalid: spec.selector: Invalid value: field is immutable. Somebody renamed a label in the manifest from app=web to app.kubernetes.io/name=web without changing the selector, and now apply fails. I can fix the manifest, but there are about twelve resources with the same pattern. Is deleting and recreating the Deployments the only way?

⚑ Report

4 replies

☆ 0
G ghost ·2026-09-28#1

You don't have to delete them if you can make the new selector match the existing pod labels. Set spec.selector back to the old value and change only the template labels if needed. But if you genuinely want a new selector, deletion is required because the ReplicaSet ownership is tied to it. kubectl replace --force does delete and recreate in one step.

⚑ Report
☆ 0
P plastic ·2026-10-07#2

The old selector is what's in the running pods, so setting it back works for apply. But terraform will then try to change it again on every plan because the source manifest has the new label. I need to decide which one is canonical. Does changing selector cause downtime if there are multiple replicas?

⚑ Report
☆ 0
G glitch ·2026-10-15#3

Yes, a selector change means a new ReplicaSet and a rollout. With a rolling update strategy and proper readiness probes, there shouldn't be downtime, but the pods get recreated. If the deployment has a PDB of minAvailable 1 and three replicas, you're fine. Without a PDB, a node drain during the rollout could take everything down.

⚑ Report
☆ 0
C cactus52 ·2026-10-18#4

What we did: kept the old selector untouched, added the new label alongside it in both selector and template, then in a later release flipped the selector once during a maintenance window. Two deploys, zero drama. Changing selectors is one of those things that always looks harmless until a rollout goes wrong.

⚑ Report
Log in to reply.