Skip to content

In-cluster network testing with busybox and netcat

Testing network connectivity and service behavior from within a Kubernetes cluster can be efficiently performed using ad-hoc containers. The busybox image, which includes the nc (netcat) utility, is a common tool for this purpose because it is lightweight and contains essential network utilities^[400-devops-07-monitoring-and-observability-k8s-istio-samples-tcp-echo-readme.md].

Methodology

To execute a test, an ephemeral Pod is created using kubectl run. This Pod serves as a temporary client to send traffic to a target service. The command typically specifies the --restart=Never flag to ensure the Pod runs once and terminates, and the -i flag to keep standard input open for interaction^[400-devops-07-monitoring-and-observability-k8s-istio-samples-tcp-echo-readme.md].

The --rm flag is used to automatically delete the Pod immediately after the command completes, ensuring that no residual resources remain in the cluster^[400-devops-07-monitoring-and-observability-k8s-istio-samples-tcp-echo-readme.md].

Example Usage

The following command demonstrates sending a TCP message to a service named tcp-echo on port 9000^[400-devops-07-monitoring-and-observability-k8s-istio-samples-tcp-echo-readme.md].

$ [kubectl](<./kubectl.md>) run -i --rm --restart=Never dummy --image=busybox -- sh -c "echo world | nc tcp-echo 9000"
hello world
[Pod](<./pod.md>) "dummy" deleted

In this example, the string world is piped into nc, which establishes a TCP connection to the service. The result, hello world, indicates that the server received the input and echoed it back with a prefix^[400-devops-07-monitoring-and-observability-k8s-istio-samples-tcp-echo-readme.md].

Sources

  • 400-devops-07-monitoring-and-observability-k8s-istio-samples-tcp-echo-readme.md