在 K8s 中调试 Alpine 镜像:没有 `netstat`、没有 `ip`、没有 `apk`

gue*_*tli 2 kubernetes alpine-linux container-image

我的 Kubernetes 集群中有一个容器我想要调试。

但没有netstat,没有ip,没有apk

有没有办法升级这个镜像,以便安装常用工具?

在本例中,它是 K8s 1.23 集群中的 nginx 容器映像。

P..*_*... 9

Alpine 是该图像的精简版本,以减少占地面积。因此,预计缺乏这些工具。尽管从Kubernetes 1.23开始,您可以使用该kubectl debug命令将调试 pod 附加到主题 pod。句法:

kubectl debug -it <POD_TO_DEBUG> --image=ubuntu --target=<CONTAINER_TO_DEBUG> --share-processes
Run Code Online (Sandbox Code Playgroud)

示例:在下面的示例中,ubuntu容器附加到 Nginx-alpine pod,需要调试。另请注意,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:/# 
Run Code Online (Sandbox Code Playgroud)

正如此处所示,以 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:/# 
Run Code Online (Sandbox Code Playgroud)

如果需要在集群中启用临时容器,那么您可以通过此处所述的功能门来启用它。

  • @RickRackow 现在它被接受并且得到了赏金 (2认同)