Refer to the official Kubernetes website documentation: Containers.
A container is a lightweight, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings. While Pods are the smallest deployable units, containers are the core components within Pods.
Debugging Containers with Refer to the official Kubernetes website documentation: Ephemeral Containers
The Kubernetes Ephemeral Containers feature provides a robust way to debug running containers by injecting specialized debugging tools (system, network, and disk utilities) into an existing Pod.
While you can often execute commands directly within a running container using kubectl exec, many production container images are intentionally minimal and may lack essential debugging utilities (e.g., bash, net-tools, tcpdump) to reduce image size and attack surface. Ephemeral Containers address this limitation by providing a pre-configured environment with a rich set of debugging tools, making them ideal for the following scenarios:
The debugging functionality is implemented using Ephemeral Containers. An Ephemeral Container is a special type of container designed for introspection and debugging. It shares the Pod's network namespace and process namespace (if enabled) with the existing primary containers
, allowing it to directly interact with and observe the application processes.
You can dynamically add an Ephemeral Container (e.g., my-app-debug
) to a running Pod and utilize its pre-installed debugging tools. The diagnostic results from this Ephemeral Container are directly relevant to the behavior and state of the primary application containers
within the same Pod.
Kubernetes 1.25+ offers the kubectl debug
command for creating ephemeral containers. This method provides a powerful command-line alternative for debugging.
Command
Example: Debugging nginx
in my-nginx-pod
First, ensure you have a Pod running:
Now, create an ephemeral debug container named debugger
inside my-nginx-pod
, targeting my-nginx-container, using the busybox
image:
This command will attach you to a shell inside the debugger ephemeral container. You can now use busybox tools to debug my-nginx-container.
To view the ephemeral containers attached to a Pod:
Look for the Ephemeral Containers
section in the output.
Container Platform, and navigate to Workloads > Pods in the left sidebar.
Locate the Pod you wish to view, and click ⋮ > Debug.
Choose the specific container within the Pod you wish to debug.
(Optional) If the interface prompts that initialization is required (e.g., for setting up necessary debug environment), click Initialize.
After initializing the Debug feature, as long as the pod is not recreated, you can directly enter the Ephemeral Container (for example, Container A-debug) for debugging.
Wait for the debugging terminal window to become ready, then begin your debugging operations. Tip: Click the "Command Query" option in the upper right corner of the terminal to view a list of common debugging tools and their usage examples.
Click the command query in the upper right corner to view common tools and their usage.
Once debugging is complete, close the terminal window.
You can directly interact with the internal instance of a running container using the kubectl exec
command, allowing you to execute arbitrary command-line operations. Additionally, Kubernetes provides convenient features for uploading and downloading files to and from containers.
To execute a command inside a specific container within a Pod (useful for getting a shell, running diagnostic commands, etc.):
Example: Getting a Bash shell in the nginx
of my-nginx-pod
Example: Listing files in /tmp
of a container
To copy files from your local machine to a container within a Pod:
To copy files from a container within a Pod to your local machine:
You can enter the internal instance of the container using the kubectl exec
command, allowing you to execute command-line operations in the Web console window. Additionally, you can easily upload and download files within the container using the file transfer feature.
Container Platform, and navigate to Application > Applications in the life sidebar.
Click on Application Name.
Locate the associated workload (e.g., Deployment, StatefulSet), click EXEC, and then select the specific Pod Name you wish to enter. EXEC > Contianer Name.
Enter the command you wish to execute.
Click OK to enter the Web console window and execute command-line operations.
Click File Transfer.
Container Platform, and navigate to Workloads > Pods.
Locate the target Pod, click the vertical ellipsis (⋮) next to it, select EXEC, and then choose the specific Container Name within that Pod you wish to enter.
Enter the command you wish to execute.
Click OK to enter the Web console window and execute command-line operations.
Click File Transfer.