在 Kubernetes 环境中 debug Java 程序

服务端

Docker 镜像启动命令

1
CMD ["java", "-Dfile.encoding=utf-8", "-jar", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005", "/opt/app.jar"]

K8s pod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: oss
spec:
replicas: 1
template:
metadata:
labels:
titans: oss
spec:
containers:
- name: titans-oss
image: 172.31.21.226/isoftone/titans-oss
imagePullPolicy: Always
ports:
- containerPort: 8008
name: "web"
- containerPort: 5005
name: "jvm-debug"

启动以后查看 pod

1
2
3
$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
oss-d74785795-fb2h2 1/1 Running 0 15m 10.244.1.112 k8s-node-1

本地

连接

1
2
$ kubectl port-forward oss-d74785795-fb2h2 5005:5005
Forwarding from 127.0.0.1:5005 -> 5005

打开 IDEA,确保以下参数,然后 debug