Remote debug container in kubernetes using intellij

12/18/2018

I try to remote debug the application in attached mode with host: 192.168.99.100 and port 5005, but it tells me that it is unable to open the debugger port. The IP is 192.268.99.100 (the cluster is hosted locally via minikube).

Output of kubectl describe service catalogservice

Name:                     catalogservice
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=catalogservice
Type:                     NodePort
IP:                       10.98.238.198
Port:                     web  31003/TCP
TargetPort:               8080/TCP
NodePort:                 web  31003/TCP
Endpoints:                172.17.0.6:8080
Port:                     debug  5005/TCP
TargetPort:               5005/TCP
NodePort:                 debug  32003/TCP
Endpoints:                172.17.0.6:5005
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

This is the pods service.yml:

apiVersion: v1
kind: Service
metadata:
  name: catalogservice
spec:
  type: NodePort
  selector:
    app: catalogservice
  ports:
  - name: web
    protocol: TCP
    port: 31003
    nodePort: 31003
    targetPort: 8080
  - name: debug
    protocol: TCP 
    port: 5005
    nodePort: 32003
    targetPort: 5005

And in here I expose the containers port

spec:
  containers:
  - name: catalogservice
    image: elps/myimage
    ports:
    - containerPort: 8080
      name: app
    - containerPort: 5005
      name: debug

The way I build the image:

FROM openjdk:11
VOLUME /tmp
EXPOSE 8082
ADD /target/catalogservice-0.0.1-SNAPSHOT.jar catalogservice-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n", "-jar", "catalogservice-0.0.1-SNAPSHOT.jar"]

When I execute nmap -p 5005 192.168.99.100 I receive

PORT     STATE  SERVICE
5005/tcp closed avt-profile-2

When I execute nmap -p 32003 192.168.99.100 I receive

PORT     STATE  SERVICE
32003/tcp closed unknown

When I execute nmap -p 31003 192.168.99.100 I receive

PORT     STATE  SERVICE
31003/tcp open unknown

When I execute kubectl get services I receive

NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                          AGE
catalogservice    NodePort    10.108.195.102   <none>        31003:31003/TCP,5005:32003/TCP   14m

minikube service customerservice --url returns

http://192.168.99.100:32004
-- elp
docker
intellij-idea
kubernetes

2 Answers

12/18/2018

There was a slip in the yaml you first posted as:

    - containerPort: 5050
      name: debug

Should be:

    - containerPort: 5005
      name: debug

You also need to use the external port of 32003 when configuring the IntelliJ debugger. With those changes it should work.

You may also want to think about how to make it more flexible. In the past when I've done this I've used a different form for the docker start command that allows you to turn remote debug on and off by an environment variable called REMOTE_DEBUG, which for you would be:

CMD if [ "x$REMOTE_DEBUG" = "xfalse" ] ; then java $JAVA_OPTS -jar catalogservice-0.0.1-SNAPSHOT.jar ; else java $JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n -jar catalogservice-0.0.1-SNAPSHOT.jar ; fi

You'll probably find you want to set the env var $JAVA_OPTS to limit jvm memory use to avoid issues in k8s.

-- Ryan Dawson
Source: StackOverflow

2/13/2020

As an alternative to using a NodePort in a Service you could also use kubectl port-forward to access the debug port in your Pod.

kubectl port-forward allows using resource name, such as a pod name, to select a matching pod to port forward to since Kubernetes v1.10.

You need to expose the debug port in the Deployment yaml for the Pod

spec:
  containers:
    ...
    ports:
      ...
      - containerPort: 5005

Then get the name of your Pod via

kubectl get pods

and then add a port-forwarding to that Pod

kubectl port-forward podname 5005:5005

In IntelliJ you will be able to connect to

Host: localhost

Port: 5005

-- LazerBass
Source: StackOverflow