Introduction
The kubectl run command is one of the simplest and most versatile ways to create resources in Kubernetes. It allows you to quickly deploy containers, create jobs for batch processing, or set up deployments to manage multiple replicas of an application. This lab is designed to help beginners understand the basics of the kubectl run command and how to use it effectively.
By the end of this lab, you will learn how to:
- Start and verify a Minikube cluster.
- Create a pod using
kubectl run. - Create a deployment with multiple replicas.
- Run a job for batch processing.
- 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.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/projectStart 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.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
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.
Create the pod:
Run the following command to create a pod named
nginx-pod:kubectl run nginx-pod --image=nginx- The
--imageoption specifies the container image to use. Here, we use the official Nginx image.
- The
Verify the pod:
Check that the pod is running:
kubectl get pods- Look for
nginx-podin the output. - The
STATUScolumn should showRunningwhen the pod is ready.
- Look for
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.
Create the deployment:
Run the following command to create a deployment named
nginx-deployment:kubectl create deployment nginx-deployment --image=nginx- The
--imageoption specifies the container image to use.
- The
Scale the deployment to 3 replicas:
Since the
--replicasflag is deprecated, we will scale the deployment usingkubectl scaleinstead.Use the
kubectl scalecommand 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.
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
READYcolumn. - Verify that three pods are listed in the output of
kubectl get pods.
- Ensure the deployment shows 3 replicas under the
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
A job is used to run tasks that need to complete successfully. For example, batch jobs or data processing tasks. We will use kubectl run to create a job and verify its execution.
- Create the job
Run the following command to create a job named busybox-job:
kubectl run busybox-job --image=busybox --restart=OnFailure -- echo "Hello from Kubernetes"
- The
--restart=OnFailureflag specifies that this is a job. - The
echocommand defines the task the job will execute.
- 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, it may have been automatically cleaned up. Proceed to the next step to check its pod.
- 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 0/1 Completed 0 30s
- The
STATUSfield should displayCompleted, indicating the job has finished.
- Check job output
Inspect the logs of the job's pod to verify the output:
kubectl logs 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.
Delete the resources:
Run the following commands:
kubectl delete pod nginx-pod kubectl delete pod busybox-job kubectl delete deployment nginx-deploymentVerify 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:
- Start and verify a Minikube cluster.
- Use
kubectl runto create and manage pods, deployments, and jobs. - Clean up resources to keep the cluster organized.
The kubectl run command provides a simple and powerful way to deploy applications and manage workloads in Kubernetes. By practicing these steps, you can build a solid foundation for more advanced Kubernetes workflows.


