9951 explained code solutions for 126 technologies


reactjsHow can I use Kubernetes to deploy a React.js application?


Kubernetes can be used to deploy a React.js application with the following steps:

  1. Create a Dockerfile for your React application. This will define the environment and dependencies your application needs to run.
FROM node:12.2.0-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD [ "npm", "start" ]
  1. Build a Docker image of your React application.
docker build -t my-react-app .
  1. Create a Kubernetes deployment yaml file. This will define the number of replicas, ports, and other configurations for your application.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-react-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-react-app
  template:
    metadata:
      labels:
        app: my-react-app
    spec:
      containers:
      - name: my-react-app
        image: my-react-app
        ports:
        - containerPort: 3000
  1. Create a Kubernetes service yaml file. This will define the type of service and port to use.
apiVersion: v1
kind: Service
metadata:
  name: my-react-app-service
spec:
  type: NodePort
  ports:
  - port: 3000
    nodePort: 30200
  selector:
    app: my-react-app
  1. Deploy the React application to Kubernetes.
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
  1. Access the application from your browser.

  2. Monitor and manage the application using the Kubernetes dashboard.

Helpful links

Edit this code on GitHub