Deploy nodeJS app to kubernetes cluster with helm
In this article, we attempt to deploy a nodeJS app to kubenetes cluster using helm.
I am using helm version 3.6.3 as shown below.
$ helm version
version.BuildInfo{Version:”v3.6.3", GitCommit:”d506314abfb5d21419df8c7e7e68012379db2354", GitTreeState:”clean”, GoVersion:”go1.16.5"}
- Create a nodeJS app
To demonstrate, i create a simple nodeJS app, as shown below.
2. Dockerise the nodeJS app
FROM node:16.13.0 AS builder
WORKDIR /dist
COPY ./package.json ./
COPY ./package-lock.json ./
RUN npm ci
COPY . .RUN npm run build
FROM node:16.13-alpine
WORKDIR /dist
The explanation of the Dockerfile is covered in here.
3. Setup a kubernetes cluster and connect to the cluster
We can use AWS EKS or Google GKE user interface to setup the kubernetes cluster. Alternatively, we can use terraform as well. Then we use the following command to connect our local environment to the cluster.
For AWS:
aws eks --region us-west-2 update-kubeconfig --name my-cluster
For GKE:
gcloud container clusters get-credentials my-cluster --zone us-east1 --project <proj_name>
4. Create helm chart
Run the command below to create helm chart files
helm create helmchart
5. Modify the helm chart files
In values.yaml, update the repository value:
image:
repository: victoryeo00/myprog
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: latest
and port value and type value:
service:
type: LoadBalancer
port: 4002
targetPort: 4002
In templates/service.yaml, update type and ports value
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: {{ .Values.service.targetPort }}
protocol: TCP
In templates/deployment.yaml, update container port value
ports:
- containerPort: 4002
6. Install the helm chart
Run the helm command below:
helm install myprog helmchart/
7. Check k8s pod and service status
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 172.20.0.1 <none> 443/TCP 24d
myprog LoadBalancer 172.20.242.168 a492a18.us-east-1.elb.amazonaws.com 4002:31599/TCP 15s
and
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
myprog-694c58864d-qmrms 1/1 Running 0 43s
8. Using curl command to test
In another terminal, run
$ curl http://a492a18.us-east 1.elb.amazonaws.com:4002/api/v1/healthz
healthy
That’s it. It works.
The helm chart files are available at:
https://github.com/victoryeo/myprog/tree/master/helmchart
Note:
In kubectl describe pod , i was getting this warning in pod (from kubectl get pod):
Readiness probe failed: Get “http://10.20.71.55:4002/api/v1/healthz": dial tcp 10.20.71.55:4002: connect: connection refused
To solve it, in templates/deployment.yaml, add initialDelaySeconds, update the path and port number.
livenessProbe:
httpGet:
path: /api/v1/healthz
port: 4002
initialDelaySeconds: 4readinessProbe:
httpGet:
path: /api/v1/healthz
port: 4002
initialDelaySeconds: 4