Sobes.tech
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:

  1. Create a Docker image of the Node.js application.
  2. Upload the image to a Docker repository (e.g., Docker Hub or a private registry).
  3. Write Kubernetes manifests — Deployment to manage application pods and Service for access.
  4. 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.