Using the kubectl run command (with examples)

Using the kubectl run command (with examples)

The kubectl run command in Kubernetes is used to run pods. It is a versatile command that allows users to quickly and easily create and manage pods in a Kubernetes cluster. In this article, we will explore different use cases of the kubectl run command with several code examples.

Use Case 1: Run an nginx pod and expose port 80

kubectl run nginx-dev --image=nginx --port 80

Motivation: Running an nginx pod and exposing port 80 is a common use case when deploying web applications. By using the kubectl run command with the appropriate arguments, we can quickly create and deploy an nginx pod with port 80 exposed.

Explanation:

  • kubectl run: The base command to create and manage pods.
  • nginx-dev: The name of the pod to be created.
  • --image=nginx: Specifies the container image to be used for the pod. In this case, the nginx image is used.
  • --port 80: Exposes port 80 of the pod.

Example Output:

pod/nginx-dev created

Use Case 2: Run an nginx pod, setting an environment variable

kubectl run nginx-dev --image=nginx --env="TEST_VAR=testing"

Motivation: Setting environment variables in pods is often necessary for configuring and customizing the behavior of an application. By using the kubectl run command with the --env argument, we can easily set environment variables in the pod.

Explanation:

  • --env="TEST_VAR=testing": Sets the environment variable TEST_VAR to the value testing in the nginx pod.

Example Output:

pod/nginx-dev created

Use Case 3: Show API calls to create an nginx container

kubectl run nginx-dev --image=nginx --dry-run=none|server|client

Motivation: Sometimes, it is necessary to see the underlying API calls that would be made to create a pod in Kubernetes. By using the --dry-run option with different values, we can generate the API calls without actually creating the pod.

Explanation:

  • --dry-run=none|server|client: Specifies the level of dry run output. Values can be none, server, or client.
    • none: No dry run is performed.
    • server: The generated API calls are sent to the Kubernetes server.
    • client: The generated API calls are displayed on the client without being sent to the server.

Example Output:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx-dev
  name: nginx-dev
spec:
  containers:
  - image: nginx
    name: nginx-dev
    ports:
    - containerPort: 80
      protocol: TCP
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

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

kubectl run temp-ubuntu --image=ubuntu:22.04 --restart=Never --rm -- /bin/bash

Motivation: Sometimes, it is necessary to run a pod interactively for troubleshooting or debugging purposes. By using the kubectl run command with the appropriate arguments, we can create an Ubuntu pod that runs an interactive shell and is removed when it exits.

Explanation:

  • --restart=Never: Specifies that the pod should never be restarted automatically.
  • --rm: Removes the pod when it exits.
  • /bin/bash: Overrides the default command of the container with /bin/bash, allowing us to run an interactive shell.

Example Output:

pod/temp-ubuntu created

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

kubectl run temp-ubuntu --image=ubuntu:22.04 --command -- echo argument1 argument2 ...

Motivation: Overriding the default command of a container with a custom command is often necessary for running specific tasks or scripts. By using the kubectl run command with the --command option and specifying the custom command and arguments, we can easily run arbitrary commands in the pod.

Explanation:

  • --command: Specifies that the following arguments are the command and its arguments.
  • echo argument1 argument2 ...: Overrides the default command of the container with the echo command, and specifies the custom arguments.

Example Output:

pod/temp-ubuntu created

Conclusion

In this article, we have explored different use cases of the kubectl run command in Kubernetes. By providing various arguments and options, we can easily create and manage pods in a Kubernetes cluster. From running an nginx pod with port 80 exposed to running an Ubuntu pod with a custom command, the kubectl run command is a powerful tool for pod management in Kubernetes.

Make sure to consult the official Kubernetes documentation for additional information and to stay up to date with any changes in the command syntax and behavior.

Related Posts

How to use the command 'rename' (with examples)

How to use the command 'rename' (with examples)

The ‘rename’ command is used to rename a file or group of files with a regular expression.

Read More
How to use the command 'log' (with examples)

How to use the command 'log' (with examples)

The ’log’ command is used to view, export, and configure logging systems.

Read More
8 Aws Cur Command Examples (with examples)

8 Aws Cur Command Examples (with examples)

In this article, we will explore 8 different use cases of the AWS CUR (Cost and Usage Report) command.

Read More