简体   繁体   English

如何在 Kubernetes Pod 中调试 nodeJS 应用程序?

[英]How to debug a nodeJS application inside a Kubernetes Pod?

Pretty much the title of the question.几乎是问题的标题。

I am working with an Ubuntu system that has a k8s deployment with multiple nodes and with multiple pods that run Docker containers.我正在使用一个 Ubuntu 系统,该系统具有带有多个节点和多个运行 Docker 容器的 pod 的 k8s 部署。 A few of the pods are nodeJS microservices which run the following command at initiation:一些 pod 是 nodeJS 微服务,它们在启动时运行以下命令:

node app.js

Sometimes I need to debug the microservice by adding logs, changing logic inside, etc.有时我需要通过添加日志、更改内部逻辑等来调试微服务。

Working with the same microservices in Windows I could just change the source code and restart the node.exe process.在 Windows 中使用相同的微服务,我只需更改源代码并重新启动node.exe进程。 How would I achieve doing the same in Linux with a Kubernetes deployment?我将如何通过 Kubernetes 部署在 Linux 中实现相同的功能?

I attempted to run a shell:我试图运行一个shell:

user@node1:~$ kubectl exec my-microservice-XXXX -it -- sh

Change source code and save: nano app.js更改源代码并保存: nano app.js

Find the node process: ps aux查找node进程: ps aux

PID   USER     TIME  COMMAND
    1 root      0:00 npm
   22 root      0:00 npm
   42 root      0:27 node --max-http-header-size=65000 app.js

Then send SIGTERM to PID 42 :然后将SIGTERM发送到PID 42

kill SIGTERM 42

And this results in me being booted out of the pod:这导致我被从 pod 中启动:

/usr/src/app # kill SIGTERM 42
sh: invalid number 'SIGTERM'
/usr/src/app # command terminated with exit code 137
test@node1:~$

And a new pod starts automatically:一个新的 pod 会自动启动:

my-microservice-XXXX                       0/1     Completed   1          19h
my-microservice-XXXX                       1/1     Running     2          19h

This is not possible in Kubernetes as a straight forward way, as we do not manage the container (Creation, termination, etc).这在Kubernetes是不可能的,因为我们不管理容器(创建、终止等)。 This is done by Kubernetes and hence the process is ephemeral.这是由Kubernetes完成的,因此这个过程是短暂的。

If you don't want to lose your changes on container restart, then you can use volume mount of the directory where you're making the changes.如果您不想在容器重新启动时丢失更改,那么您可以使用进行更改的目录的卷挂载 ( This completely defeats the purpose of docker containerisation and a not a recommended for production (any) environment to store the code in volume ). 这完全违背了 docker 容器化的目的,也不建议在生产(任何)环境中批量存储代码)。

With the following two steps, you can debug a Node app running inside a Docker container in a kubernetes Pod:通过以下两个步骤,您可以调试在 kubernetes Pod 中的 Docker 容器内运行的 Node 应用程序:

  1. Log into the container and run the Node app in the debug mode:登录容器并在调试模式下运行 Node 应用程序:
kubectl exec -it <pod-name> bash
node --inspect-brk index.js
  1. Forward connections to a local port to a port on the Pod将本地端口的连接转发到 Pod 上的端口
kubectl port-forward <pod-name> 9229

Note: 9229 is the default port number that the debugger listens on, and you don't need to expose this port in your Kubernetes configuration yaml file.注意: 9229是调试器侦听的默认端口号,您不需要在 Kubernetes 配置 yaml 文件中公开此端口。

That is it.这就对了。

Now you can open you Chrome browser with address chrome://inspect , click the remote target, and start debugging.现在您可以使用地址chrome://inspect打开 Chrome 浏览器,单击远程目标,并开始调试。

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

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