Kubernetes has become the go-to platform for containerized application management. One of the most critical tools in the Kubernetes ecosystem is kubectl, which allows users to interact with their clusters. Among the many kubectl commands, kubectl apply, and kubectl create are essential for resource management. While they seem similar at first glance, they serve different purposes. This guide will explore the key differences and help you decide when to use each command effectively.

Introduction to kubectl Commands for Kubernetes Resource Management

kubectl is a command-line tool that helps you manage Kubernetes resources. It allows you to deploy applications, inspect cluster resources, and manage Kubernetes objects like Pods, Services, ConfigMaps, and Deployments. Two frequently used commands for creating or modifying resources are kubectl apply, and kubectl create. Understanding their distinctions is critical for effective Kubernetes management.

Understanding the Purpose and Usage of kubectl apply

kubectl apply the command you should use when creating or modifying existing resources declaratively. This means that you describe the desired state of your resources in a YAML or JSON file, and Kubernetes will ensure that the current state matches this desired state.

One of the key features of kubectl apply is that it tracks changes to resources over time. This makes it suitable for managing resources in a GitOps-style workflow, where configurations are version-controlled and gradually rolled out.

Critical Benefits of kubectl apply:

  • Declarative Management: You can modify resources without deleting and recreating them.
  • Version Control Integration: Works well with Infrastructure as Code (IaC) practices.
  • Incremental Updates: It only applies the differences between the current and desired states.

Example of kubectl apply:

kubectl apply -f deployment.yaml

In this example, the deployment described in the deployment.yaml file will be created or updated depending on whether it already exists.

Detailed Examination of kubectl create Command Functionality.

On the other hand, kubectl create is primarily used for creating new resources. It doesn’t track or compare resource configurations over time. Once a resource is created, any changes to its configuration must be made through other commands like kubectl replace or kubectl delete followed by a kubectl create again.

While kubectl create is faster and simpler than kubectl apply, it is more limited in scope. It’s well-suited for scenarios where you need to make a resource quickly but do not intend to manage its ongoing lifecycle declaratively.

Critical Benefits of kubectl create:

  • Speed: Quickly creates resources without any additional tracking or comparison.
  • Simplicity: Suitable for one-time resource creation.

Example of kubectl create:

kubectl create -f pod.yaml

Here, the Pod described in the pod.yaml file is created, but future changes won’t be managed unless done manually.

Critical Differences Between kubectl apply, and kubectl create

Feature kubectl apply kubectl create
Purpose Declarative resource management Imperative resource creation
Modifications Automatically updates existing resources Requires manual intervention for updates
Version Control Friendly Yes No
Resource Lifecycle Continuously managed One-time creation
Performance Slightly slower due to state comparison Faster, as it doesn’t check the state

Practical Examples Demonstrating kubectl apply and kubectl create Usage

Let’s walk through some examples to see these commands in action.

Example 1: Using kubectl apply for Deployment Management

You can create and manage a deployment with kubectl apply:

# deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: my-app

spec:

  replicas: 3

  selector:

    matchLabels:

      app: my-app

  template:

    metadata:

      labels:

        app: my-app

    spec:

      containers:

      – name: my-app

        image: nginx

By running kubectl apply -f deployment.yaml, Kubernetes will ensure that three replicas of the nginx container are running. If you later decide to change the replica count to 5, updating the YAML file and running kubectl apply again will update the deployment without downtime.

Example 2: Using kubectl create for Quick Pod Creation

Here’s how you can use kubectl create to deploy a pod quickly:

# pod.yaml

apiVersion: v1

kind: Pod

metadata:

  name: my-pod

spec:

  containers:

  – name: my-container

    image: nginx

Running kubectl create -f pod.yaml will launch the nginx pod. However, if you want to modify the pod later, you must manually delete and recreate it or use other commands like kubectl replace.

Choosing between Kubectl apply and kubectl create for Effective Resource Management

When to use kubectl apply:

  • For continuous and declarative resource management.
  • When resources need to be version controlled and updated incrementally.
  • Ideal for configurations managed in source control repositories (e.g., Git).

When to use kubectl create:

  • This is for quick resource creation without further lifecycle management.
  • When you don’t need to modify the resource later or prefer manual control.
  • Ideal for one-time resources like jobs or test environments.

In general, kubectl apply is the recommended option for long-term and scalable management of your Kubernetes environment. However, if you’re looking for simplicity and speed for one-off resource creation, kubectl create can be more efficient.

Conclusion

Both kubectl apply, and kubectl create are potent commands that serve different purposes in Kubernetes resource management. Understanding their differences and use cases can help you manage your Kubernetes cluster more effectively. Choose kubectl apply for declarative, version-controlled updates, and kubectl create for rapid, one-time deployments.

References

What is a Kubernetes Cluster?

Set up kubectl and eksctl