Node.js Development Workflow for Kubernetes - Debugging (Part 2)

Kentaro Wakayama
May 24, 2018 · 3 min read

Minikube is a single node Kubernetes cluster which runs on your laptop in a virtual machine. It provides the possibility to run applications in a production-like environment and avoids the need to use Docker Compose for local development.

In this article series we will demonstrate how to run, debug and set up live reload for your containerized Node.js application in Kubernetes.

You can find the code which we use in this article on GitHub: https://github.com/coder-society/node-minikube-workflow

Debugging

Clone the repository and cd into the 2-debugging directory:

$ git clone https://github.com/coder-society/node-minikube-workflow.git
$ cd ../2-debugging

You can see in the Dockerfile that we start the Node.js app in debug mode. We specified the host IP0.0.0.0 and debug port 5858 to which we attach the debugger.

FROM node:8-alpineWORKDIR /srcCOPY package.json /src/
COPY package-lock.json /src/
RUN npm installADD . /srcEXPOSE 3000
EXPOSE 5858
CMD ["node", "--inspect=0.0.0.0:5858", "index.js"]

We use Visual Code which provides a great debugging experience for Node.js. In thelaunch.json file, we specified the debug configuration:

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Remote",
"address": "192.168.99.100",
"port": 30886,
"localRoot": "${workspaceRoot}",
"remoteRoot": "/src"
}
]
}

The address value in the configuration is the Minikube IP. You can use minikube ip to retrieve it:

$ minikube ip
192.168.99.100

The remoteRoot value in the configuration is the directory path inside the docker container.

We specified the nodePort values to prevent Minikube to map the exposed ports randomly.

apiVersion: v1
kind: Service
metadata:
name: node-api
spec:
ports:
- name: http
protocol: TCP
port: 80
nodePort: 30001
targetPort: http
- name: debug
protocol: TCP
port: 5858
nodePort: 30002
targetPort: debug
selector:
app: node-api
type: NodePort

Run make create to initialize the application in Minikube.

$ make create
Sending build context to Docker daemon 136.2kB
Step 1 : FROM node:8-alpine
---> 016382f39a51
Step 2 : WORKDIR /src
---> Using cache
---> 60edc3af49e2
Step 3 : COPY package.json /src/
---> Using cache
---> 2f883f662b17
Step 4 : COPY package-lock.json /src/
---> Using cache
---> b546a008ec1c
Step 5 : RUN npm install
---> Using cache
---> 764610b6a88f
Step 6 : ADD . /src
---> fe76275b7e4a
Removing intermediate container b5c78d72e01d
Step 7 : EXPOSE 3000
---> Running in 132ccdbe05b8
---> ffa44f3adc78
Removing intermediate container 132ccdbe05b8
Step 8 : EXPOSE 5858
---> Running in 74424a5840f6
---> 10a8dc073af0
Removing intermediate container 74424a5840f6
Step 9 : CMD node --inspect=0.0.0.0:5858 index.js
---> Running in 152550ff1d38
---> 772501131850
Removing intermediate container 152550ff1d38
Successfully built 772501131850
kubectl apply -f kubernetes/
deployment "node-api" created
service "node-api" created

Press F5 to start the debugger in Visual Code and run minikube service node-api --url to retrieve the exposed URLs of the Kubernetes service:

$ minikube service node-api --url
http://192.168.99.100:30001
http://192.168.99.100:30002

Two URLs are returned. One for accessing the API on port 30001 and one with the port 30002 which we use for debugging.

Make a curl request to verify the API:

$ curl http://192.168.99.100:30002
Hello World

Now make a breakpoint in the route handler and make another request:

The app stops at the breakpoint and you can inspect the request options.


In the next article we will show you how to set up live reload for your Node.js application in Minikube.


Learn more about Coder Society, a network of cutting edge developers, designers and product engineers at https://codersociety.com

Coder Society

An exclusive network of expert developers, designers and product managers.

Kentaro Wakayama

Written by

Software Engineer at Coder Society. https://codersociety.com

Coder Society

An exclusive network of expert developers, designers and product managers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade