Debugging Microservices Running on Kubernetes
Learn how to debug microservices locally on Kubernetes.
Join the DZone community and get the full member experience.Join For Free
in order to learn more about microservices, container orchestration, and service meshes, i’ve set up a local development environment with minikube, istio, and tools like kiali and created a sample application . in this article, i describe how to debug these services locally.
microservices can be developed in various languages and with multiple frameworks. for the different implementation types, there are specialized and established ides (integrated development environments) and tools to debug the single services.
the new challenge when debugging cloud-native applications is that applications often have many microservices and there is a lot of communication between them. testing and debugging only separate services is not sufficient.
here is a list of my debugging requirements. in the optimal case, i’d like to:
- run and debug my code in my favorite ide locally (rather than using a slow remote debugger)
- run the single service as part of the overall application, for example, invoke it from another service (rather than via unit tests and mock objects only)
- run the code in the same container locally as in production (rather than installing application servers locally)
- do changes in the code locally and test them immediately (rather than restarting containers)
there are several different ways to debug microservices. one great solution is to use telepresence , which is an open-source tool for the “fast, local development for kubernetes and openshift microservices.”
here is an example. i’m using a simple application that just has two microservices so far. the bff (backend for frontend) ‘web-api’ microservice invokes another ‘articles’ microservice. i want to debug the ‘articles’ service locally and let the ‘web-api’ service invoke it, which runs in a kubernetes cluster.
i’ve implemented these services with javaee, openj9, open liberty , and microprofile within eclipse. there is a lot of documentation available how to debug open liberty in eclipse via the open liberty tools . you can import the ‘articles’ service as existing maven project. in order to run the service in the local open liberty server, you need to check ‘dynamic web module’ in the ‘project facets’ settings. after this, you can start the local open liberty server.
kind: service apiversion: v1 metadata: name: articles labels: app: articles spec: selector: app: articles ports: - port: 9080 name: http type: nodeport --- kind: deployment apiversion: apps/v1beta1 metadata: name: articles spec: replicas: 1 template: metadata: labels: app: articles version: v1 spec: containers: - name: articles image: articles:1 ports: - containerport: 9080
now to the exciting part. telepresence allows you to swap a pod running in kubernetes with a service running locally. all requests to the pod in kubernetes are redirected to the local url and port.
after you have installed telepresense , the following command can be run:
once the kubernetes remote port 9080 has been mapped to the local port 9080, the locally running service can be invoked from the ‘web-api’ service running in kubernetes. the best part is you can also use the eclipse debugger as if everything would run locally.
calling from local services to kubernetes
with this easy setup, services in kubernetes can invoke local services. unfortunately, the other direction doesn’t work. for example, a locally running service ‘web-api’ cannot invoke the kubernetes service ‘articles’ via ‘ http://articles:9080 ’.
fortunately, telepresense comes with a mechanism to support this as well. essentially, you can run your code in a local docker container and debug it with a remote debugger. telepresense can replace the kubernetes pod with a two-way proxy that handles the traffic to your local docker container. there is even support for ‘hot code replace’ (see documentation).
i haven’t tried the two-way communication yet, but the functionality sounds very promising and would fulfill the requirements above!
Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.