Introduction
In this lab, you will learn how to use the Kubernetes exec
command to execute commands inside a container running in a Kubernetes pod. You will start with simple examples and gradually progress to more complex scenarios.
In this lab, you will learn how to use the Kubernetes exec
command to execute commands inside a container running in a Kubernetes pod. You will start with simple examples and gradually progress to more complex scenarios.
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/project
Start Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status
kubelet
and apiserver
listed as Running
.minikube start
.If you encounter issues starting Minikube. Use minikube delete
to reset the environment if needed.
kubectl exec
CommandThe kubectl exec
command is used to execute commands directly inside a container within a pod. It is particularly useful for debugging and inspecting container environments.
Run the following command to view the available options for kubectl exec
:
kubectl exec -h
You will see the following output:
Execute a command in a container.
Examples:
## Get output from running the 'date' command from pod mypod, using the first container by default
kubectl exec mypod -- date
## Get output from running the 'date' command in ruby-container from pod mypod
kubectl exec mypod -c ruby-container -- date
## Switch to raw terminal mode; sends stdin to 'bash' in ruby-container from pod mypod
## and sends stdout/stderr from 'bash' back to the client
kubectl exec mypod -c ruby-container -i -t -- bash -il
## List contents of /usr from the first container of pod mypod and sort by modification time
## If the command you want to execute in the pod has any flags in common (e.g. -i),
## you must use two dashes (--) to separate your command's flags/arguments
## Also note, do not surround your command and its flags/arguments with quotes
## unless that is how you would execute it normally (i.e., do ls -t /usr, not "ls -t /usr")
kubectl exec mypod -i -t -- ls -t /usr
## Get output from running 'date' command from the first pod of the deployment mydeployment, using the first container by default
kubectl exec deploy/mydeployment -- date
## Get output from running 'date' command from the first pod of the service myservice, using the first container by default
kubectl exec svc/myservice -- date
In this step, you will learn how to execute a command in a container running in a pod.
Start by creating a deployment with one replica and an Nginx container:
kubectl create deployment nginx --image=nginx --replicas=1
Wait for the pod to become ready:
kubectl wait --for=condition=Ready pod -l app=nginx
Use the kubectl exec
command to execute a command inside the Nginx container:
kubectl exec nginx -v < pod_name > --
Replace <pod_name>
with the name of the pod created in step 1, and you can get the <pod_name>
with the kubectl get pod -l app=nginx
command.
In this step, you will learn how to execute a command in a specific container running in a pod with multiple containers.
Create a pod with two containers: Nginx and BusyBox:
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: nginx-busybox
spec:
containers:
- name: nginx
image: nginx
- name: busybox
image: busybox
command:
- sleep
- "3600"
EOF
Wait for the pod to become ready:
kubectl wait --for=condition=Ready pod nginx-busybox
Use the kubectl exec
command to execute a command inside the BusyBox container:
kubectl exec nginx-busybox -c busybox -- ls /bin
In this step, you will learn how to execute a command with a tty in a container.
Use the kubectl exec
command with the -it
options to execute a command with a tty:
kubectl exec -it nginx-busybox -- /bin/sh
Once inside the container shell, run a command:
echo "Hello, world!"
Exit the container shell:
exit
In this step, you will learn how to execute a command with environment variables inside a container.
Create a deployment with one replica and an Nginx container with an environment variable:
kubectl run nginx-env --image=nginx --env="MY_VAR=my-value"
Wait for the pod to become ready:
kubectl wait --for=condition=Ready pod -l run=nginx-env
Use the kubectl exec
command to execute a command inside the Nginx container that outputs the environment variable:
kubectl exec nginx-env -- sh -c 'echo $MY_VAR'
Replace nginx-env with the name of the pod created in step 1.
Congratulations, you have successfully completed the Kubernetes exec command lab! In this lab, you learned how to use the kubectl exec
command to execute commands inside containers running in Kubernetes pods. You also learned how to execute commands in specific containers, execute commands with a tty, and execute commands with environment variables. These skills are essential for debugging issues in a Kubernetes cluster.