【问题标题】:Unable to resolve sequelize package in /usr/src/app of the api server container?无法解析 api 服务器容器的 /usr/src/app 中的 sequelize 包?
【发布时间】:2019-07-23 13:23:42
【问题描述】:

我基本上是在尝试运行一个 React js 应用程序,它主要由 3 个服务组成,即 postgres db、API 服务器和 UI 前端(使用 nginx 服务)。目前该应用程序在使用 docker-compose 的开发模式下按预期工作,但是当我尝试使用 kubernetes 在生产中运行它时,我无法访问应用程序的 api 服务器(连接被拒绝)。

我已经尝试在 api 服务器容器中运行命令 npm install。

API 服务器镜像的 Dockerfile

FROM node:12.4.0-alpine
RUN mkdir -p usr/src/app
WORKDIR /usr/src/app
COPY package.json package.json
RUN npm install sequelize-cli nodemon -g
RUN npm install && npm cache clean --force
WORKDIR /usr/src/app
COPY . .
WORKDIR /usr/src/app
EXPOSE 8000
CMD [ "npm","start" ]

API服务器的package.json

{
  "name": "wootz-backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node_modules/.bin/nodemon index.js",
    "migrate": "node_modules/.bin/sequelize db:migrate --config config/config.json"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^1.19.1",
    "pg": "^7.11.0",
    "sequelize": "^5.8.7",
    "sequelize-cli": "^5.4.0"
  }
}

API 持久化卷一 yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: api-initdb-pv-volume
  labels:
    type: local
    app: api
spec:
  storageClassName: manual
  capacity:
    storage: 1Mi
  accessModes:
    - ReadOnlyMany
  hostPath:
    path: "/home/vignesh/page designer kubernetes yamls/api"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: api-initdb-pv-claim-one
  labels:
    app: api
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

API 持久化卷二 yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: api-initdb-pv-volume-2
  labels:
    type: local
    app: api
spec:
  storageClassName: manual
  capacity:
    storage: 1Mi
  accessModes:
    - ReadOnlyMany
  hostPath:
    path: "/home/vignesh/page designer kubernetes yamls/api"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: api-initdb-pv-claim-two
  labels:
    app: api
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

APIserver.yaml

apiVersion: v1
kind: Service
metadata:
  name: apiserver
  labels:
    app: apiserver
spec:

  ports:
  - name: apiport
    port: 8000
    targetPort: 8000

  selector:
    app: apiserver
    tier: backend

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: apiserver
  labels:
    app: apiserver
spec:
  selector:
    matchLabels:
      app: apiserver
      tier: backend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: apiserver
        tier: backend
    spec:

      containers:
      - image: suji165475/vignesh:apifinal
        name: apiserver
        env:
        - name: POSTGRES_PASSWORD
          value: password
        - name: POSTGRES_USER
          value: postgres
        - name: POSTGRES_DB
          value: wootz
        ports:
        - containerPort: 8000
          name: myport
        volumeMounts:
        - name: api-persistent-storage-one
          mountPath: /usr/src/app
        - name: api-persistent-storage-two
          mountPath: /usr/src/app/node_modules
      volumes:
      - name: api-persistent-storage-one
        persistentVolumeClaim:
          claimName: api-initdb-pv-claim-one
      - name: api-persistent-storage-two
        persistentVolumeClaim:
          claimName: api-initdb-pv-claim-two

当我尝试在 api 服务器容器中运行命令 npm run migrate 时,我收到一条错误消息-

/usr/src/app # npm 运行迁移

wootz-backend@1.0.0 迁移 /usr/src/app sequelize db:migrate --config config/config.json

无法解析 /usr/src/app 中的 sequelize 包 呃!代码生命周期 呃!错误号 1 呃! wootz-backend@1.0.0 迁移:sequelize db:migrate --config > config/config.json 呃!退出状态 1 呃! 呃! wootz-backend@1.0.0 迁移脚本失败。

我也尝试在 api 容器中运行命令 npm install --save sequelize 但这次我得到一个不同的错误 -

WARN checkPermissions 缺少对 /usr/src/app/node_modules/pg 的写访问权限 WARN pg-pool@2.0.6 需要 pg@>5.0 的对等点,但没有安装。您必须 > 自己安装对等依赖项。 WARN wootz-backend@1.0.0 无描述 WARN wootz-backend@1.0.0 没有存储库字段。

错误!路径 /usr/src/app/node_modules/pg 呃!代码ENOENT 呃!错误号 -2 呃!系统调用访问 呃! enoent ENOENT:没有这样的文件或目录,访问 > > '/usr/src/app/node_modules/pg' 呃! enoent 这与 npm 找不到文件有关。 呃!恩恩特

注意:此问题仅在我使用 kubernetes 运行时发生,而不是在使用 docker-compose 的开发模式下发生。因此,它不会是应用程序本身的问题。

【问题讨论】:

    标签: node.js reactjs docker npm kubernetes


    【解决方案1】:

    您的应用程序应该内置到您正在部署的 Docker 映像中。

    有一种将本地源代码安装在 Docker 映像之上以进行本地开发的“模式”。 这种模式在 Kubernetes 中不起作用。您不能在已部署的 Kubernetes pod 上进行“实时”开发。

    幸运的是,修复此问题可将 Kubernetes YAML 减少约三分之二。您只需彻底删除部署规范中的PersistentVolumes 和PersistentVolumeClaims,以及volumes:volumeMounts:

    当您对应用程序进行更改时,您需要docker build 一个新图像并以某种方式导致部署重新启动。 “最好”的方法是使用不同的镜像标签并更改部署对象中的标签,这将导致它对 Pod 进行滚动重启,如果存在严重问题,可以干净地回滚到旧镜像新代码。

    (在 Kubernetes 中这种模式存在两个技术问题。第一个是您不能直接控制 pod 将在哪个节点上运行,因此您必须手动将源代码复制到每个节点上;非常重要错过了 Kubernetes 的重点。第二个是普通的docker run 将使用映像中的内容填充安装在映像路径上的空卷,但 Kubernetes 不会,因此使用匿名卷的“技巧”来使用图片中的node_modules 树 [现在它包含非常重要的应用程序数据,Docker 将永远不会再次更新它] 实际上不起作用;你只会得到空的node_modules。)

    【讨论】:

    • 所以你的意思是我必须删除我的 API 持久卷和声明以及部署规范中的卷安装和卷,然后命令 npm install 不会给我容器中的任何错误对?
    • 我按照你说的做了,但这次我运行 npm run migrate 时出现错误:getaddrinfo ENOTFOUND pg_db
    猜你喜欢
    • 2019-08-08
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多