Better Debugging Environment for your Micro-Services
Debugging a micro-service in a Kubernetes cluster can be a real pain.
Sometimes we face burdensome bugs which might be hard to reproduce in a non-realistic environment.
What if I told you debugging on your local machine with your favorite tools feeling like your service is running remotely inside your cluster is just a few command-lines away?
Meet the Tools
In order to reach our desired environment I’ll mainly use the following tools:
- Gebug: an open-source tool that makes debugging of Dockerized Go applications much easier by enabling hot reload and debugger configurations (Disclaimer: I’m this tool’s author 😊)
- Telepresence: an amazing CNCF project which helps us set up network tunneling with our cluster network
In my book, the best way to get familiar with a new tool is by getting your hands dirty. So, let’s set up our demo environment.
Kubernetes cluster
We’ll use a tiny Kubernetes cluster running a “hello-world” deployment and an exposed service.
For the demo’s sake, I’ll use minikube. If you have a cluster up and running (and it’s not a production one) you can use it too.
We can retrieve our service external endpoint and run a little test:
Hello-World Service
Our hello-world deployment is nice. But, let’s say we want to change the HTTP response to a JSON one.
You can of course compile your application, build a Docker image, push it, edit the deployment, and… you get it.
We’re here to make things faster and more fun.
This will be our tiny HTTP server:
Along with the following Gebug configurations:
If you don’t have Gebug installed you can simply run:
go get -u github.com/moshebe/gebug
Telepresence
This awesome tool enables us to connect our service to the cluster without the hassle of the network configuration.
The Installation Guide can be found here: https://www.telepresence.io/reference/install
The next step in exposing our local service and rewire the traffic to and from the cluster so we replace the existing deployment.
Our deployment name is “hello-world” and the exposed port is 8000. So the command we should use will be something like:
This command does a few things:
- Starts a VPN-like process that sends queries to the appropriate DNS and IP ranges to the cluster.
— swap-deployment
tells Telepresence to replace the existing hello-world pod with one running the Telepresence proxy. On exit, the old pod will be restored.- The
—-run
option tells Telepresence to run the Gebug tool along with our service and hook it up to the networking proxy.
As long as you leave the HTTP server running inside telepresence it will be accessible from inside the Kubernetes cluster.
It’s Show Time
Let’s see how can we grab a request that sent to our Kubernetes exposed endpoint, catch it using a breakpoint and return the result we want
That’s it. I hope you benefited and learned some new techniques to improve your micro-services debugging.
Feel free to contact me for any kind of question!
References:
[Gebug — http://github.com/moshebe/gebug]
[Telepresence — https://www.telepresence.io/tutorials/kubernetes]