Java Remote Debug for Applications Running in Kubernetes
Kubernetes 1.2 is out, and what's more, it's seeing widespread adoption. Here's an overview of tailing logs, debugging your app, and more!
Join the DZone community and get the full member experience.Join For Free
Kubernetes 1.2 was just released and is quickly becoming the defacto cluster management solution for containers (Docker, Rocket, Hyper, etc). Check it out if you haven’t already – here are some interesting tidbits about the 1.2 release:
- Cluster can now scale to 30,000 containers per cluster
- Graceful shutdown of nodes, transitioning to other running nodes in the cluster
- Custom defined metrics as the basis for autoscaling
- Dynamic configuration management
When you’re developing microservices on your local laptop, you can use something like Kubernetes to run you docker containers locally and get developer/QA/production likeness in how you deploy your applications. For example, you get process isolation, port-space isolation, network/storage, etc so things don’t collide on your local developer laptop.
One thing for Java Developers that comes up is how do you view logs, do remote debug, and take stack traces.
Here are a few tips:
Tailing Logs for Your Pod
On some cluster management systems, you have to basically look up the local IP of your application (if running in a container), ssh into it somehow, and then find the log and tail it. With Kubernetes, you don’t have to do any of that. Regardless of which machine you’re running on (ie, where you run the kubernetes client), you can do the following:
List the pods in your cluster
Tail the logs
Now pick the pod you want to stream the logs from and go!
Connect via Shell if you must
If you must log into the pod for some reason (poke around the file system, grok other config files, etc):
JVM Remote Debug Your Application
This becomes really handy to see exactly what’s going on in your application. To do this, you don’t really do anything different from what you do today. When you bootstrap your JVM, you should have a way to enable JVM debug. For example, for the HawtApp Maven plugin which is a simple mvn plugin that assigns a Java Main as the executable and a simple, flexible bootstrap
bin/run.sh script (or batch file for windows) that allows you to control classpath and debugging via environment variables.
Bootstrap Java app to be able to expose remote debug port
Define debug port in docker container via kubernetes manifest
Now you need to expose port
5005 (in this example) in your Docker containers via your Kubernetes manifest (json/yaml) file:
Note, we’ve also added an env variable in the kubernetes manifest file to be able to control whether we want remote debugging on or off (true/false). The bootstrap scripts (above) will check that env variable and you can control it via the kube manifest (now with ConfigMap in Kube 1.2, or OpenShift templates).
The last step is to proxy the debug port to your local machine. If you run the
kubectl client locally, this is easy:
List the pods in your cluster
Proxy the pod to a specific port
The above will port-forward from your local environment (5005) to the pod’s port 5005. Now you can just attach your remote debugger to
Hope this helps you debug your Java apps!
Published at DZone with permission of Christian Posta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.