Prepare the docker image
为了调试Java应用程序,我们需要向远程应用程序添加一些命令行参数。
- 在kubernetes部署有效负载中设置参数
DEBUG_OPTIONS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
- 将其传递给Dockerfile中的Java命令
ENTRYPOINT [ "sh", "-c", "java $DEBUG_OPTIONS -jar app.jar" ]
Expose the port 5005 in the GKE service
In GCP console, go to Kubernetes Engine -> Service & Ingress, choose your service and add the debug port:
ports:
- name: http
nodePort: 30060
port: 80
protocol: TCP
targetPort: 8080
- name: debug
nodePort: 30080
port: 5005
protocol: TCP
targetPort: 5005
Forward the pods ports to your localhost:5005
gcloud container clusters get-credentials ${CLUSTER_NAME} --region us-central1 --project ${PROJECT_NAME} \
&& kubectl port-forward --namespace ${NAMESPACE} $(kubectl get pod --namespace ${NAMESPACE} --selector="app=${APP}" --output jsonpath='{.items[0].metadata.name}') 5005:5005
您可以通过点击调试端口附近的“端口转发”按钮,从Google控制台生成此命令: