The kubectl exec command is used to execute commands directly inside a running container within a Kubernetes Pod. Its primary purposes include:
-
Debugging: You can troubleshoot issues by accessing the container's shell and running commands to inspect the environment, check configurations, or view logs.
-
Interacting with Applications: It allows you to interact with applications running inside the container, such as running database queries or testing network connectivity.
-
Running One-off Commands: You can execute temporary commands without needing to modify the container image or redeploy the Pod.
Basic Syntax:
kubectl exec -it <pod-name> -- <command>
-it: This flag enables interactive terminal mode, allowing you to interact with the shell.<pod-name>: The name of the Pod where the command will be executed.<command>: The command you want to run inside the container.
Example:
To open a shell in a Pod named nginx-pod:
kubectl exec -it nginx-pod -- /bin/sh
This command is essential for managing and troubleshooting applications running in Kubernetes. If you want to learn more about using kubectl exec, consider checking out relevant LabEx labs!
