“kubectl run” Command Examples

kubectl run is a command-line tool used to create and run pods in a Kubernetes cluster. It is a versatile command that allows users to quickly deploy containers as pods, enabling the execution of various workloads within the Kubernetes environment.

Here’s a more detailed explanation of kubectl run and its features:

  • Pod Creation: The primary purpose of kubectl run is to create pods in a Kubernetes cluster. Users can specify the name of the pod, the container image to use, and additional configuration options such as environment variables, command-line arguments, resource limits, and more.
  • Container Deployment: kubectl run simplifies the process of deploying containers as pods by providing a single command to initiate the deployment. Users can specify the container image they want to deploy, and Kubernetes will automatically create a pod running the specified container image.
  • Pod Generator: In some Kubernetes versions, kubectl run can be used as a pod generator to avoid deprecation errors. By specifying the –generator flag with the value run-pod/v1, users can ensure compatibility with older Kubernetes versions and avoid deprecated features.
  • Flexible Options: kubectl run supports a wide range of options and configurations to customize pod deployment according to user requirements. Users can specify options such as labels, annotations, service account, restart policy, affinity rules, and more to fine-tune the behavior and characteristics of the created pods.
  • Integration with Kubernetes API: kubectl run interacts directly with the Kubernetes API server to create and manage pods within the cluster. This allows users to leverage the full capabilities of Kubernetes, such as scheduling, networking, and resource management, when deploying containers as pods using kubectl run.
  • Documentation and Resources: More information about kubectl run, including usage examples, command options, and best practices, can be found in the official Kubernetes documentation (https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run). The documentation offers comprehensive guidance on using kubectl run effectively and deploying pods in Kubernetes clusters.

“kubectl run” Command Examples

1. Run an nginx pod and expose port 80:

# kubectl run --generator=run-pod/v1 nginx --image=nginx --port 80

2. Run an nginx pod, setting the TEST_VAR environment variable:

# kubectl run --generator=run-pod/v1 nginx --image=nginx --env="TEST_VAR=testing"

3. Show API calls that would be made to create an nginx container:

# kubectl run --generator=run-pod/v1 nginx --image=nginx --dry-run

4. Run an Ubuntu pod interactively, never restart it, and remove it when it exits:

# kubectl run --generator=run-pod/v1 -it temp-ubuntu --image=ubuntu:20.04 --restart=Never --rm -- /bin/bash

5. Run an Ubuntu pod, overriding the default command with echo, and specifying custom arguments:

# kubectl run --generator=run-pod/v1 temp-ubuntu --image=ubuntu:20.04 --command -- echo arg1 arg2 arg3

Summary

Overall, kubectl run is a powerful and convenient tool for deploying pods and running containerized workloads in Kubernetes clusters. It provides users with a simple and efficient means of initiating pod deployments, enabling rapid development and deployment of applications and services within Kubernetes environments.

Related Post