kubectl apply
and kubectl create
are both commands used to create and manage Kubernetes resources, but they serve slightly different purposes and are used in different scenarios. Understanding the differences between the two is important for effectively managing your Kubernetes clusters.
kubectl create
Purpose:
kubectl create
is primarily used to create new resources in a Kubernetes cluster from a given manifest file or command options.Behavior:
It creates the resource if it does not already exist.
If you attempt to create a resource that already exists, the command will fail with an error stating that the resource already exists.
kubectl create
does not track changes or apply updates to resources; it simply creates them.
Use Case:
Use
kubectl create
when you are certain that the resource does not already exist in the cluster, such as when setting up a new resource for the first time.
Example:
kubectl create -f deployment.yaml
This command will create a deployment from the deployment.yaml
file, but if the deployment already exists, it will return an error.
kubectl apply
Purpose:
kubectl apply
is used for declarative management of Kubernetes resources, meaning it applies the configuration in a manifest file to a resource, creating it if it doesn't exist, or updating it if it does.Behavior:
If the resource does not exist,
kubectl apply
will create it.If the resource already exists,
kubectl apply
will compare the current configuration of the resource with the desired state described in the manifest file and update the resource to match the desired state.kubectl apply
can be used repeatedly on the same manifest file to ensure that the resource configuration stays consistent with the desired state.It uses a "3-way merge" process, where it compares the last-applied-configuration with the current configuration and the new configuration to decide what needs to be updated.
Use Case:
Use
kubectl apply
for ongoing management and updates to resources, especially when you expect to make changes to the configuration over time.It's commonly used in GitOps workflows and infrastructure-as-code approaches, where you apply the same configuration file multiple times to maintain the desired state.
Example:
kubectl apply -f deployment.yaml
This command will create the deployment if it doesn't exist, or update it if it does exist, based on the contents of deployment.yaml
.
Key Differences
Creation vs. Update:
kubectl create
only creates new resources and will fail if the resource already exists.kubectl apply
will create the resource if it doesn't exist or update it if it does, making it more flexible for managing resources.
Declarative vs. Imperative:
kubectl apply
supports a declarative approach to resource management, where you define the desired state in a manifest file and useapply
to ensure the actual state matches.kubectl create
is more imperative, focusing on the action of creating resources based on the current state.
Use Cases:
Use
kubectl create
when you want to create resources that you know do not yet exist.Use
kubectl apply
when you want to manage resources over time, ensuring that changes in the manifest are reflected in the cluster.
Summary
kubectl create
is best for initial creation of resources or when you know the resource doesn’t already exist.kubectl apply
is best for managing resources over time, allowing you to create or update resources as needed to match a desired state defined in your manifests.
In most day-to-day operations, especially when using infrastructure-as-code practices, kubectl apply
is more commonly used because it allows for ongoing management and updates of Kubernetes resources.
Stackoverflow link: https://stackoverflow.com/questions/47369351/kubectl-apply-vs-kubectl-create