Kubernetes
kubectl
deployment
DevOps
container orchestration

kubernetes - kubectl run vs create and apply

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Kubernetes is a powerful platform for managing containerized applications across a cluster of machines. Its command-line tool, `kubectl`, is essential for interacting with Kubernetes. However, the nuances between the `kubectl run`, `kubectl create`, and `kubectl apply` commands can be confusing, especially for beginners. This article delves into each command, their differences, and use cases with examples to clarify when and why to use each one.

Understanding `kubectl`

Before diving into the specifics of each command, it’s essential to have a basic understanding of `kubectl`. It’s the primary command-line interface used to manage Kubernetes resources. `kubectl` allows users to create and control Kubernetes objects by sending requests to the Kubernetes API server.

`kubectl run`

Overview

`kubectl run` was originally used to create deployments or run a simple pod directly from an image. However, as Kubernetes evolved, the focus of this command shifted primarily towards creating a single Pod.

Example Usage

  • Best suited for running a single Pod.
  • Simplifies configuration by not requiring a separate YAML file, making it useful for quick tests or temporary setups.
  • As of newer Kubernetes versions, using `kubectl run` to create controllers like Deployments is deprecated.
  • Ideal for creating declarative resources.
  • Ensures immutability; once created, you have to use another command (`kubectl delete`) and reissue `create` to make changes.
  • Often used in scripts for initial resource definition.
  • Supports creation from both files and command-line specifications.
  • Supports declarative object configuration management.
  • Ideal for managing resources over time, supporting updates without disruptions.
  • Keeps track of changes using the kubernetes “last applied” configuration metadata.
  • Facilitates continuous integration/continuous deployment (CI/CD) pipelines.
  • Development and Testing: Use `kubectl run` for testing scenarios where rapid iteration and ephemeral environments are key.
  • Production and CI/CD Environments: Favor `kubectl apply` to maintain configurations over time and streamline updates.
  • Immutable Deployments: Utilize `kubectl create` when you require certain resources to be strictly defined and managed manually.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.