Blog

Server-side apply, field managers and conflicts, explained

How server-side apply differs from kubectl apply, what managedFields and field managers are, why apply conflicts happen, and how to resolve them safely.

Sooner or later, kubectl apply --server-side greets you with something like this:

error: Apply failed with 1 conflict: conflict with "kube-controller-manager" using apps/v1: .spec.replicas
Please review the fields above--they currently have other managers.

It looks like Kubernetes being difficult. It's actually Kubernetes saving you from a fight between two tools that both think they own the same field. Here's how it works.

Client-side apply, briefly

Classic kubectl apply works out changes on your machine. It compares three things: the manifest you're applying, the live object, and the copy of your last apply that kubectl stashed in the kubectl.kubernetes.io/last-applied-configuration annotation. From those it builds a patch.

That works when kubectl is the only thing writing to an object. It gets murky when an autoscaler, an operator, a GitOps controller and a human with kubectl edit all touch the same Deployment, because the annotation only remembers what kubectl last applied.

Server-side apply

With server-side apply, generally available since Kubernetes 1.22, the API server does the merge, and it tracks who set which field:

kubectl apply --server-side -f checkout.yaml

Every writer has a name, its field manager. kubectl's default is kubectl; controllers use their own names. The API server records which manager owns which fields in metadata.managedFields. It's hidden from kubectl get output by default, so ask for it:

kubectl get deploy checkout -o yaml --show-managed-fields
managedFields:
  - manager: kubectl
    operation: Apply
    apiVersion: apps/v1
    fieldsV1:
      f:spec:
        f:template: { ... }
  - manager: kube-controller-manager
    operation: Update
    fieldsV1:
      f:spec:
        f:replicas: {}

operation: Apply means the manager used server-side apply; Update means an ordinary write such as a patch or kubectl edit.

Where conflicts come from

When you apply, you declare the fields you care about. If one of them is owned by another manager and you're setting it to a different value, the API server refuses with a conflict instead of silently overwriting the other writer. That's the message at the top of this post.

If you set a field to the value it already has, there's no conflict: you become a co-owner.

The most common conflict is the one above. A manifest says replicas: 3, an autoscaler has set it to 7, and now your apply would quietly undo the autoscaler. Before server-side apply, that's exactly what happened, every deploy.

Resolving conflicts

You have three honest options:

  1. Stop managing the field. If an HPA owns replica counts, take replicas out of your manifest. You no longer claim it, so there's nothing to conflict with. This is the right answer surprisingly often.

  2. Take ownership on purpose. --force-conflicts makes you the owner and sets your value:

    kubectl apply --server-side --force-conflicts -f checkout.yaml
    

    Use it when the other writer is wrong, or gone, for example a one-off kubectl edit someone made during an incident.

  3. Let the other tool own it. If a GitOps controller manages the object, change the value in Git and let it apply.

What you shouldn't do is reach for --force-conflicts by reflex. A conflict is the API server telling you two tools disagree; forcing it means the other tool will usually change it back, and you'll have the same argument on the next sync.

Preview first

Server-side apply also has a server-side dry run: the API server runs admission, defaulting and validation, and returns the object it would store, without storing it:

kubectl apply --server-side --dry-run=server -f checkout.yaml -o yaml
kubectl diff --server-side -f checkout.yaml

kubectl diff shows the difference between the live object and that dry-run result, which catches defaults, webhooks and conflicts that a local diff can't see.

A few practical notes

  • Pick a stable field manager name for automation (--field-manager=ci-deploy) so managedFields tells you which pipeline changed what.
  • Older objects often have fields owned by kubectl-client-side-apply or kubectl-edit. Recent kubectl versions hand client-side apply's fields over to server-side apply for you, but fields someone changed with kubectl edit can still conflict the first time.
  • managedFields is also a free audit hint: when something changed unexpectedly, the manager name and timestamp say which tool did it.

How Klystiq uses it

Every YAML edit in Klystiq is applied with server-side apply as field manager klystiq, after a server-side dry run that shows you the diff. A field owned by another tool shows up as a conflict you can review and, if you mean it, force. Each resource's History tab also lists its field owners, so "who changed this?" has an answer. See Working with resources.

Try it on your own clusters.

Download Klystiq, point it at the kubeconfig you already have, and see all of your clusters in one window. Your kubeconfig has been through enough; Klystiq only reads it. Free for personal use; a license for work.