Kubernetes
kubectl
deployment
DevOps
container orchestration

kubernetes - kubectl run vs create and apply

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

Course illustration
Course illustration