Kubernetes Exec Command

KubernetesKubernetesBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicsGroup(["`Basics`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/exec("`Exec`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/run("`Run`") kubernetes/BasicsGroup -.-> kubernetes/initialization("`Initialization`") subgraph Lab Skills kubernetes/describe -.-> lab-8502{{"`Kubernetes Exec Command`"}} kubernetes/exec -.-> lab-8502{{"`Kubernetes Exec Command`"}} kubernetes/get -.-> lab-8502{{"`Kubernetes Exec Command`"}} kubernetes/run -.-> lab-8502{{"`Kubernetes Exec Command`"}} kubernetes/initialization -.-> lab-8502{{"`Kubernetes Exec Command`"}} end

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 exec Command

The 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

Executing a Command in a Container

In this step, you will learn how to execute a command in a container running in a pod.

  1. Start by creating a deployment with one replica and an Nginx container:

    kubectl create deployment nginx --image=nginx --replicas=1
  2. Wait for the pod to become ready:

    kubectl wait --for=condition=Ready pod -l app=nginx
  3. 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.

Executing a Command in a Specific Container

In this step, you will learn how to execute a command in a specific container running in a pod with multiple containers.

  1. 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
  2. Wait for the pod to become ready:

    kubectl wait --for=condition=Ready pod nginx-busybox
  3. Use the kubectl exec command to execute a command inside the BusyBox container:

    kubectl exec nginx-busybox -c busybox -- ls /bin

Executing a Command with a Tty

In this step, you will learn how to execute a command with a tty in a container.

  1. Use the kubectl exec command with the -it options to execute a command with a tty:

    kubectl exec -it nginx-busybox -- /bin/sh
  2. Once inside the container shell, run a command:

    echo "Hello, world!"
  3. Exit the container shell:

    exit

Executing a Command with Environment Variables

In this step, you will learn how to execute a command with environment variables inside a container.

  1. 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"
  2. Wait for the pod to become ready:

    kubectl wait --for=condition=Ready pod -l run=nginx-env
  3. 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.

Summary

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.

Other Kubernetes Tutorials you may like