Kubernetes API call equivalent to 'kubectl apply'
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The API equivalent of kubectl apply is not a plain POST or PUT. If you want the same declarative behavior, the closest direct API call is a PATCH request using server-side apply. That tells the API server to create or update the object while also tracking field ownership.
What kubectl apply is trying to do
kubectl apply means "make the live object look like this manifest." That is different from imperative commands that say "set replicas to 3" or "update this one field."
Historically, kubectl apply had a client-side mode that calculated diffs locally and stored the last-applied configuration in an annotation. The modern API-native form is server-side apply, where the API server performs the merge and conflict detection.
So if you are calling the API directly, think in terms of:
- HTTP method:
PATCH - patch type: apply patch
- field manager: identify the actor making the change
The direct HTTP request
For a deployment named demo in the default namespace, a server-side apply request looks like this:
The request body is the manifest you want applied, for example:
If the deployment does not exist, the API server can create it. If it does exist, the API server merges the manifest according to apply semantics.
Why this is different from other patch types
Kubernetes supports several patch styles, including JSON Patch and Merge Patch. Those are useful, but they are not the same as apply.
Server-side apply adds two important behaviors:
- it tracks which actor owns which fields
- it detects conflicts when another actor owns a field you are trying to change
That ownership model is why fieldManager matters. It gives the API server a stable name for the caller so later applies can be merged or rejected consistently.
The optional force=true query parameter tells the API server to take ownership of conflicting fields. Use it carefully, because it can overwrite values set by another controller or operator.
The client-go version
If you are writing Go rather than shelling out to curl, the same operation uses types.ApplyPatchType:
That is the programmatic equivalent most people want when they ask for the API call behind kubectl apply.
When a plain create or update is enough
If you fully control the object and do not need declarative merge behavior, a normal POST, PUT, or non-apply PATCH may be simpler. But that is not the same contract as apply, especially when multiple actors manage the same resource.
Use apply when you want Kubernetes to reconcile a declarative desired state and track field ownership. Use imperative updates when you are intentionally changing a specific field in a specific object.
Common Pitfalls
The most common mistake is using Content-Type: application/merge-patch+json and expecting kubectl apply behavior. That is a different patch strategy.
Another mistake is omitting fieldManager. Server-side apply depends on field ownership metadata, so the manager name should be stable and explicit.
People also misuse force=true. It resolves conflicts by taking ownership, which is sometimes correct but can also stomp fields owned by another controller.
Finally, do not forget that the applied manifest still needs required fields such as metadata.name and, for namespaced resources, the correct namespace.
Summary
- The closest API equivalent to
kubectl applyis aPATCHrequest using server-side apply. - Use
Content-Type: application/apply-patch+yamlor the JSON apply variant. - Set a stable
fieldManagerso Kubernetes can track field ownership. - Use
force=trueonly when you intentionally want to take over conflicting fields. - Plain create and update calls are useful, but they are not the same as declarative apply semantics.
Related reading
- kubernetes API object created by a deployement creation
- Kubernetes API server , serving pod logs
- Kubernetes API Server Unable to listen for secure
- Kubernetes autoscaler - NotTriggerScaleUp' pod didn't trigger scale-up it wouldn't fit if a new node is added
- Kubernetes cannot pull image from private docker image repository
- kubernetes cannot pull local image
- Kubernetes AWS NLB Services Create Excessive Amount Of Security Group Rules
- Kubernetes calico node CrashLoopBackOff

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.