Setting up a remote debugging for Java microservices running inside Kubernetes pods

Step 1: Configuring your Java application for a remote debugging

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-service-deployment
namespace: my-namespace
spec:
selector:
matchLabels:
app: my-service
template:
metadata:
labels:
app: my-service
spec:
...
env:
- name: JAVA_TOOL_OPTIONS
value: "-Xdebug -agentlib:jdwp=transport=dt_socket,address=0.0.0.0:5005,server=y,suspend=n"
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-service-deployment
namespace: my-namespace
spec:
selector:
matchLabels:
app: my-service
template:
metadata:
labels:
app: my-service
spec:
...
env:
- name: JAVA_TOOL_OPTIONS
value: "{{.Values.jvmParams}}"

Step 2: Exposing remote debugging Kubernetes endpoint

kind: Service
apiVersion: v1
metadata:
name: my-service-remote-debugging
namespace: my-namespace
spec:
selector:
app: my-service
ports:
- protocol: TCP
name: tcp
targetPort: 5005
nodePort: 32081
type: NodePort

Step 3: Attaching IntelliJ to remotely running Java application

Connected to the target VM, address: ‘<your-node-ip>:32081’, transport: ‘socket’

Remote Debugging at scale

kind: Service
apiVersion: v1
metadata:
name: my-service-remote-debugging
namespace: my-namespace
spec:
selector:
remote-debugging: true
ports:
- protocol: TCP
name: tcp
targetPort: 5005
CoreV1Api api = ...;V1PodList podsList = api.listNamespacedPod(namespace,
"true",
null,
null,
null,
null,
null,
null,
null,
null,
null);
// show developer all pods from podsList.getItems()
public void setPodLabel(String namespace, String podName, String key, String value) throws ApiException, IOException {

String patchValue = "[{\"op\":\"replace\",\"path\":\"/metadata/labels/" + key + "\", \"value\": \"" + value + "\" }]";

V1Patch patch = new V1Patch(patchValue);

this.api.patchNamespacedPod(podName,
namespace,
patch,
null,
null,
null,
null);
}
//for every running pod, alter remote debugging label to "false":for (V1Pod pod : podsList.getItems()){this.setPodLabel(namespace, pod.getMetadata().getName(), "remote-debugging", "false");
}
//now set remote debugging label for target pod:V1Pod targetPod = ...;
this.setPodLabel(namespace, targetPod.getMetadata().getName(), "remote-debugging", "true");

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store