Sobes.tech
Junior — Middle

What is the difference between 'kubectl apply' and 'kubectl create' when managing resources in Kubernetes?

sobes.tech AI

Answer from AI

The kubectl create and kubectl apply commands are used for managing resources in Kubernetes but have different use cases:

  • kubectl create — creates a new resource from a manifest. If a resource with the same name already exists, the command will fail.

  • kubectl apply — applies changes to an existing resource or creates it if it does not exist. It uses a declarative management mechanism, comparing current and desired states.

Example:

kubectl create -f pod.yaml  # creates a pod if it does not exist
kubectl apply -f pod.yaml   # creates or updates the pod considering changes

Thus, apply is convenient for managing configurations in GitOps mode and during frequent changes, while create is for one-time resource creation.