#3246·arthas

k8s 临时 debug 容器无法 attach 应用容器中的 Java 进程(跨 mount namespace)

Author: killingwolfCreated Jul 21, 2026Updated Jul 21, 2026

问题描述

使用 kubectl debug 的临时容器(ephemeral container)调试应用容器中的 Java 进程时,as.sh / arthas-boot attach 失败。

复现环境

  • 应用容器:普通用户启动的 Java 进程
  • debug 容器:root,与应用容器共享 pid namespace,但 mount namespace 相互独立
  • 执行 java -jar arthas-boot.jar 1

现象(依次出现)

  1. com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file /tmp/.java_pid1
  2. com.sun.tools.attach.AgentLoadException: Agent JAR not found or no Agent-Class attribute
  3. attach 显示成功,但 arthas-client connect 127.0.0.1 3658Connection refused

原因分析

临时容器与应用容器共享 pid namespace 但 mount namespace 独立,而 JDK 的 attach 机制默认双方在同一文件系统:

  • attach socket 建在目标容器自己的 tmpdir,发起方看不到;
  • loadAgent 让目标 JVM 打开的 agent/core jar 路径在目标侧不存在(跨 UID 时目标进程也无权读取发起方的 /proc/<pid>/root);
  • 即使递过去 agent/core 两个 jar,ArthasBootstrap 启动还需从 arthas home 读取 arthas-spy.jar(注入 bootstrap classloader,缺失即抛异常)等资源,资源不全导致 bind 失败,而 AgentBootstrap 会吞掉该异常,于是 loadAgent“成功”但 telnet 端口根本没监听。

期望

支持从 k8s 临时 debug 容器直接 attach 应用容器中的 Java 进程(已提交 PR 解决)。