19. April 2021

How to do an ICMP ping of a Service in Kubernetes

When moving software to the cloud you may encounter a component that relies on good old ICMP ping.

The problem is that Kubernetes does not support ICMP ping between services. Nigel Poulton described reasons in his post.

But what if the software depends on ICMP ping because it’s hardcoded since days of the pre-cloud era?

One way to solve the problem and make ICMP ping between Service working in Kubernetes is to use the following trick which has two steps:
– open TCP tunnel which forwards a port of service to localhost
– define host alias in /etc/hosts pointing to 127.0.0.1

The trick is based on the simple idea that you can ping localhost and talk to the port with local address which is then forwarded to the desired address.

Commands to add ICMP ping to service “myservice”:

socat tcp-listen:8000,reuseaddr,fork tcp:myservice:8000
echo "127.0.0.1 myservice" >>/etc/hosts

Test:

ping myservice
nc -z myservice 8000

The solution is definitely not 100% bullet-proof, but it can be a good workaround until the software is fixed.