Cloud Native 7 min read

When to Use kubectl run vs kubectl create/apply in Kubernetes?

This article explains the two primary ways to create resources in Kubernetes—using the direct kubectl run command and using kubectl create/apply with YAML files—providing examples, code snippets, and guidance on when each method is appropriate for ad‑hoc testing or declarative, large‑scale deployments.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
When to Use kubectl run vs kubectl create/apply in Kubernetes?

In Kubernetes, resources can be created in two ways: directly with the kubectl run command or by using kubectl create / kubectl apply with a YAML configuration file. This article demonstrates both approaches with examples and explains when to choose each method.

kubectl run

The kubectl run command creates and runs a pod from a specified image.

$ kubectl run nginx --image=nginx --port 80
pod/nginx created
$ kubectl get po nginx
NAME   READY   STATUS    RESTARTS   AGE
nginx  1/1     Running   0          25s

Describing the pod shows details such as labels and environment variables.

$ kubectl describe po nginx
Name:          nginx
Namespace:     default
... (output truncated for brevity) ...
Labels:        run=nginx

You can also set environment variables when running the pod:

$ kubectl run nginx --image=nginx --port 80 \
  --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"

Running kubectl describe po nginx then reveals the added variables. The kubectl run command is best suited for simple, quick, ad‑hoc tasks and experiments.

kubectl create/apply

The kubectl create and kubectl apply commands work from a YAML file that describes the desired state of the application. The file acts as a reusable template, supports version‑controlled deployments, and is ideal for formal, cross‑environment, large‑scale deployments, though it requires familiarity with YAML syntax.

Example Deployment YAML (nginx‑deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

Creating the resource:

$ kubectl create -f nginx-deployment.yaml
deployment.apps/nginx created
$ kubectl get po
NAME                                 READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-k7tfl               1/1     Running   0          5s

Deleting the resource:

$ kubectl delete -f nginx-deployment.yaml
deployment.apps "nginx" deleted

The same file can be applied with kubectl apply:

$ kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx created

kubectl create vs kubectl apply

create tells the API server to create, delete, or replace resources—an imperative action that builds a new object from scratch or modifies an existing one.

apply declares the desired state in the YAML file, letting the system determine the necessary actions. If the resource does not exist, it is created; if it exists, it is patched to match the declared state.

Attempting to run kubectl create -f nginx-deployment.yaml a second time results in an error because the deployment already exists:

$ kubectl create -f nginx-deployment.yaml
Error from server (AlreadyExists): deployments.apps "nginx" already exists

Running kubectl apply -f nginx-deployment.yaml instead updates the existing deployment without error:

$ kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx configured

Describing the pod after applying shows the updated labels, confirming the declarative change.

In summary, for a single‑file operation both commands behave similarly, but apply excels when managing multiple files or when you want the system to handle creation and patching declaratively.

I hope you enjoyed this quick knowledge share and see you in the next article!

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Kubernetesapplykubectlcreaterun
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.