简体   繁体   English

你如何使用 kube.netes 和 skaffold 调试 python 代码?

[英]How do you debug python code with kubernetes and skaffold?

I am currently running a django app under python3 through kube.netes by going through skaffold dev .我目前通过skaffold dev通过 kube.netes 在 python3 下运行一个 django 应用程序。 I have hot reload working with the Python source code.我使用 Python 源代码进行热重载。 Is it currently possible to do interactive debugging with python on kube.netes?目前是否可以在 kube.netes 上使用 python 进行交互式调试?

For example,例如,

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

Usually, outside a container, hitting the endpoint will drop me in the (pdb) shell.通常,在容器外,点击端点将使我进入(pdb) shell。

In the current setup, I have set stdin and tty to true in the Deployment file.在当前设置中,我在Deployment文件中将stdintty设置为true The code does stop at the breakpoint but it doesn't give me access to the (pdb) shell.代码确实在断点处停止,但它不允许我访问(pdb) shell。

There is a kubectl command that allows you to attach to a running container in a pod: 有一个kubectl命令,允许您附加到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

It should allow you to interact with the debugger in the container. 它应该允许您与容器中的调试器进行交互。 Probably you may need to adjust your pod to use a debugger, so the following article might be helpful: 可能您可能需要调整pod以使用调试器,因此以下文章可能会有所帮助:

There is also telepresence tool that helps you to use different approach of application debugging: 还有远程呈现工具可以帮助您使用不同的应用程序调试方法:

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. 使用远程呈现 ,您可以为本地服务使用自定义工具(如调试器和IDE),并为服务提供对ConfigMap,机密和远程群集上运行的服务的完全访问权限。

Use the --swap-deployment option to swap an existing deployment with the Telepresence proxy. 使用--swap-deployment选项将现有部署与Telepresence代理交换。 Swapping allows you to run a service locally and connect to the remote Kubernetes cluster. 交换允许您在本地运行服务并连接到远程Kubernetes集群。 The services in the remote cluster can now access the locally running instance. 远程集群中的服务现在可以访问本地运行的实例。

It might be worth looking into Rookout which allows in-prod live debugging of Python on Kubernetes pods without restarts or redeploys. 可能值得研究Rookout ,它允许在Kubernetes pod上对Python进行in-prod实时调试,而无需重新启动或重新部署。 You lose path-forcing etc but you gain loads of flexibility for effectively simulating breakpoint-type stack traces on the fly. 您将失去路径强制等,但您可以获得足够的灵活性,以便有效地模拟断点类型的堆栈跟踪。

This doesn't use Skaffold, but you can attach the VSCode debugger to any running Python pod with an open source project I wrote .这不使用 Skaffold,但您可以使用我编写的开源项目将 VSCode 调试器附加到任何正在运行的 Python pod。

There is some setup involved to install it on your cluster, but after installation you can debug any pod with one command:在集群上安装它需要一些设置,但安装后你可以使用一个命令调试任何 pod:

robusta playbooks trigger python_debugger name=myapp namespace=default 

You can take a look at okteto/okteto .你可以看看okteto/okteto There's a good tutorial which explains how you can develop and debug directly on Kube.netes.有一个很好的教程解释了如何直接在 Kube.netes 上进行开发和调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM