Remote debug container in kubernetes using intellij
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
Alternatively, you can use the Cloud Code Intellij plugin.Also, if you use Fabric8, it provides the fabric8:debug
goal.
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.