How to Test Port Connectivity Inside a Kubernetes Cluster


When working with Kubernetes, it’s essential to ensure that the services running in your pods are accessible and functioning correctly. One key aspect of testing a service is verifying if a port is working and accepting connections. In this blog post, we’ll walk you through the steps to access a pod and test port connectivity inside your Kubernetes cluster.

Step 1: List the Running Pods

The first step in troubleshooting is to list all the pods running in your Kubernetes cluster. This helps identify the pod where the service is running, so you know where to perform your testing.

You can list all pods using the following command:

kubectl get pods

This will display the names of all the running pods along with their statuses. Take note of the pod you want to test.

Step 2: Access the Pod

Once you know the pod’s name, the next step is to access the pod’s shell. Kubernetes provides a powerful exec command that allows you to open an interactive terminal session inside the container running within a pod.

To enter the pod and get a shell, run:

kubectl exec -it <pod-name> -- /bin/bash

For example, if your pod is called jasmin-0, you would run:

kubectl exec -it jasmin-0 -- /bin/bash

This will give you a terminal inside the container, allowing you to test network connectivity and inspect any logs if necessary.

Step 3: Install Networking Tools (If Required)

If your pod doesn’t have the necessary networking tools like curl, telnet, or nc (netcat) pre-installed, you may need to install them. This is common with minimal container images that don’t include extra utilities by default.

For Debian-based containers, you can install tools using apt:

apt update && apt install curl telnet

For RedHat-based containers, you can use yum or dnf:

yum install curl telnet

These tools will be useful for testing whether the ports in your service are open and accessible.

Step 4: Test Port Connectivity

Once inside the pod, you can now test if the desired port is working. Here are some of the most common tools you can use to check port connectivity:

Using curl for HTTP/HTTPS services

If you’re testing an HTTP or HTTPS service, use curl to make a request to the service on the desired port. For example, to test a service running on port 1401, you would run:

curl http://<service-name>:1401

For example:

curl http://jasmin-http-api:1401

If the port is open and the service is accessible, you should see the response from the service, such as the content of a web page.

Using telnet for TCP services

For testing a general TCP service, telnet can be used. This is useful for checking if a port is accepting TCP connections.

To test if port 8990 is open on the service jasmin-cli, run:

telnet jasmin-cli 8990

If the connection is successful, telnet will confirm that the port is open. If the connection is refused, you’ll see an error message like Connection refused.

Using nc (Netcat) for TCP/UDP services

Netcat (nc) is another tool commonly used to test port connectivity. It is especially useful if you need to test both TCP and UDP ports.

To check if port 2775 on the jasmin-smpp-api service is open, run:

nc -vz jasmin-smpp-api 2775

A successful connection will result in a message like:

Connection to jasmin-smpp-api 2775 port [tcp/*] succeeded!

Step 5: Investigate Further with Pod Logs

If you’re unable to connect to a port or suspect an issue with the service running in the pod, you can review the pod’s logs for any errors or service startup issues. Use the following command to view the logs of a specific pod container:

kubectl logs <pod-name> -c <container-name>

For example:

kubectl logs jasmin-0 -c jasmin

This will show you the logs for the specified container inside the jasmin-0 pod, helping you identify potential issues such as failed services, crashes, or misconfigurations.

Step 6: Exit the Pod

After completing your tests, you can exit the pod’s shell by typing:

exit

This will return you to your local shell prompt.

Conclusion

Testing port connectivity inside a Kubernetes cluster is an essential step in ensuring that your services are accessible and functioning correctly. By following these simple steps, you can easily troubleshoot issues with port access and diagnose network problems within your pods. Whether you’re using curl, telnet, or nc, these tools provide valuable insights into whether your Kubernetes services are reachable or if there are underlying issues preventing connectivity.


Leave a Comment