<p>Pretty much the title of the question.</p>

<p>I am working with an Ubuntu system that has a k8s deployment with multiple nodes and with multiple pods that run Docker containers. A few of the pods are nodeJS microservices which run the following command at initiation:</p>

<p><code>node app.js</code></p>

<p>Sometimes I need to debug the microservice by adding logs, changing logic inside, etc.  </p>

<p>Working with the same microservices in Windows I could just change the source code and restart the <code>node.exe</code> process. How would I achieve doing the same in Linux with a Kubernetes deployment?</p>

<p>I attempted to run a shell:</p>

<p><code>user@node1:~$ kubectl exec my-microservice-XXXX -it -- sh</code></p>

<p>Change source code and save:
<code>nano app.js</code></p>

<p>Find the <code>node</code> process:
<code>ps aux</code></p>

<pre><code>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
</code></pre>

<p>Then send <code>SIGTERM</code> to <code>PID 42</code>:</p>

<p><code>kill SIGTERM 42</code></p>

<p>And this results in me being booted out of the pod:</p>

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

<p>And a new pod starts automatically:</p>

<pre><code>my-microservice-XXXX                       0/1     Completed   1          19h
my-microservice-XXXX                       1/1     Running     2          19h
</code></pre>

<p>With the following two steps, you can debug a Node app running inside a Docker container in a kubernetes Pod:</p>

<ol>
<li>Log into the container and run the Node app in the debug mode:</li>
</ol>

<pre><code>kubectl exec -it &lt;pod-name&gt; bash
node --inspect-brk index.js
</code></pre>

<ol start="2">
<li>Forward connections to a local port to a port on the Pod</li>
</ol>

<pre><code>kubectl port-forward &lt;pod-name&gt; 9229
</code></pre>

<p>Note: <code>9229</code> 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.</p>

<p>That is it.</p>

<p>Now you can open you Chrome browser with address <code>chrome://inspect</code>, click the remote target, and start debugging.</p>
