DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Java Remote Debug for Applications Running in Kubernetes

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!

Christian Posta user avatar by
Christian Posta
·
Mar. 24, 16 · Tutorial
Like (1)
Save
Tweet
Share
11.01K Views

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

ceposta@postamac(~) $ kubectl get pod
NAME                        READY     STATUS    RESTARTS   AGE
broker-amq-1-hjbeh          1/1       Running   1          15h
file-ingress-events-3artj   1/1       Running   1          13h

Tail the logs

Now pick the pod you want to stream the logs from and go!

ceposta@postamac(~) $ kubectl logs -f file-ingress-events-3artj

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):

ceposta@postamac(~) $ kubectl exec -it file-ingress-events-3artj bash

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

Example:

# Set debug options if required
if [ x"${JAVA_ENABLE_DEBUG}" != x ] && [ "${JAVA_ENABLE_DEBUG}" != "false" ]; then
    java_debug_args="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=${JAVA_DEBUG_PORT:-5005}"
fi

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:

    spec:
        containers:
        - args: []
          command: []
          env:
          - name: "JAVA_ENABLE_DEBUG"
            value: "true"
          - name: "OUTGOING_FILE_PATH"
            value: "/deployments/camel/outgoing"
          - name: "INCOMING_FILE_PATH"
            value: "/deployments/camel/incoming"
          - name: "KUBERNETES_NAMESPACE"
            valueFrom:
              fieldRef:
                fieldPath: "metadata.namespace"
          image: "fabric8/file-ingress-events:1.0-SNAPSHOT"
          name: "file-ingress-events"
          ports:
          - containerPort: 5005
            name: "jvm-debug"
          - containerPort: 8778
            name: "jolokia"

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

ceposta@postamac(~) $ kubectl get pod
NAME                        READY     STATUS    RESTARTS   AGE
broker-amq-1-hjbeh          1/1       Running   1          15h
file-ingress-events-3artj   1/1       Running   1          13h

Proxy the pod to a specific port

ceposta@postamac(~) $ kubectl port-forward file-ingress-events-3artj  5005:5005

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 localhost:5005.

Hope this helps you debug your Java apps!


Kubernetes Java (programming language) Debug (command) application remote Docker (software)

Published at DZone with permission of Christian Posta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Ways That You Can Operate Record Beyond DTO [Video]
  • How To Avoid “Schema Drift”
  • Comparing Flutter vs. React Native
  • Hackerman [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: