【问题标题】:Remote debug container in kubernetes using intellij使用 intellij 在 kubernetes 中远程调试容器
【发布时间】:2021-04-14 04:41:57
【问题描述】:

我尝试使用主机192.168.99.100 和端口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>

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

【问题讨论】:

    标签: docker intellij-idea kubernetes


    【解决方案1】:

    作为在Service 中使用NodePort 的替代方法,您还可以使用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

    【讨论】:

      【解决方案2】:

      或者,您可以使用Cloud Code Intellij 插件。 此外,如果您使用Fabric8,它会提供fabric8:debug 目标。

      【讨论】:

      • Cloud Code IntelliJ 插件为我完成了这项工作。 Fabric8 库不是停产了吗?
      【解决方案3】:

      您第一次发布的 yaml 中有一个错误:

          - containerPort: 5050
            name: debug
      

      应该是:

          - containerPort: 5005
            name: debug
      

      在配置 IntelliJ 调试器时还需要​​使用32003 的外部端口。通过这些更改,它应该可以工作。

      您可能还想考虑如何使其更灵活。过去when I've done this 我使用了不同的form for the docker start command,它允许您通过名为REMOTE_DEBUG 的环境变量打开和关闭远程调试,对您而言,它是:

      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

      你可能会发现你想set the env var$JAVA_OPTS 来限制 jvm 内存使用以避免issues in k8s

      【讨论】:

      • 糟糕!还是不行,所以编辑了上面正确的端口。
      • @elp 我认为您正在尝试将调试器进程附加到错误的端口。 5005 是内部端口,但 intelliJ 调试器需要链接到外部端口,即 32003
      • 我也映射了32003,它也关闭了吗:|在上面附加了一些更多信息。我还尝试更改 intellij 5005 -> 32003 中的端口,但结果相同。
      • 您是否可以尝试仅使用docker run 启动该容器并公开端口(例如使用-p)并查看是否可以进行远程调试?只是为了缩小问题的程度。
      • 您需要允许调试代理从任何主机接受。指定地址为“address=*:5005”,否则只允许从127.0.0.1访问。完成此设置后,请尝试使用您指定的节点端口(来自 intellij 的 32003)远程连接到 pod。
      猜你喜欢
      • 2018-03-21
      • 2016-07-01
      • 2015-09-24
      • 2017-02-03
      • 1970-01-01
      • 2017-12-19
      • 2016-11-18
      • 2016-07-24
      相关资源
      最近更新 更多