如何使用 kubernetes 和 skaffold 调试 python 代码?

How do you debug python code with kubernetes and skaffold?

我目前 运行 通过 kubernetes skaffold dev 在 python3 下的 django 应用程序。我使用 Python 源代码进行热重载。目前是否可以在 kubernetes 上使用 python 进行交互式调试?

例如,

def index(request):
    import pdb; pdb.set_trace()
    return render(request, 'index.html', {})

通常,在容器外,点击终点将使我掉入 (pdb) shell。

在当前设置中,我在 Deployment 文件中将 stdintty 设置为 true。代码确实在断点处停止,但它不允许我访问 (pdb) shell.

有一个 kubectl 命令允许您 attach 到 pod 中的 运行 容器:

kubectl attach <pod-name> -c <container-name> [-n namespace] -i -t

-i  (default:false) Pass stdin to the container
-t  (default:false) Stdin is a TTY

它应该允许您与容器中的调试器进行交互。 可能您可能需要调整您的 pod 以使用调试器,因此以下文章可能会有所帮助:

还有 telepresence 工具可以帮助您使用不同的应用程序调试方法:

Using telepresence allows you to use custom tools, such as a debugger and IDE, for a local service and provides the service full access to ConfigMap, secrets, and the services running on the remote cluster.

Use the --swap-deployment option to swap an existing deployment with the Telepresence proxy. Swapping allows you to run a service locally and connect to the remote Kubernetes cluster. The services in the remote cluster can now access the locally running instance.

可能值得研究 Rookout,它允许在 Kubernetes pods 上对 Python 进行生产中实时调试,而无需重新启动或重新部署。你失去了路径强制等,但你获得了负载的灵活性,可以有效地动态模拟断点类型的堆栈跟踪。

这不使用 Skaffold,但您可以使用 an open source project I wrote.

将 VSCode 调试器附加到任何 运行 Python pod

在您的集群上安装它涉及一些设置,但安装后您可以使用一个命令调试任何 pod:

robusta playbooks trigger python_debugger name=myapp namespace=default 

您可以看一下 okteto/okteto. There's a good tutorial,其中介绍了如何直接在 Kubernetes 上进行开发和调试。