Debugging on Remote Kubernetes using Workstation Tools
What is my current challenge ?
95% of issues can be easily diagnosed with good telemetry data with logging, traces, and metrics. But there are some tricky issues that do require remote troubleshooting. The remote network, enterprise firewall rules make it a little tricky to leverage local tooling.
As an example, Have you tried running a remote debugger on a remote pod ? Or have you ever wanted to connect to a remote database/messaging system, such as Kafka, using a workstation but have been blocked by firewall rules?
What if there was an easier way to troubleshoot using tools that are setup on your local workstation ? What if you could use these tools to connect to remote systems and databases? Meet two of my favorite tools, sshuttle and kuttle which is essentially a poor man's VPN solution leveraging kubectl exec.
Pre-Requisites
How do I setup ?
kubectl run kuttle -n kube-system --image=python:alpine --restart=Never -- sh -c 'exec tail -f /dev/null' (Note: You can change to desired namespace using -n option)
sshuttle -v -r 'kuttle -n kube-system' -e kuttle 10.121.0.0/16 -v
Recommended by LinkedIn
(Replace 10.121.0.0/16 by remote CIDR range for your k8s network)
How does it work under the hood?
>> iptables -t nat -A sshuttle-12300 -j REDIRECT --dest 10.161.0.0/16 -p tcp --to-ports 12300 -m ttl ! --ttl 42
├─kubectl,5329 exec -i kuttle -n kube-system -- /bin/sh -c exec /bin/sh -c 'P=python3; $P -V 2>/dev/null || P=python; exec "$P" -c '"'"'import sys, os; verbosity=1; sys.stdin = os.fdopen(0, "rb"); exec(compile(sys.stdin.read(1317), "assembler.py", "exec"))'"'"''
Final word of thought
As powerful as this tooling might sound, it does allow remote Python script execution. For normal applications, this can easily be blocked by runtime container scanning tools. As a result, this approach should only be used in a break-glass scenario, when telemetry data was not enough to get to the root cause.