Dolphin
Dolphin

Reputation: 38681

is it possible to remote debugging java program in kubernetes using service name

Now I am remote debugging my java program in kubernetes(v1.15.2) using kubectl proxy forward like this:

kubectl port-forward soa-report-analysis 5018:5018 -n dabai-fat

I could using intellij idea to remote connect my localhost port 5018 to remote debugging my pod in kubernetes cluster in remote datacenter,but now I am facing a problem is every time I must change the pod name to redebug after pod upgrade,any way to keep a stable channel for debugging?

Upvotes: 1

Views: 3205

Answers (4)

alex-kar
alex-kar

Reputation: 23

Suggested solutions either require exposing the debug port through a Service or running a script to find one of the Pod names to port-forward to.

I think the simplest solution is to run port-forward against the Deployment and let Kubernetes pick one of the Pods automatically.

kubectl port-forward deploy/<deployment_name> <port>

This is ideal for a Deployment with a ReplicaSet of 1 or when you don't need to debug a specific Pod but any available one.

Upvotes: 0

Rustam Lotsmanenko
Rustam Lotsmanenko

Reputation: 21

I could suggest for anyone who looks for ways to debug Java(and Go, NodeJS, Python, .NET Core) applications in Kubernetes to look at skaffold.
It simple CLI tool that uses already existing build and deploy configuration that you used to work with. There is no need for additional installation in the cluster, modification for existing deployment configuration, etc.
Install CLI: https://skaffold.dev/docs/install/
Open your project, and try:

skaffold init

This will make skaffold create

skaffold.yaml

(the only needed config file for skaffold)

And then

skaffold debug

Which will use your existing build and deploy config, to build a container and deploy it. If needed necessary arguments will be injected into the container, and port forwarding will start automatically.

For more info look at: https://skaffold.dev/docs/workflows/debug/

This can provide a consistent way to debug your application without having to be aware all time about the current pod or deployment state.

Upvotes: 2

Shivani
Shivani

Reputation: 76

We can use a service of type nodeport to resolve your issue.Here is a sample yaml file:-

apiVersion: v1
kind: Service
metadata:
  name: debug-service
spec:
  type: NodePort
  selector:
    app: demoapp
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 8001  // port which exposed in DockerFile for debugging purpose
      targetPort: 8001
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30019

In IntelliJ, you will be able to connect to

Host: localhost

Port: 30019

Upvotes: 2

Dolphin
Dolphin

Reputation: 38681

I use this script to improve my workflow:

#!/usr/bin/env bash


set -u

set -e

set -x

kubectl get pods -n dabai-fat | grep "soa-illidan-service"

POD=$(kubectl get pod -l k8s-app=soa-illidan-service -o jsonpath="{.items[0].metadata.name}")

kubectl port-forward ${POD} 11014:11014

This script automatic get the pod name and open remote debugging.

Upvotes: 1

Related Questions