kubectl apply vs kubectl create?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Kubectl is a command-line interface used to interact and manage Kubernetes clusters. Among the numerous commands available in kubectl, kubectl apply and kubectl create are two of the most commonly used commands for managing resources. They appear to be similar, but their differences could significantly impact how Kubernetes resources are managed. Understanding their distinctions can help optimize resource management within your Kubernetes cluster.
kubectl Create
kubectl create is used to create a resource from a file or from stdin input. It essentially instructs Kubernetes to create new resource objects based on the manifest files provided by the user. Upon execution, kubectl create attempts to initialize a fresh instance of the resource.
Behavior
- Immutable Operation:
kubectl createis designed to create resources only if they do not already exist. If you attempt to create a resource that already exists, Kubernetes will throw an error. - Use Case: Extensively used in CI/CD pipelines where idempotent creation is ensured by deleting resources before creation or using a different strategy for updates.
Example
This command creates a new Pod defined in pod.yaml. If a Pod with the same name already exists, you'll receive an error.
kubectl Apply
kubectl apply, on the other hand, is a more flexible and smarter tool that manages resources by partially updating them depending on the changes in the source file. It could be considered as an evolved version of kubectl create.
Behavior
- Declarative Configurations:
kubectl applyworks with the concept of versioned configurations for resources. It tracks changes in manifest files and updates resources to match the desired state. - Resource Update: If a resource specified in a
kubectl applyoperation already exists, Kubernetes applies the changes required to update the resource rather than throwing an error. It compares the current resource state with the new configuration and determines which changes to apply. - Patch Management: Utilizes a three-way merge strategy to apply changes without impacting the manually edited fields in the resources.
- Use Case: Ideal for managing ongoing changes to Kubernetes resources, providing a flexible management approach by synchronizing the current state with the desired state.
Example
This command creates or updates the Pod defined in pod.yaml depending on whether it exists.
Key Differences
The core difference between kubectl create and kubectl apply lies in their approach for resource management. Below is a summarizing table outlining these key differences:
| Aspect | kubectl create | kubectl apply |
| Operation Type | Imperative | Declarative |
| Resource Update | No updates; creates new resources only | Updates existing resources to match the configuration |
| Resource Conflict | Fails if resource already exists | Safely updates resources using a three-way merge |
| Use Case | Initial resource creation; CI/CD implementations | Ongoing resource management; DevOps environments |
| Error Handling | Throws an error if resource already exists | Handles resources gracefully by updating differences |
Additional Details and Subtopics
When to Use: Best Practices
kubectl create:- Useful when you are certain the resource does not exist and should be created.
- Best used in scripts and systems where resources are cleaned up before creation.
- Ideal for one-off tasks where a fresh instance is mandatory.
kubectl apply:- Excellent for environments where resources need to be frequently updated.
- Beneficial in DevOps practices where configuration drift prevention is critical.
- Fits well with practices like GitOps, where the repository state should reflect the actual cluster state.
Performance Considerations
kubectl apply can be more resource-intensive due to its need to compute the changes that need to be applied. However, its ability to only alter what is necessary makes it efficient for long-standing and dynamic environments. On the other hand, kubectl create is more direct but can lead to inefficiencies in systems that require updates, as manual intervention or deletes may become necessary.
Conclusion
Both kubectl create and kubectl apply have viable use-cases in Kubernetes environments. By comprehending the differences, strengths, and limitations of each, users are better equipped to manage Kubernetes resources efficiently and effectively. Choosing the right command relies heavily on the specific needs of the deployment environment and the desired workflow for maintaining Kubernetes resources.
Incorporating these commands appropriately can streamline Kubernetes operations, ensuring resources are both consistent and optimally managed.

