首页 > 解决方案 > 使用intellij在kubernetes中远程调试容器

问题描述

我尝试使用 host:192.168.99.100和 port以附加模式远程调试应用程序5005,但它告诉我它是unable to open the debugger port. IP 是192.268.99.100(集群通过 minikube 在本地托管)。

的输出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>

这是 pod 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

在这里我暴露了容器端口

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

我构建图像的方式:

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"]

当我执行时,nmap -p 5005 192.168.99.100我收到

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

当我执行时,nmap -p 32003 192.168.99.100我收到

PORT     STATE  SERVICE
32003/tcp closed unknown

当我执行时,nmap -p 31003 192.168.99.100我收到

PORT     STATE  SERVICE
31003/tcp open unknown

当我执行时,kubectl get services我收到

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返回

http://192.168.99.100:32004

标签: dockerintellij-ideakubernetes

解决方案


作为NodePort在 a 中使用 a 的替代方法,Service您还可以使用它kubectl port-forward来访问Pod.

kubectl port-forward自 Kubernetes v1.10 起,允许使用资源名称(例如 pod 名称)来选择匹配的 pod 进行端口转发。

您需要在 Pod 的 Deployment yaml 中公开调试端口

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

然后通过以下方式获取您的 Pod 的名称

kubectl get pods

然后向该 Pod 添加端口转发

kubectl port-forward podname 5005:5005

在 IntelliJ 中,您将能够连接到

主持人:localhost

港口:5005


推荐阅读