how to deploy the dist folder of npm run build in kubernetes?

9/19/2019

I have a node.js application that I am trying to deploy to Kubernetes.To run this normally on my machine without using Kubernetes, i would run the commands npm install and npm run build and then serve the "dist" folder. Normally i would install npm's serve using "npm install -g serve" and then run "serve -s dist".This works fine.But now to deploy to Kubernetes for production how can I create my image?I mean how should the docker file for this look like?

Note: I don't want to use nginx, apache or any kind of web server.I want to do this using node/npm's server for serving the dist folder.Plz help

Dockerfile(What I have tried)

FROM node:8

WORKDIR /usr/src/app

COPY /dist

RUN npm install -g serve

serve -s dist

I am sure if this dockerfile is right.So i need guidance on how to correctly create image to serve dist folder of npm run build.Plz help?

-- Vignesh Swaminathan
docker
kubernetes
npm

1 Answer

9/20/2019

I think that you can find tons of tutorials in the globe about customers web applications integration in Kubernetes cluster and further exposing them to the service visitors.

Actually, application containerized in Docker environment has to be ported in the particular Image from Dockerfile or build up within Docker Compose tool in order to remain all the application’s service dependencies; when the image is ready, it can be stored in public DockerHub or in isolated Private registry, thus Kubernetes container runtime then pulls this image and creates appropriate workloads(Pods) within the cluster according to the declared resource model.

I would recommend the following scenario:

Build docker image from your initial Dockerfile (I've made some correction):

FROM node:8

WORKDIR /usr/src/app

COPY dist/ ./dist/

RUN npm install -g serve

$ sudo docker image build <PATH>

Create tag related to the source image:

$ sudo docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Export the image to DockerHub or some private registry:

$ sudo docker push [OPTIONS] NAME[:TAG]

Create relevant Kubernetes workload(Pod) and apply it in Kubernetes cluster, starting Node server inside the container, listening on 5000 port:

apiVersion: v1
kind: Pod
metadata:
  name: nodetest
  labels:
    node: test
spec:
  containers:
  - name: node-test
    image: TARGET_IMAGE[:TAG]
    ports:
    - containerPort: 5000
    command: [ "/bin/bash", "-ce", "serve -s dist" ]

If you consider exposing the application for external cluster clients, then look at NodePort service:

$ kubectl expose po nodetest --port=5000 --target-port=5000 --type=NodePort

Update_1:

The application service then might reachable on the host machine within some specific port, you can simply retrieve this port value:

kubectl get svc nodetest -o jsonpath='{.spec.ports[0].nodePort}'

Update_2:

In order to expose NodePort service on some desired port, just apply the following manifest, approaching 30000 port assignment:

apiVersion: v1
kind: Service
metadata:
  labels:
    node: test
  name: nodetest
spec:
  ports:
  - nodePort: 30000
    port: 5000
    protocol: TCP
    targetPort: 5000
  selector:
    node: test
  type: NodePort
-- mk_sta
Source: StackOverflow