首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >构建时react应用程序的Kubernetes容器中的KILL -9

构建时react应用程序的Kubernetes容器中的KILL -9
EN

Stack Overflow用户
提问于 2021-06-24 15:05:20
回答 1查看 49关注 0票数 0

我有一个react应用程序,我想使用docker文件将其部署到kubernetes,并使用nginx托管它。我的Dockerfile如下所示

代码语言:javascript
运行
复制
FROM node:14-alpine as build

# Install git
RUN apk update && apk upgrade && \
    apk add --no-cache git

WORKDIR /src
COPY package*.json ./

RUN npm config set unsafe-perm true
RUN npm install
COPY . .
RUN npm run build

在运行kustomization之后,我的kuberetes配置如下

代码语言:javascript
运行
复制
apiVersion: v1
data:
  settings.ts.ctmpl: |
    export const LIGHTHOUSE_API_HOST = 'stageBackendURL';
kind: ConfigMap
metadata:
  name: lighthouse-web-configmap-6968hk6f5g
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: lighthouse-web
    manifest_type: svc
  name: lighthouse-web
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 3001
  selector:
    app: lighthouse-web
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: lighthouse-web
spec:
  minReadySeconds: 10
  progressDeadlineSeconds: 600
  selector:
    matchLabels:
      app: lighthouse-web
  template:
    metadata:
      labels:
        app: lighthouse-web
      name: lighthouse-web
    spec:
      containers:
      - args:
        - npm
        - run
        - serve
        image: <url to image>
        imagePullPolicy: Always
        name: lighthouse-web
        ports:
        - containerPort: 3001
          name: http
        volumeMounts:
        - mountPath: /src/src/settings.ts
          name: lighthouse-web-config
          subPath: settings.ts
        - mountPath: /src/build
          name: lighthouse-web-static
      - image: nginx:1.14
        imagePullPolicy: Always
        name: nginx
        ports:
        - containerPort: 80
          name: nginx
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: lighthouse-web-static
      initContainers:
      - args:
        - -template
        - /config-templates/settings.ts.ctmpl:/config/settings.ts
        - -log-level
        - trace
        - -once
        image: <URL TO DOCKER IMAGE>
        imagePullPolicy: Always
        name: consul-template
        resources:
          limits:
            cpu: 500m
            memory: 500Mi
          requests:
            cpu: 500m
            memory: 500Mi
        volumeMounts:
        - mountPath: /config-templates
          name: lighthouse-web-configmap
        - mountPath: /config
          name: lighthouse-web-config
      volumes:
      - configMap:
          name: lighthouse-web-configmap-6968hk6f5g
        name: lighthouse-web-configmap
      - emptyDir: {}
        name: lighthouse-web-config
      - emptyDir: {}
        name: lighthouse-web-static

当我部署它时,build目录是空的。我尝试ssh到容器并手动构建它,但得到了这个错误:

代码语言:javascript
运行
复制
export NODE_OPTIONS=--max_old_space_size=4096 && react-scripts build

Creating an optimized production build...
The build failed because the process exited too early. This probably means the system ran out of memory or someone called `kill -9` on the process.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! lighthouse@0.1.0 build: `export NODE_OPTIONS=--max_old_space_size=4096 && react-scripts build`
npm ERR! Exit status 1
npm ERR!

我之前将内存和cpu保持在1gi和100m,但尝试增加到16Gi和8Gi,但仍然不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-24 16:14:47

以上几点注意事项:

1:我使用多阶段Dockerfile构建,整个构建过程发生在节点容器中,但最终的完整构建应用程序随后被复制到基于nginx的容器中,如下所示

代码语言:javascript
运行
复制
FROM node as build
...
FROM nginx
COPY --from=build /path/to/app /usr/share/nginx/html

2: npm构建是昂贵的过程,在kube环境中进行npm构建,并设置合理的运行时限制,很可能会遇到您在这里提到的问题。

3:确保在您的工作站/构建系统上正确创建应用程序工件

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68111022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档