Nsenter Command: Debug Container Networks with Tcpdump

During a critical production issue, the senior developer quickly isolated the malfunctioning container and began investigating the root cause. Utilizing the nsenter command, she entered the namespace of the target container to run specific diagnostics and commands. With her adept use of Linux tools, she monitored performance metrics and analyzed logs effectively. Her expertise with tools like nsenter allowed the team to resolve the issue in record time.

1. Introduction to Nsenter Command

nsenter is in the util-linux package, which is installed by default in most common Linux distributions. If your system does not have it installed, you can install it using the following command:

A typical use is to enter the network namespace of the container. Usually, in order to be lightweight, most containers do not contain basic network management and debugging tools, such as ip, ping, telnet, ss, tcpdump and other commands, which brings considerable trouble to debugging the network in the container.

The nsenter command can easily enter the network namespace of the specified container and use the host’s commands to debug the container network. In addition, nsenter can also enter the mnt, uts, ipc, pid, user and other namespaces, as well as specify the root directory and working directory.

1.1 Use

The command parameters are as follows:

1.1.1 Options

1.1.2 Common parameters

2. Common Nsenter Command Options Explained


By giving an example of how to capture packets in a container in a real k8s environment

2.1. Get the container ID

Enter the coredns container to capture packets

containerd runtime output snippet

Nsenter Command

Docker runtime output snippet

2.2. Get PID


After getting the container id, we log in to the node where the pod is located to obtain its main process pid.

First determine which node the pod is running on

Log in to the node

When containerd is running, use the crictl command to obtain:

The pid found is 4605

When dockerd is running, use the docker command to get:


3. Enter the container’s netns


nsenter -n -t 4605

After successfully entering the container’s netns, you can use the network tools on the node to debug the network. First, you can use it  ip a to verify whether the IP address is the pod IP:

At this point, you have entered the container space. If you need to capture packets, you can use the packet capture tool on the node.

4. Tcpdump capture


Commonly used capture packets

-r: Specify the package file.

-nn: Display numeric IP and port numbers without converting them to names.

-tttt: Display timestamp format: 2006-01-02 15:04:05.999999.

After capturing the packet, analyze it through wireshark

Summarize


If you do not need to enter the container, you can directly use tcpdum -i to specify the physical network card or virtual network card of the node to capture packets.

The normal network troubleshooting method is to capture the physical network card on the node once and the virtual network card on the container once. By comparing the two, you can determine whether there is an abnormality when the network traffic enters the container