Ephemeral Containers(临时容器)

临时容器与其他容器的不同之处在于,临时容器是被临时添加到 Pod 上,用于在线调试应用的,它永远不会自动重启,因此不适用于构建应用程序

因为临时容器是为了调试程序而设计的,所以在添加临时容器时,最好使用一个包含所有常用工具的镜像进行创建。

业务容器崩溃容器镜像不包含调试工具而导致 kubectl exec 不可用时,临时容器对于交互式故障排查和在线 Debug 很有用。尤其是在使用像不包含任何 shell 和其他工具的 distroless 镜像作为基础镜像时,虽然可以减少攻击面和漏洞,但是对于问题的排查会变得尤为棘手,此时临时容器就可以发挥很大的作用,带来诸多便利性。

使用临时容器来调试的例子

首先,像示例一样创建一个 pod:

$ kubectl run ephemeral-demo \
--image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 --restart=Never
pod/ephemeral-demo created

如果尝试使用 kubectl exec 来创建一个 shell,将会看到一个错误,因为这个容器镜像中没有 shell:

$ kubectl exec -it ephemeral-demo -- sh
error: Internal error occurred: error executing command in container: failed to exec in container: failed to start exec "1993bffac1b845330114c8a71d16ee918b4586fbd7ad36bd5e6a844b4bdcdc0d": OCI runtime exec failed: exec failed: unable to start container process: exec: "sh": executable file not found in $PATH: unknown

使用 kubectl debug 添加调试容器。如果指定 -i 或者 --interactive 参数,kubectl 将自动挂接到临时容器的控制台:

$ kubectl debug -it ephemeral-demo --image=centos:6 --target=ephemeral-demo
Targeting container "ephemeral-demo". If you don't see processes from this container it may be because the container runtime doesn't 
support this feature.
Defaulting debug container name to debugger-mmn8j.
If you don't see a command prompt, try pressing enter.
[root@ephemeral-demo /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 03:10 ?        00:00:00 /pause
root         18      0  0 03:12 pts/0    00:00:00 /bin/bash
root         31     18  0 03:12 pts/0    00:00:00 ps -ef

最后更新于

这有帮助吗?