Open In App

How To kubectl Exec Into Pod?

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To run a command within a container that is already executing inside a pod, use the command “kubectl exec into pod.” This feature helps with debugging, troubleshooting, and administrative chores in the containerized environment.

What Is Kubectl?

A command-line interface for working with Kubernetes clusters is called Kubectl. It lets users to do a lot of things like diagnosing problems, inspecting cluster state, managing resources in the cluster, and installing applications. Using a set of commands and options, Kubectl gives developers, administrators, and operators efficient control over Kubernetes clusters.

What Is Kubectl Exec Into Pod?

kubectl exec is a command in Kubernetes that allows you to execute commands inside a running container within a pod. It provides a way to interact with the running processes inside the container, similar to how you would SSH into a virtual machine.

kubectl exec <pod_name> [options] -- <command> [args]
  • <pod_name>: Specifies the name of the pod where you want to execute the command.
  • [options]: Additional options you can specify, such as -c to specify the container name if the pod has multiple containers.
  • <command> [args]: The command you want to execute inside the container, along with any arguments.

Why Do We Need To kubectl Exec Into Pod?

To access a pod within a Kubernetes cluster, you may need to use kubectl exec for a number of reasons:

  • Debugging: To examine logs, verify configurations, or run diagnostic commands, you might need to gain access to the container when debugging a problem with your application operating inside a pod.
  • Executing one-time tasks: Occasionally, you might need to carry out a one-time operation—like installing fixes or executing maintenance scripts—within an operating container.
  • Interactive sessions: To initiate an interactive shell session within a container for human intervention or debugging, use kubectl exec.
  • Data retrieval: It may be necessary for you to take artifacts or data produced by your application straight out of the container.
  • Testing: You can use kubectl exec to perform tests, which enables you to

Steps To kubectl Exec Into Pod

Step 1: Make a pod that you wish to interact with and run the commands inside of. You can use the following manifest file to create the pod.

apiVersion: v1
kind: Pod
metadata:
name: nginxapp
spec:
containers:
- name: jenkins
image: nginx
ports:
- containerPort: 80
  • apiVersion: Specifies the API version of the Kubernetes object being created. In this case, it’s v1, indicating the core/v1 version of the API.
  • kind: Specifies the type of Kubernetes object being created. Here, it’s Pod, indicating that this YAML defines a Pod resource.
  • metadata: Contains metadata about the Pod, such as its name, namespace, and labels.
  • name: Specifies the name of the Pod, which is set to “nginxapp”.
  • namespace: Specifies the namespace in which the Pod will be created. In this case, it’s “test-ns”.
  • labels: Provides key-value pairs to help identify and organize the Pod. In this case, the Pod is labeled with “app: nginxapp”.
  • spec: Defines the specification for the Pod, including its containers and other properties.
  • containers: Specifies the containers running within the Pod.
  • name: Specifies the name of the container. Here, it’s “jenkins”.
  • image: Specifies the Docker image to use for the container. In this case, it’s “nginx”, which is the official Nginx image.
  • ports: Specifies the ports that the container exposes.
  • containerPort: Specifies the port number on which the container will listen for incoming traffic. Here, it’s set to 80, indicating that the container listens on port 80.

manifest.yaml

Step 2: You must apply the above manifest file to the pod by using the following command after creating the manifest file as shown above.

kubectl apply -f <name od the manifest file>

Step 3: List all the pods avalibe in the kubernetes cluster here we have deployed pod in the default namespace.

kubectl get pods
  • kubectl: This is the command-line tool for interacting with Kubernetes clusters.
  • get: This is a subcommand of kubectl used to retrieve information about Kubernetes resources.
  • pods: This specifies that we want to retrieve information about pods specifically.

Screenshot-(687)

Step 4: Recognise that you have the ability to perform commands inside a pod’s running container. With the assistance of the command below.

kubectl exec -it nginxapp bin/bash

Step 5: I’ve created a sample file below that you may edit inside the pod to suit your organization’s needs. For example, you can perform debugging and make any necessary revisions.

kubectl apply

Conclusion

To execute commands within a pod in Kubernetes using kubectl exec, you first specify the pod’s name with -it <pod_name>. Optionally, if the pod contains multiple containers, you can specify the container’s name with -c <container_name>. Then, append — followed by the command you wish to run within the container. This command structure allows you to interactively access and execute commands within the container’s environment. For instance, running kubectl exec -it my-pod -c my-container — /bin/bash will initiate an interactive shell session within the specified container in the designated pod.

kubectl Exec – FAQ’s

What Is The Difference Between Docker Exec And kubectl Exec?

  • Docker exec: Operates on a single Docker container running on a local Docker engine. It allows you to execute commands within the context of a specific Docker container.
  • kubectl exec: Operates within the Kubernetes cluster. It allows you to execute commands within a container running inside a pod managed by Kubernetes.

What Is kubectl Used For?

“kubectl” is a command-line tool used for interacting with Kubernetes clusters. It allows users to perform various tasks related to managing and administering Kubernetes resources.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads