How to remote debug an application hosted in Azure K8s Cluster

4/10/2020

Basic background

My Application is a Java application, My application is getting deployed in Azure cluster. everything is good. But I want to know how to connect Eclipse debugger with the application running in Azure cluster.

In the startup script, JPDA port is bind with 8000 and in dockerfile 8000 port is exposed.

The issue is how to connect eclipse debugger with code running in Azure cluster.

I tried to put the IP address in Remote Java Application connection properties Host: but not a success.

Need procedure, for remote debugging.

-- Dupinder Singh
azure-devops
jpda
kubernetes
portforwarding
remote-debugging

1 Answer

4/10/2020

So after googling, I found one ninja technique . A technique we know as port-forwarding.

So basic idea is to forward a running application's port to our local system's available port.

So I found one command for port-forwarding:

kubectl port-forward pods/<podName> 8000:8000 -n <namespace>

In this command, we need to know . For this, we need to know running pods in Kubernetes cluster on Azure. It means we need to connect or authenticate your local machine CLI with Azure.

Download Azure CLI from this link and Install https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest

  • Now open PowerShell run command

    az login

  • Your default browser will open add your Azure credentials and Authenticate, so your PowerShell will show you following message when your authentication is done.

`C:\Users\MachineName> az login You have logged in. Now let us find all the subscriptions to which you have access...

{ "cloudName": "", "id": "", "isDefault": true, "name": "", "state": "Enabled", "user": { "name": "", "type": "" } } `

  • Now next command to run is:

    az aks get-credentials --resource-group <ResourseGroupName> --name <Name of Kubernetes cluster>

  • Run Command to get running pods in a specific namespace if defined any.

    kubectl get pods -n <namespace>

  • Now you will have your running pods in specific namespace in Kubernetes cluster of Azure cloud.

NAME                         READY STATUS  RESTARTS    AGE

application-8664866df5-x4zns 2/2           Running 0   21m
  • Time to run our initial command.

    kubectl port-forward pods/<application-8664866df5-x4zns> 6000:8000 -n myNameSpace

  • In the cli you will see

Forwarding from 127.0.0.1:6000 -> 8000
Forwarding from [::1]:6000 -> 8000

Might be confused why I use 6000 port, because, my 8000 port is already in use.

Time to connect with eclipse: Project Right-click > debug > debug configuration > search for Remote Java Application.

Set a name for the debugger like mine is debugCluster Host: 127.0.0.1 Port: 6000

Now Apply and Press Debug button in some time you will see your debugger is connected with Instance running in Azure cluster.

enter image description here

-- Dupinder Singh
Source: StackOverflow