DEV Community

Cover image for What happens when you run kubectl apply?
Ashif Eqbal
Ashif Eqbal

Posted on

What happens when you run kubectl apply?

Kubernetes also known as K8s (Kates) was built by Google based on their experience running containers in production. It is now an open source project and is arguably one of the best and most popular container orchestration technologies.

One of the core component of Kubernetes cluster, infact part of master server is - The API server . It acts as the front end for kubernetes. We interact with kubernetes cluster via CLI/SDK/API from API server itself. For CLI we use what is known as - kubectl.

But have you ever imagined what happens when you run - kubectl apply ... command

When you run:

kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

youโ€™re telling Kubernetes:

๐Ÿ‘‰ โ€œMake the cluster match whatโ€™s defined in this file โ€” create or update as needed.โ€

But under the hood, quite a bit happens.


What Actually Happens

1. YAML is Parsed Locally

kubectl reads your manifest YAML file and converts it into an API request.

Example:

  • Kind: Deployment
  • Name: my-app
  • Spec: replicas, containers, etc.

2. Request Sent to API Server

The request goes to the control plane via the
Kubernetes API Server

๐Ÿ‘‰ This is the entry point of the cluster.


3. Authentication & Authorization

The API server checks:

  • Who are you? (Authentication)
  • Are you allowed? (Authorization)

4. Server-Side Apply Logic

This is the core magic of apply.

Kubernetes compares:

  • โœ… Desired state (your YAML)
  • โœ… Current state (in cluster)
  • โœ… Last applied config (stored in annotation)

It uses a 3-way merge:

  • Last applied
  • Current live state
  • New config

๐Ÿ‘‰ This avoids overwriting fields managed by others.


5. Object is Stored in etcd

Once validated, the object is saved in etcd

๐Ÿ‘‰ This is the source of truth for cluster state.


6. Controllers Detect Changes

Controllers inside Kubernetes Controller Manager start reacting.

Example for Deployment:

  • Sees desired replicas = 3
  • Creates/updates ReplicaSet

7. Scheduler Assigns Pods

If new Pods are needed, Kubernetes Scheduler decides which node should run them.


8. Kubelet Creates Containers

On the selected node:

  • Kubelet pulls image
  • Starts containers via container runtime

9. Continuous Reconciliation

Kubernetes keeps checking:

๐Ÿ‘‰ โ€œIs actual state == desired state?โ€

If not:

  • It fixes it automatically

Key Concept: Declarative Model

kubectl apply is declarative, not imperative.

You donโ€™t say: โŒ โ€œCreate 3 podsโ€

You say: โœ… โ€œI want 3 replicasโ€

Kubernetes ensures it stays that way.


Apply vs Create vs Replace

Command Behavior
apply Smart merge (recommended)
create Fails if resource exists
replace Overwrites completely

What Makes apply Powerful

  • Idempotent (run multiple times safely)
  • Works great with GitOps
  • Tracks last applied config
  • Supports partial updates

Thank You for reading my post.

You can find more about me: ๐Ÿ‘‰ Github Profile

Top comments (0)