Introduction
NodeJS is an open-source server environment, Node.js can be used on Windows, Linux, Unix, and macOS because it is cross-platform. A back-end JavaScript runtime environment is Node.js. Node.js executes JavaScript code outside of a web browser on the V8 JavaScript Engine.
Kubernetes is a powerful Platform as a Service that enables you to manage a cluster of nodes that run the Linux operating system. Kubernetes can be used to manage a variety of applications, including web applications, database and more.
Details about the Kubernetes components
- API Server – Intract with kubectl command utility, Primary component, Authorization work.
- Controller Manager – Replica controller, Node Controller maintainer the define pods and nodes.
- Scheduler – Matain proper balacing of hardware utilization and assign new pods to sutable workder node as per ranking.
- ECTD – Database its store the data using key and value form format.
- Kubelet – Pod moniotr, Pod creation and deletation.
- Kube-proxy – Pods communication between 2 nodes, Network related rules manages by kube-proxy.
In this post, We will deploy NodeJS app on Kubernetes cluster.
Step 1: Write a Deployment YAML
To deploy the nodeJS docker container in K8s, you can use the provided code by creating a deployment YAML file with the name node-deployment.yaml
.
vim node-deployment.yaml
and paste the following code.
apiVersion: apps/v1
kind: Deployment
metadata:
name: node
labels:
app: node
spec:
replicas: 1
selector:
matchLabels:
app: node
template:
metadata:
labels:
app: node
spec:
containers:
- name: node
image: node:latest
ports:
- containerPort: 3000
Take a look at the above YaML configuration:
apiVersion: Define the Kubernetes API in order to create objects.
kind: specify the type of object you intend to create.
metadata: define the object name ( including a name string, uniquely identifying the object, the UID, and an optional namespace ).
spec: Define the state of objects.
selector: specify the mapping between deployment and managed pods.
We need to save and exit from text editor, Our deployment YAML is ready Lets deploy the nodeJS pod on K8s cluster.
Step 2: Create a Deployment
We need to execute the following command to deploy the nodeJS pods which is container node image.
kubectl create -f node-deployment.yaml
You will get success message for the nodejs deployment on your screen.
We will get a single pod of the node image and, according to the Dockerfile configuration, NodeJS application will be up and running on port 3000.
Step 3: Let’s Verify the Deployment
We need to execute the given command to see pods and running or not.
To Validate the pod.
kubectl get pods
To check deployment.
kubectl get deployment
Step 4: Create a Service
We need to create a node-service.yaml
file in order access tomcat server, Create a node-service.yaml
file with following code.
To create a service.yaml file
vim node-service.yaml
Paste the following code.
apiVersion: v1
kind: Service
metadata:
name: node-service
spec:
selector:
app: node
type: NodePort
ports:
- protocol: TCP
port: 3000
targetPort: 30253
The Kubernetes service section: K8s services allow network access to the set of pods.
port: Define the application’s port inside the Docker container.
nodePort: The port number to use to connect to the base machine.
protocol: define a network layer type like TCP/UDP, To communicate with the network.
selector: To know and identify the deployment-created objects.
Save and close the text editor. As per our service configuration, we define port 3000 to access the nodeJS app, which is mapped with a label like app: node
, We are using NodePort to access the node app server on each node using port 30253.
Step 5: Deploy Service
We need to execute the given command to deploy the node-service on K8s cluster.
kubectl create -f node-service.yaml
We should get a success message on the screen, To check the service status for node, execute the given command.
To get service status.
kubectl get svc
Here we can see that NodePort assigned the ramdon port to access the node app on each node
Note: If you are using minikube So you need to use the given command to expose the total url to access your kubernets application.
minikube service node-service --url
Click here to get apache deployment process step by step on kubernetes cluster.
Conclusion
We have successfully deploy nodeJS app on kubernetes cluster in a simple way, If you still have questions, please post them in the comments section below.
Hi Aftab Ali,
As per your documentation, I created yaml file , also I run the command- kubectl create -f node-deployment.yaml
I got CrashLoopBack issue after running kubectl get pods command.
Please check below link screenshot.
blob:https://web.whatsapp.com/6503be59-f8a1-4710-a127-386618b573f8
My question is what happened with node deployment? How to resolve this CrashLoopBack deployment issue? please suggest.
Please refer below screenshots here for exact issue.
blob:https://web.whatsapp.com/09b7775c-3762-4240-80a4-06aa2d4d1027
blob:https://web.whatsapp.com/dda619fc-adcd-4dff-baa7-e6d587878d2b
blob:https://web.whatsapp.com/b0377164-edcb-4a04-b8dc-745e09945311
Hi Vishal,
I am so sorry for the late reply on it, I was quit busy, I go through the your issue, I will share the update on it, As soon as possible :).
Please tell me more about this. May I ask you a question?
You helped me a lot by posting this article and I love what I’m learning.
Thanks for your help and for writing this post. It’s been great.
Thank you for your post. I really enjoyed reading it, especially because it addressed my issue. It helped me a lot and I hope it will also help others.
Thank you for your post. I really enjoyed reading it, especially because it addressed my issue. It helped me a lot and I hope it will also help others.
Thank you for your help and this post. It’s been great.
Thank you for writing this post. I like the subject too.