Sub topic 14
How apply tracks changes: client-side and server-side apply
This chapter assumes you can write a Deployment manifest, run kubectl apply -f, and read basic kubectl get -o yaml output. It explains how apply decides what to change, and what happens when two actors edit the same object.
The problem apply has to solve
When you run kubectl apply on a file, the cluster already holds a live object. Three versions of that object matter:
- The manifest you are applying now.
- The live object in the cluster, which controllers and other tools may have changed.
- Something that records what you applied last time.
Without the third item, apply could not tell whether a field is missing from your file because you removed it, or because someone else added it. Removing it from the cluster would be right in the first case and wrong in the second.
Client-side apply
Client-side apply is the default for kubectl apply. It stores your last applied manifest in an annotation on the object, kubectl.kubernetes.io/last-applied-configuration. On the next apply, kubectl computes a three-way merge from the last-applied annotation, your new file, and the live object.
The rules are:
- A field in your new file is set to your value.
- A field in the last-applied annotation but absent from the new file is removed.
- A field in the live object that was never in your file is left alone.
You can see the annotation with this command:
kubectl get deployment web -n demo -o yaml | grep -A2 last-appliedThe merge runs in kubectl, on your machine. The API server only receives the resulting patch.
Server-side apply
Server-side apply moves the merge into the API server. Instead of an annotation, the server records field ownership in metadata.managedFields. Each entry names a manager (such as kubectl or a controller) and lists the fields that manager set.
You opt in with a flag:
kubectl apply -f deployment.yaml --server-sideIf you apply a value for a field that another manager owns, and the value differs, the server rejects the request with a conflict error. This protects you from silently overwriting another tool's work. To take ownership anyway, add --force-conflicts.
kubectl apply -f deployment.yaml --server-side --force-conflictsA common case is spec.replicas. If a HorizontalPodAutoscaler manages replicas and your manifest also sets replicas: 2, each apply fights the autoscaler. The fix is to remove replicas from the manifest so you stop owning that field.
Comparing the two
| Aspect | Client-side apply | Server-side apply |
|---|---|---|
| Where the merge runs | In kubectl | In the API server |
| Change record | `last-applied-configuration` annotation | `metadata.managedFields` |
| Conflicts | Not detected | Reported per field |
| Enable with | Default | `--server-side` |
Common errors
- Editing live objects with
kubectl editorkubectl scale, then re-applying. Fields you omit from the file stay at the live value, so the file no longer describes the cluster. - Mixing apply with
kubectl createon the same object.createwrites no last-applied annotation, so the firstapplyhas no record of earlier fields and cannot remove any. - Ignoring conflict errors by always using
--force-conflicts. You take fields away from other controllers, and they may take them back on the next reconcile.
Exercise
A manifest sets replicas: 3. An autoscaler later changes the live object to 6. You apply the same manifest with --server-side and get a conflict error. Why, and what is the cleanest long-term fix?
Show answer
The autoscaler owns spec.replicas in managedFields, and your manifest sets a different value, so the server reports a conflict. The cleanest fix is to remove replicas from the manifest so your manager no longer claims that field. --force-conflicts would work once, but the two would conflict again.