Kubernetes Run Command

KubernetesBeginner
Practice Now

Introduction

The kubectl run command is a simple way to create a Pod in Kubernetes. Modern Kubernetes versions do not use kubectl run to create Deployments or Jobs, so this lab pairs kubectl run with the dedicated commands for those resources. This lab is designed to help beginners understand which command to use for each workload type.

By the end of this lab, you will learn how to:

  1. Start and verify a Minikube cluster.
  2. Create a pod using kubectl run.
  3. Create a deployment with multiple replicas.
  4. Create and inspect a Job for batch processing.
  5. Clean up resources to maintain your cluster.

Start the Minikube Cluster

Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.

  1. Navigate to your working directory:

    Open the terminal and navigate to the default project folder:

    cd /home/labex/project
    
  2. Start Minikube:

    Start Minikube to initialize a Kubernetes cluster:

    minikube start
    
    • This command sets up a single-node Kubernetes cluster on your local machine.
    • Minikube may take a few minutes to start depending on your system's performance.
  3. Verify Minikube is running:

    Check the status of the Minikube cluster:

    minikube status
    
    • Look for components like kubelet and apiserver listed as Running.
    • If the cluster is not running, rerun minikube start.

If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.

Explore the kubectl run Command

The kubectl run command is used to create and run a specific image in a pod. It provides multiple options to customize the pod's behavior, environment, and specifications.

Run the following command to view the available options for kubectl run:

kubectl run -h

You will see the following output:

Create and run a particular image in a pod.

Examples:
## Start a nginx pod
kubectl run nginx --image=nginx

## Start a hazelcast pod and let the container expose port 5701
kubectl run hazelcast --image=hazelcast/hazelcast --port=5701

## Start a hazelcast pod and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the container
kubectl run hazelcast --image=hazelcast/hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"

## Start a hazelcast pod and set labels "app=hazelcast" and "env=prod" in the container
kubectl run hazelcast --image=hazelcast/hazelcast --labels="app=hazelcast,env=prod"

## Dry run; print the corresponding API objects without creating them
kubectl run nginx --image=nginx --dry-run=client

## Start a nginx pod, but overload the spec with a partial set of values parsed from JSON
kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'

## Start a busybox pod and keep it in the foreground, don't restart it if it exits
kubectl run -i -t busybox --image=busybox --restart=Never

## Start the nginx pod using the default command, but use custom arguments (arg1 .. argN) for that command
kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>

## Start the nginx pod using a different command and custom arguments
kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>

Create a Pod

A pod is the smallest deployable unit in Kubernetes and represents one or more containers running together. In this step, we will create a pod running an Nginx web server.

  1. Create the pod:

    Run the following command to create a pod named nginx-pod:

    kubectl run nginx-pod --image=nginx
    
    • The --image option specifies the container image to use. Here, we use the official Nginx image.
  2. Verify the pod:

    Check that the pod is running:

    kubectl get pods
    
    • Look for nginx-pod in the output.
    • The STATUS column should show Running when the pod is ready.

If the pod status shows Pending, Kubernetes may still be pulling the container image. Wait a few moments and rerun kubectl get pods.

Create a Deployment and Scale Replicas

A deployment manages a set of pods and ensures they are running as desired. It is useful for scaling and updating applications.

  1. Create the deployment:

    Run the following command to create a deployment named nginx-deployment:

    kubectl create deployment nginx-deployment --image=nginx
    
    • The --image option specifies the container image to use.
  2. Scale the deployment to 3 replicas:

    Since the --replicas flag is deprecated, we will scale the deployment using kubectl scale instead.

    Use the kubectl scale command to adjust the number of replicas:

    kubectl scale deployment nginx-deployment --replicas=3
    
    • This ensures that three pods are running as part of the deployment.
  3. Verify the deployment and its replicas:

    Check the status of the deployment and pods:

    kubectl get deployments
    kubectl get pods
    
    • Ensure the deployment shows 3 replicas under the READY column.
    • Verify that three pods are listed in the output of kubectl get pods.

If a pod is not in the Running state, it might be due to insufficient cluster resources. Check the pod events with:

kubectl describe pod <pod-name>

Create a Job

In this step, you will create a Kubernetes Job to run a short batch task. A Job tracks successful completion, so it is the right resource when a command should run and exit instead of staying up like a long-running Pod.

  1. Create the job

Run the following command to create a job named busybox-job:

kubectl create job busybox-job --image=busybox -- echo "Hello from Kubernetes"
  • kubectl create job creates a Job resource instead of a standalone Pod.
  • The echo command defines the task the Job will execute.
  1. Check the job status

Run the following command to verify the job:

kubectl get jobs

Expected output:

NAME          COMPLETIONS   DURATION   AGE
busybox-job   1/1           5s         10s
  • COMPLETIONS: Shows the Job ran successfully once (1/1).
  • If no Job is listed yet, wait a few seconds and run kubectl get jobs again.
  1. Verify the job’s pod

Since a job runs inside a pod, use the following command to verify the pod:

kubectl get pods

Expected output:

NAME                      READY   STATUS      RESTARTS   AGE
busybox-job-abcde         0/1     Completed   0          30s
  • The Pod name includes a generated suffix because it is owned by the Job.
  • The STATUS field should display Completed, indicating the Job's Pod finished successfully.
  1. Check job output

Inspect the logs of the Job to verify the output:

kubectl logs job/busybox-job

Expected output:

Hello from Kubernetes

This confirms the job executed successfully.

Cleanup

To keep your cluster clean, delete the resources you created during the lab.

  1. Delete the resources:

    Run the following commands:

    kubectl delete pod nginx-pod
    kubectl delete deployment nginx-deployment
    kubectl delete job busybox-job
    
  2. Verify the cleanup:

    Check that no resources remain:

    kubectl get pods
    kubectl get deployments
    
    • Ensure the output does not list the resources you created.

Summary

In this lab, you learned how to:

  1. Start and verify a Minikube cluster.
  2. Use kubectl run to create a Pod and use dedicated commands to create Deployments and Jobs.
  3. Clean up resources to keep the cluster organized.

You also saw how Kubernetes uses different commands for different workload types. By practicing these steps, you can build a solid foundation for more advanced Kubernetes workflows.