Sobes.tech
Junior — Middle

How to work with Kubernetes resources using the apply command and what happens in the system?

sobes.tech AI

Answer from AI

The kubectl apply command is used to create and update Kubernetes resources based on descriptions in YAML or JSON files. When executing kubectl apply -f <file>:

  1. The client reads the resource manifest from the file.
  2. It sends a request to the Kubernetes API server with the resource description.
  3. The API server compares the current state of the resource in the cluster with the desired state from the manifest.
  4. If the resource does not exist, it is created.
  5. If the resource exists, the API server applies changes, updating only the fields that have changed (patching).

Thus, apply implements a declarative approach: you describe the desired state, and Kubernetes brings the cluster to that state. This is convenient for managing infrastructure as code and makes it easy to track changes.

Example usage:

kubectl apply -f deployment.yaml

where deployment.yaml contains the description of a Deployment, Service, or other resource.

How to work with Kubernetes resources using the apply… - sobes.tech