Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在docker容器中运行会没有attach的权限 #238

Closed
ycwdaaaa opened this issue Dec 12, 2019 · 15 comments
Closed

在docker容器中运行会没有attach的权限 #238

ycwdaaaa opened this issue Dec 12, 2019 · 15 comments
Assignees
Labels

Comments

@ycwdaaaa
Copy link

报错信息:permission denied, is not allow attach to 45.

用户:root

@oldmanpushcart
Copy link
Collaborator

能加上-X参数运行下,贴下输出信息么?

例如

./sandbox.sh -p 45 -X

@ycwdaaaa
Copy link
Author

image

@ycwdaaaa
Copy link
Author

你能看到图片么。 我这里看不到。 文字版的信息如下:
[root@task-controller-65fc689b58-qvjjq bin]# ./sandbox.sh -p 44 -X

  • getopts hp:vFfRu:a:A:d:m:I:P:ClSn:X ARG
  • reset_for_env
  • [[ -n /etc/alternatives/jre ]]
  • SANDBOX_JAVA_HOME=/etc/alternatives/jre
  • [[ -z /etc/alternatives/jre ]]
  • [[ -f /etc/alternatives/jre/lib/tools.jar ]]
    ++ cat /root/.sandbox.token
    cat: /root/.sandbox.token: No such file or directory
  • test -n '' -a -z ''
  • check_permission
  • pgrep java
  • grep 44
  • pgrep -U ''
  • grep 44

Usage:
pgrep [options]

Options:
-d, --delimiter specify output delimiter
-l, --list-name list PID and process name
-a, --list-full list PID and full command line
-v, --inverse negates the matching
-w, --lightweight list all TID
-c, --count count of matching processes
-f, --full use full process name to match
-g, --pgroup <PGID,...> match listed process group IDs
-G, --group <GID,...> match real group IDs
-n, --newest select most recently started
-o, --oldest select least recently started
-P, --parent <PPID,...> match only child processes of the given parent
-s, --session <SID,...> match session IDs
-t, --terminal <tty,...> match by controlling terminal
-u, --euid <ID,...> match by effective IDs
-U, --uid <ID,...> match by real IDs
-x, --exact match exactly with the command name
-F, --pidfile read PIDs from file
-L, --logpidfile fail if PID file is not locked
--ns match the processes that belong to the same
namespace as
--nslist <ns,...> list which namespaces will be considered for
the --ns option.
Available namespaces: ipc, mnt, net, pid, user, uts

-h, --help display this help and exit
-V, --version output version information and exit

For more details see pgrep(1).

  • exit_on_err 1 'permission denied, is not allow attach to 44.'
  • [[ -n permission denied, is not allow attach to 44. ]]
  • echo 'permission denied, is not allow attach to 44.'
    permission denied, is not allow attach to 44.
  • exit 1

@ycwdaaaa
Copy link
Author

这个是我们启动在k8s中的业务容器, 我已经修改了deployment,加入了securityContext. 如下:
securityContext:
capabilities:
add:
- SYS_PTRACE
- SYS_ADMIN

@oldmanpushcart
Copy link
Collaborator

oldmanpushcart commented Dec 12, 2019

明白了,pgrep命令出错了

问题出在了sandbox.sh脚本的[182-184]

# check attach
pgrep -U "${USER}" | grep "${TARGET_JVM_PID}" > /dev/null ||
  exit_on_err 1 "permission denied, ${USER} is not allow attach to ${TARGET_JVM_PID}."

问题的表象是sandbox.sh脚本获取不到环境变量${USER},期待应该是root,但实际是'',我对docker不熟,需要你再帮忙请教下:在docker中如何获得${USER}环境变量

@ycwdaaaa
Copy link
Author

执行whoami ?

@ycwdaaaa
Copy link
Author

我手动修改了你说的那段代码, 这块已经过去了。 但是现在又遇到了这个问题:
[root@task-controller-65fc689b58-qvjjq bin]# ./sandbox.sh -p 44 -X

  • getopts hp:vFfRu:a:A:d:m:I:P:ClSn:X ARG
  • reset_for_env
  • [[ -n /etc/alternatives/jre ]]
  • SANDBOX_JAVA_HOME=/etc/alternatives/jre
  • [[ -z /etc/alternatives/jre ]]
  • [[ -f /etc/alternatives/jre/lib/tools.jar ]]
    ++ cat /root/.sandbox.token
  • test -n '' -a -z ''
  • check_permission
  • pgrep java
  • grep 44
  • pgrep -U root
  • grep 44
  • [[ ! -w /root ]]
  • [[ ! -r /app/task-controller/sandbox/bin/../lib ]]
  • touch /root/.sandbox.token
  • [[ ! -x /etc/alternatives/jre ]]
  • [[ ! -x /etc/alternatives/jre/bin/java ]]
  • /etc/alternatives/jre/bin/java -version
  • awk -F '"' '/version/&&$2<="1.5"{exit 1}'
  • exit_on_err 1 'permission denied, please make sure target java process: 44 run in JDK[6,11]'
  • [[ -n permission denied, please make sure target java process: 44 run in JDK[6,11] ]]
  • echo 'permission denied, please make sure target java process: 44 run in JDK[6,11]'
    permission denied, please make sure target java process: 44 run in JDK[6,11]
  • exit 1

@oldmanpushcart
Copy link
Collaborator

oldmanpushcart commented Dec 12, 2019

jvm-sandbox需要跑在JDK环境下,JDK版本是[6,11]。从你的信息来看,你在docker中用的是JRE。更改为JDK就好了

@ycwdaaaa
Copy link
Author

我试过echo $USER 在docker里是空的, 所以还是执行woami才能拿到正确的用户名吧。 上面的错误我看提示是java 版本不对, 但是我们用的jdk 8

@oldmanpushcart
Copy link
Collaborator

我试过echo $USER 在docker里是空的, 所以还是执行woami才能拿到正确的用户名吧。 上面的错误我看提示是java 版本不对, 但是我们用的jdk 8

好吧,docker中挺诡异,$USER环境变量是标准应该要有。我在下个版本的sandbox.sh中加上这个容错

@ycwdaaaa
Copy link
Author

刚才那段检测我也绕过去了(其实就是删除了你脚本里的那个验证)。 但是现在又抛出一个诡异的事:
java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine
at com.alibaba.jvm.sandbox.core.CoreLauncher.attachAgent(CoreLauncher.java:59)
at com.alibaba.jvm.sandbox.core.CoreLauncher.(CoreLauncher.java:20)
at com.alibaba.jvm.sandbox.core.CoreLauncher.main(CoreLauncher.java:43)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
sandbox load jvm failed : com.sun.tools.attach.VirtualMachine

@oldmanpushcart
Copy link
Collaborator

刚才那段检测我也绕过去了(其实就是删除了你脚本里的那个验证)。 但是现在又抛出一个诡异的事:
java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine
at com.alibaba.jvm.sandbox.core.CoreLauncher.attachAgent(CoreLauncher.java:59)
at com.alibaba.jvm.sandbox.core.CoreLauncher.(CoreLauncher.java:20)
at com.alibaba.jvm.sandbox.core.CoreLauncher.main(CoreLauncher.java:43)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
sandbox load jvm failed : com.sun.tools.attach.VirtualMachine

这个你是绕不过去的,这个错误说的是:“你必须要用JDK,而不是JRE”

@ycwdaaaa
Copy link
Author

原来如此, 多谢~~ 我们业务容器我还不能随便改他的东西, 我看看怎么弄

@Charles-Kai
Copy link

Charles-Kai commented May 4, 2021

原来如此, 多谢~~ 我们业务容器我还不能随便改他的东西, 我看看怎么弄
你们是怎么解决的?
大公司一般不能改基础容器,绝大部分生产环境都是jre,所以线上测试环境也是jre。

@ycwdaaaa
Copy link
Author

ycwdaaaa commented May 5, 2021

原来如此, 多谢~~ 我们业务容器我还不能随便改他的东西, 我看看怎么弄
你们是怎么解决的?
大公司一般不能改基础容器,绝大部分生产环境都是jre,所以线上测试环境也是jre。

我们的产品部署在K8S中, 所以只需要向pod里注入一个side car 容器, 这个容器里有jvm-sandbox和jdk, 然后共享目录和进程名称空间就可以了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants