Junior — Middle
How do you deploy a Node.js application inside a Kubernetes cluster?
sobes.tech AI
Answer from AI
The following steps are usually performed to deploy a Node.js application in Kubernetes:
- Create a Docker image of the Node.js application.
- Upload the image to a Docker repository (e.g., Docker Hub or a private registry).
- Write Kubernetes manifests — Deployment to manage application pods and Service for access.
- Apply the manifests using
kubectl apply -f.
Example of a simple Deployment for Node.js:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-app
spec:
replicas: 3
selector:
matchLabels:
app: nodejs-app
template:
metadata:
labels:
app: nodejs-app
spec:
containers:
- name: nodejs-container
image: your-docker-repo/nodejs-app:latest
ports:
- containerPort: 3000
And a Service for access:
apiVersion: v1
kind: Service
metadata:
name: nodejs-service
spec:
selector:
app: nodejs-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer
After that, the commands kubectl apply -f deployment.yaml and kubectl apply -f service.yaml will deploy the application in the cluster.