How to debug java after it has been deployed to kubernetes cluster.
step1:
Add this two lines in the Dockerfile.(I use port 30055 here. You can use port between 30000-32767, you must set the same port number in deployment and service if you use service Type as NodePort. If you use LoadBalancer, you must set the same port number )
EXPOSE 8080 30055
ENV _JAVA_OPTIONS '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=30055'
Full Dockerfile.
FROM maven:3.5-jdk-8 as BUILD
#ADD repository.tar.gz /usr/share/maven/ref/
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN mvn -s /usr/share/maven/ref/settings-docker.xml package
FROM openjdk:8-jre
EXPOSE 8080 30055
COPY --from=BUILD /usr/src/app/target /opt/target
WORKDIR /opt/target
ENV _JAVA_OPTIONS '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=30055'
CMD ["java", "-jar", "greeting.war"]
step2:
Set the port number.
containerPort: 30055
port: 30055
targetPort: 30055
nodePort: 30055
Full yaml file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: greeting
spec:
replicas: 1
selector:
matchLabels:
app: greeting
template:
metadata:
labels:
app: greeting
spec:
containers:
- name: greeting
image: atjapan2015/studyk8s:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: "http"
- containerPort: 30055
name: "debug"
---
apiVersion: v1
kind: Service
metadata:
name: greeting
spec:
selector:
app: greeting
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8080
nodePort: 30080
- name: debug
protocol: TCP
port: 30055
targetPort: 30055
nodePort: 30055
type: NodePort
Step3:
Setup Eclipse remote debug as below. Then, click [Debug].
Step4:
Set break point in Eclipse and call you service, then Eclipse's debug will become active.
curl NODE=IP:30080/hello
---END---