我的Kubernetes集群中有一个容器,我想调试它。
但是没有netstat,没有ip,没有apk。
是否有一种方法来升级此映像,以便安装通用工具?
在本例中,它是K8s 1.23集群中的nginx容器映像。
发布于 2022-01-26 20:03:51
阿尔卑斯是一个精简版本的图像,以减少足迹。因此,这些工具的缺乏是意料之中的。尽管自库伯内斯 1.23以来,您可以使用kubectl debug命令将调试结束符附加到主题pod。语法:
kubectl debug -it <POD_TO_DEBUG> --image=ubuntu --target=<CONTAINER_TO_DEBUG> --share-processes示例:在下面的示例中,ubuntu容器连接到Nginx-高寒吊舱,需要进行调试。另外,请注意,ps -eaf输出显示nginx进程运行,cat /etc/os-release显示ubuntu运行。指示进程在两个容器之间共享/可见。
ps@kube-master:~$ kubectl debug -it nginx --image=ubuntu --target=nginx --share-processes
Targeting container "nginx". If you don't see processes from this container, the container runtime doesn't support this feature.
Defaulting debug container name to debugger-2pgtt.
If you don't see a command prompt, try pressing enter.
root@nginx:/# ps -eaf
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 19:50 ? 00:00:00 nginx: master process nginx -g daemon off;
101 33 1 0 19:50 ? 00:00:00 nginx: worker process
101 34 1 0 19:50 ? 00:00:00 nginx: worker process
101 35 1 0 19:50 ? 00:00:00 nginx: worker process
101 36 1 0 19:50 ? 00:00:00 nginx: worker process
root 248 0 1 20:00 pts/0 00:00:00 bash
root 258 248 0 20:00 pts/0 00:00:00 ps -eaf
root@nginx:/# 如图所示,调试ubuntu时,我们可以使用各种工具进行调试:
root@nginx:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
root@nginx:/# 如果您的集群中需要启用临时容器,那么您可以通过这里描述的功能门来启用它。
发布于 2022-01-26 19:00:40
使用容器的全部目的是优化集群中的资源利用率。所使用的图像应该只包括运行应用程序所需的包。
应该从图像(特别是prod中)删除不需要的包,以降低计算利用率和减少攻击向量。
这似乎是一个简化的映像,只有运行该应用程序所需的库。
为了进行调试,您必须在与您要调试的容器相同的pid和网络命名空间中创建一个新容器。
先构建容器
Dockerfile
FROM alpine
RUN apk update && apk add strace
CMD ["strace", "-p", "1"]构建
$ docker build -t strace .跑
docker run -t --pid=container:<targetContainer> \
--net=container:targetContainer \
--cap-add sys_admin \
--cap-add sys_ptrace \
strace
strace: Process 1 attached
futex(0xd72e90, FUTEX_WAIT, 0, NULLhttps://stackoverflow.com/questions/70868709
复制相似问题