如果我做
\nPOD=$($KUBECTL get pod -lsvc=app,env=production -o jsonpath="{.items[0].metadata.name}")\nkubectl debug -it --image=mpen/tinker "$POD" -- zsh -i\n
Run Code Online (Sandbox Code Playgroud)\n我可以进入在我的 pod 内运行的 shell,但我想访问我称为“php”的容器的文件系统。我认为这应该位于/proc/1/root/app
但该目录不存在。作为参考,我的 Dockerfile 有:
WORKDIR /app\nCOPY . .\n
Run Code Online (Sandbox Code Playgroud)\n所以所有的文件都应该在根/app
目录中。
如果我添加,--target=php
那么我会被拒绝:
\xe2\x9d\xaf cd /proc/1/root\ncd: permission denied: /proc/1/root\n
Run Code Online (Sandbox Code Playgroud)\n我如何访问这些文件?
\ntlw*_*tec 13
调试容器始终有自己的文件系统,但是 - 如果容器运行时支持它 - 您应该能够通过很好地使用--target
和访问它。/proc/$PID/root
只需扮演完全相同的用户即可(两者$UID
和$GID
必须匹配)。
首先准备一个需要调试的容器:
kubectl run tested-pod --rm -it --image=busybox:latest --restart=Never \
--overrides='
{"spec": {
"containers": [
{
"name":"tested-pod",
"image":"busybox:latest",
"stdin":true,
"tty":true,
"securityContext": {
"runAsUser":1234,
"runAsGroup":4321
}
}
]
}
}' \
-- sh
Run Code Online (Sandbox Code Playgroud)
这将创建一个新的 pod tested-pod
,其中容器使用UID=1234
和运行GID=4321
,并为您提供一个交互式 shell。向容器的文件系统写入一些内容,以便您稍后可以看到它:
touch /tmp/tested-pod-file
ls /tmp
# tested-pod-file
Run Code Online (Sandbox Code Playgroud)
接下来,打开一个新终端并使用临时容器debug
:
kubectl debug tested-pod -it --image=busybox:latest --target=tested-pod -- sh
Run Code Online (Sandbox Code Playgroud)
此时调试用户是root
,无法访问FS,但我们已经可以看到容器的进程:
id
# uid=0(root) gid=0(root) groups=0(root),10(wheel)
ls /proc/1/root
# ls: /proc/1/root: Permission denied
ps -o 'pid,user,group,comm'
# PID USER GROUP COMMAND
# 1 1234 4321 sh
# 9 root root sh
# 25 root root ps
Run Code Online (Sandbox Code Playgroud)
现在您必须创建一个本地组和用户(名称并不重要)并使用它:
addgroup -g 4321 karel
adduser -HD -u 1234 -G karel karel
su karel
Run Code Online (Sandbox Code Playgroud)
在生成的 shell 中,您应该能够访问文件系统,甚至可以进行写入(至少在我的情况下有效):
ls /proc/1/root/tmp/
# tested-pod-file
touch /proc/1/root/tmp/debugger-file
ls /proc/1/root/tmp/
# debugger-file tested-pod-file
Run Code Online (Sandbox Code Playgroud)
您可以从原始容器的 shell 验证这是否有效:
ls /tmp
debugger-file tested-pod-file
Run Code Online (Sandbox Code Playgroud)
通读文档,使用kubectl debug
不会让您访问另一个容器中的文件系统。
最简单的选项可能是kubectl exec
在现有容器内启动 shell。在某些情况下,这不是一个选项(例如,某些容器仅包含单个二进制文件,并且没有 shell 或其他可用的常用实用程序),但 php 容器通常具有完整的文件系统。
在这种情况下,您可以简单地:
kubectl exec -it $POD -- sh
Run Code Online (Sandbox Code Playgroud)
您可以替换sh
为bash
或zsh
取决于现有映像中可用的 shell。
链接的文档提供了其他几个调试选项,但都涉及对pod副本进行操作。
归档时间: |
|
查看次数: |
4402 次 |
最近记录: |