来自服务器的错误(冲突):无法完成对 pod "my-app-5d4f8b7c6-abcde" 的操作:对象已被修改;请将更改应用于最新版本并重试
Error from server (Conflict): Operation cannot be fulfilled on pods "my-app-5d4f8b7c6-abcde": the object has been modified; please apply your changes to the latest version and try again
ID: cicd/kubectl-apply-pending
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Kubernetes 1.28 | active | — | — | — |
| Kubernetes 1.29 | active | — | — | — |
| kubectl 1.29.0 | active | — | — | — |
根因分析
当两个或多个控制器或 CI 作业尝试同时修改同一 Kubernetes 资源时,会发生资源更新冲突,导致第二个更新因 resourceVersion 过期而失败。
English
A Kubernetes resource update conflict occurs when two or more controllers or CI jobs try to modify the same resource concurrently, causing the second update to fail due to stale resourceVersion.
官方文档
https://kubernetes.io/docs/reference/using-api/api-concepts/#conflicts解决方案
-
在短暂延迟(例如 10 秒)后重新运行 CI 作业,以允许冲突操作完成并更新 resourceVersion。
-
使用 `kubectl apply --server-side=true`,它使用服务器端应用来合并更改而不会出现冲突错误。
-
在 CI 脚本中添加重试循环:`for i in 1 2 3; do kubectl apply -f deployment.yaml && break || sleep 5; done`
无效尝试
常见但无效的做法:
-
60% 失败
Deleting and recreating the resource manually; this causes downtime and may trigger cascading failures in dependent services.
-
70% 失败
Using `kubectl replace --force` which deletes and recreates the resource; this bypasses conflict detection but can delete other dependent objects.
-
90% 失败
Ignoring the error and retrying the same CI job without changes; the conflict persists because the resourceVersion hasn't been updated.