Home Web Front-end JS Tutorial How to build reliable containerized applications with React and Kubernetes

How to build reliable containerized applications with React and Kubernetes

Sep 26, 2023 am 09:07 AM
react kubernetes Containerization

How to build reliable containerized applications with React and Kubernetes

How to use React and Kubernetes to build reliable containerized applications

With the rapid development of cloud native technology, containerized applications have become a hot trend in software development today. As a popular front-end framework, React's flexibility and efficiency make it the first choice for many developers. This article will introduce how to build reliable containerized applications using React and Kubernetes, and provide some specific code examples.

  1. Creating a React application
    First, we need to create a basic React application. Create React App can be used to initialize a new React project. Use the following command:

    npx create-react-app my-app
    cd my-app
    npm start
    Copy after login

This will create a new project called my-app and start the local development server. After ensuring that the project can run normally, we can continue with the next steps.

  1. Writing a Dockerfile
    In order to package a React application into a container, we need to create a Dockerfile. In the root directory of the project, create a file called Dockerfile and add the following content to the file:

    # 使用Node镜像作为基础镜像
    FROM node:14-alpine
    
    # 将工作目录设置为/app
    WORKDIR /app
    
    # 将package.json和package-lock.json复制到容器中
    COPY package*.json ./
    
    # 安装应用的依赖
    RUN npm install
    
    # 将项目文件复制到容器中
    COPY . ./
    
    # 构建React应用
    RUN npm run build
    
    # 在容器中运行React应用
    CMD ["npm", "start"]
    Copy after login

This Dockerfile defines how to build and run the image of the React application . It uses an Alpine image based on Node, first installs the project's dependencies, then copies the project files to the container, and runs the React application in the container.

  1. Build Docker image
    In the root directory of the project, use the following command to build the Docker image:

    docker build -t my-react-app .
    Copy after login

    This will build a named Docker image based on the definition of the Dockerfile file. Is the mirror of my-react-app.

  2. Create Kubernetes deployment file
    Next, we need to create a Kubernetes deployment file to deploy our application. In the root directory of the project, create a file called deployment.yaml and add the following content to the file:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-react-app
      labels:
     app: my-react-app
    spec:
      replicas: 1
      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
    Copy after login

This file defines a file called my-react- To deploy the app, use the my-react-app image you just built and expose it to port 3000.

  1. Deploy the application to the Kubernetes cluster
    In the command line, use the following command to deploy the application to the Kubernetes cluster:

    kubectl apply -f deployment.yaml
    Copy after login

    This will be based on the deployment.yaml file Definition, create a deployment named my-react-app in the Kubernetes cluster.

  2. Verify that the app is running properly
    You can check that the app is running properly using the following command:

    kubectl get pods
    Copy after login

    If everything is fine, you should see a message named my- react-app's Pod is running.

  3. Accessing the application
    Finally, we can access our application through the service. In the root directory of the project, create a file named service.yaml and add the following content to the file:

    apiVersion: v1
    kind: Service
    metadata:
      name: my-react-app-service
    spec:
      selector:
     app: my-react-app
      type: LoadBalancer
      ports:
     - protocol: TCP
       port: 80
       targetPort: 3000
    Copy after login

    This file defines a service named my-react-app-service, External requests can be forwarded to our application through the load balancer.

Create the service using the following command:

kubectl apply -f service.yaml
Copy after login

Run the following command to get the external IP address of the service:

kubectl get services
Copy after login

Finally, you can open the application using a browser :

http://<EXTERNAL-IP>
Copy after login

Through the above steps, we have successfully built a reliable containerized application using React and Kubernetes. React provides powerful front-end development capabilities, while Kubernetes provides reliable container orchestration and operating environment. Their combination allows us to easily build, deploy and manage containerized applications.

I hope this article will be helpful to you, and also encourage you to continue to study and explore the development of cloud native technology in depth.

The above is the detailed content of How to build reliable containerized applications with React and Kubernetes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C# development experience sharing: microservices and containerization practice C# development experience sharing: microservices and containerization practice Nov 22, 2023 am 08:44 AM

C# Development Experience Sharing: Microservices and Containerization Practices With the rise of cloud computing and distributed architecture, microservices and containerization have become two hot topics in modern software development. Microservice architecture can help developers better divide system functions and improve scalability and maintainability; while containerization technology can achieve rapid deployment and elastic expansion. This article will be aimed at C# developers and share some experiences and techniques in the practice of microservices and containerization. 1. Overview of Microservice Architecture Microservice architecture is a method that splits an application into a series of small, independent departments.

PHP, Vue and React: How to choose the most suitable front-end framework? PHP, Vue and React: How to choose the most suitable front-end framework? Mar 15, 2024 pm 05:48 PM

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

How to optimize the performance of Java functions through containerization? How to optimize the performance of Java functions through containerization? Apr 29, 2024 pm 03:09 PM

Containerization improves Java function performance in the following ways: Resource isolation - ensuring an isolated computing environment and avoiding resource contention. Lightweight - takes up less system resources and improves runtime performance. Fast startup - reduces function execution delays. Consistency - Decouple applications and infrastructure to ensure consistent behavior across environments.

Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

Advantages and Disadvantages of Containerized Java Applications Advantages and Disadvantages of Containerized Java Applications Jun 04, 2024 pm 05:45 PM

Advantages of containerized Java applications: Resource isolation, prevention of interference and security vulnerabilities. Highly portable and easy to migrate to different platforms. Scalability, easy to automatically expand and adjust capacity. Improve development efficiency and use consistent tools and environments. Reduce costs and efficiently utilize system resources. Disadvantages: Performance overhead, may affect startup and restart times. Security issues: Containers share the kernel and there may be security vulnerabilities. Managing complexity, large systems require the use of specialized tools and platforms. Resource limitations affecting performance or stability. Network failures can cause distributed application problems.

React and the Frontend: Building Interactive Experiences React and the Frontend: Building Interactive Experiences Apr 11, 2025 am 12:02 AM

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

See all articles