Different ways & strategies to build and deploy Node.js for Production

PM2 Deployment

Before we start with more advanced deployment strategies, we will see the Pm2 deployment along with test coverages. In that way, it helps you to understand the entire flow of the strategy.

1module.exports \= {

2 apps : \[{

3 name: "app",

4 script: "./server.js",

5 instances: "max",

6 env: {

7 NODE\_ENV: "development",

8 },

9 env\_production: {

10 NODE\_ENV: "production",

11 }

12 }\]

13}
1"start": "pm2-runtime start ecosystem.config.js --env production",
Screenshot 2021-09-18 at 9.56.10 AM.png
Screenshot 2021-09-18 at 9.58.40 AM.png
Screenshot 2021-09-18 at 10.01.00 AM.png
Screenshot 2021-09-18 at 10.13.31 AM.png
Screenshot 2021-09-18 at 10.14.13 AM.png
1{

2 "environments": {

3 "test": {

4 "addons":\[

5 "Heroku-PostgreSQL:in-dyno"

6 \]

7 }

8 }

9 }
Screenshot 2021-09-18 at 10.19.31 AM.png

Docker Container Deployment

Let’s see how to deploy the Node.js application by containerizing it along with CI, CD pipeline. There are different ways to deploy docker containers to the production environment. For example, you can deploy to AWS on-premise or use managed services like AWS ECS to deploy and manage containers or use Hosting services like DigitalOcean, Heroku.

1FROM node:14\-alpine

2WORKDIR /usr/src/app

3COPY package.json ./

4COPY . /usr/src/app/

5RUN npm install \-\-production

6USER node

7EXPOSE 80

8

9CMD \["node","index.js"\]
1version: "3.6"

2services:

3 web:

4 build:

5 context: .

6 environment:

7 DATABASE\_URL: postgres://postgres:postgres@postgres:5432/postgres

8 depends\_on:

9 \- postgres

10 ports:

11 \- '8080:8080'

12 postgres:

13 image: postgres

14 restart: always

15 environment:

16 POSTGRES\_PASSWORD: postgres

17 ports:

18 \- '5432:5432'

19 volumes:

20 \- todo\-db:/var/lib/postgresql/data

21

22volumes:

23 todo\-db:
1\*\*/node\_modules/

2\*\*/.git

3\*\*/README.md

4\*\*/LICENSE

5\*\*/.vscode

6\*\*/npm\-debug.log

7\*\*/coverage

8\*\*/.env

9\*\*/.editorconfig

10\*\*/.aws

11\*\*/dist
Screenshot 2021-09-18 at 1.11.37 PM.png
1

2name: Deploy container to Heroku Server in Production

3

4

5on:

6

7 push:

8 branches: \[ docker\-aws \]

9 pull\_request:

10 branches: \[ docker\-aws \]

11

12

13 workflow\_dispatch:

14

15

16jobs:

17

18 build:

19

20 runs-on: ubuntu\-latest

21

22

23 steps:

24 \- name: Checkout Repo

25 uses: actions/checkout@v2

26 with:

27 ref : docker\-aws

28

29 \- name: Build, Push and Release a Docker container to Heroku

30 uses: gonuit/heroku\-docker\-deploy@v1.3.3

31 with:

32 email: ${{ secrets.HEROKU\_EMAIL }}

33 heroku\_api\_key: ${{ secrets.HEROKU\_API\_KEY }}

34 heroku\_app\_name: ${{ secrets.HEROKU\_APP\_NAME }}

35 dockerfile\_directory: ./

36 dockerfile\_name: Dockerfile

37 docker\_options: "--no-cache"

38 process\_type: web
1on:

2

3 push:

4 branches: \[ docker\-aws \]

5 pull\_request:

6 branches: \[ docker\-aws \]

7

8

9 workflow\_dispatch:
1steps:

2 \- name: Checkout Repo

3 uses: actions/checkout@v2

4 with:

5 ref : docker\-aws
1\- name: Build, Push and Release a Docker container to Heroku

2 uses: gonuit/heroku\-docker\-deploy@v1.3.3

3 with:

4 email: ${{ secrets.HEROKU\_EMAIL }}

5 heroku\_api\_key: ${{ secrets.HEROKU\_API\_KEY }}

6 heroku\_app\_name: ${{ secrets.HEROKU\_APP\_NAME }}

7 dockerfile\_directory: ./

8 dockerfile\_name: Dockerfile

9 docker\_options: "--no-cache"

10 process\_type: web
Screenshot 2021-09-18 at 5.41.38 PM.png
Screenshot 2021-09-18 at 5.44.58 PM.png

Kubernetes Deployment

So far, we have seen deploying Nodejs application using Pm2, Docker container. Now, we are going to see the enhanced version of Container Deployment. We are still going to deploy the docker container but in a different way.

1apiVersion: apps/v1

2kind: Deployment

3metadata:

4 name: node\-app

5spec:

6 replicas: 1

7 selector:

8 matchLabels:

9 app: node\-app

10 template:

11 metadata:

12 labels:

13 app: node\-app

14 spec:

15 containers:

16 \- name: node\-app

17 image: ganeshmani009/node\-deployment\-web

18 resources:

19 limits:

20 memory: "128Mi"

21 cpu: "500m"

22 ports:

23 \- containerPort: 8080

24 env:

25 \- name: DATABASE\_URL

26 value: postgres://postgres:postgres@postgres:5432/postgres

27 imagePullPolicy: Always
Kubernetes_Config.png
1apiVersion: v1

2kind: PersistentVolumeClaim

3metadata:

4 name: pg\-claim

5spec:

6 resources:

7 requests:

8 storage: 3Gi

9 accessModes:

10 \- ReadWriteOnce

11\---

12apiVersion: v1

13kind: Service

14metadata:

15 name: postgres

16spec:

17 selector:

18 app: postgres

19 ports:

20 \- port: 5432

21 targetPort: 5432

22\---

23apiVersion: apps/v1

24kind: Deployment

25metadata:

26 name: postgres

27spec:

28 selector:

29 matchLabels:

30 app: postgres

31 template:

32 metadata:

33 labels:

34 app: postgres

35 spec:

36 containers:

37 \- name: postgres

38 image: postgres:13.4\-alpine

39 resources:

40 limits:

41 memory: "128Mi"

42 cpu: "500m"

43 ports:

44 \- containerPort: 5432

45 env:

46 \- name: POSTGRES\_PASSWORD

47 value: postgres

48 volumeMounts:

49 \- mountPath: /data/db

50 name: storage

51 volumes:

52 \- name: storage

53 persistentVolumeClaim:

54 claimName: pg\-claim
1apiVersion: v1

2kind: PersistentVolumeClaim

3metadata:

4 name: pg\-claim

5spec:

6 resources:

7 requests:

8 storage: 3Gi

9 accessModes:

10 \- ReadWriteOnce
1kubectl apply \-f <Directory name of config \- Here's its infra\>
Screenshot 2021-09-18 at 6.21.32 PM.png

Want to stand out from the Crowd?

Don’t get stuck in the tutorial loop. Learn a technology by practicing real world scenarios and get a job like a boss. Subscribe and get the real world problem scenarios in your inbox for free

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store