You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

K8s部署Gitlab Runner执行npm install时出现网络错误求助

问题描述

已在K8s上部署Gitlab及Gitlab Runner,Runner可正常连接Gitlab实例并执行命令,但执行Vue.js项目发布任务时,npm install命令出现网络错误。

gitlab-ci.yaml配置

build site:
  image: node:lts-alpine
  stage: build
  script:
    - npm install --progress=false
    - npm run build
  artifacts:
    expire_in: 1 week
    paths:
      - dist

Gitlab Runner日志

Running with gitlab-runner 16.6.1 (f5da3c5a)
  on lms-git-runner-01 esosuhkgL, system ID: r_SjhlYAhCSRTB
Preparing the "kubernetes" executor
00:00
Using Kubernetes namespace: gitlab-runner
Using Kubernetes executor with image node:lts-alpine ...
Using attach strategy to execute scripts...
Preparing environment
00:06
Using FF_USE_POD_ACTIVE_DEADLINE_SECONDS, the Pod activeDeadlineSeconds will be set to the job timeout: 1h0m0s...
Waiting for pod gitlab-runner/runner-esosuhkgl-project-1-concurrent-0-bgff4642 to be running, status is Pending
Waiting for pod gitlab-runner/runner-esosuhkgl-project-1-concurrent-0-bgff4642 to be running, status is Pending
    ContainersNotReady: "containers with unready status: [build helper]"
    ContainersNotReady: "containers with unready status: [build helper]"
Running on runner-esosuhkgl-project-1-concurrent-0-bgff4642 via gitlab-runner-8b59c79dc-zmrfb...
Getting source from Git repository
00:03
Fetching changes with git depth set to 20...
Initialized empty Git repository in /builds/frontend/lms-frontend-v3/.git/
Created fresh repository.
Checking out e6aa6709 as detached HEAD (ref is main)...
Skipping Git submodules setup
Executing "step_script" stage of the job script
$ npm install --progress=false
npm ERR! code ECONNRESET
npm ERR! network aborted
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'
npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2023-12-26T09_21_54_880Z-debug-0.log
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: command terminated with exit code 1
解决方案

1. 切换npm镜像源

默认npm源在受限环境下访问不稳定,切换到国内镜像源(如淘宝镜像):
修改gitlab-ci.yaml的script部分,先设置镜像源再执行install:

build site:
  image: node:lts-alpine
  stage: build
  script:
    - npm config set registry https://registry.npmmirror.com/
    - npm install --progress=false
    - npm run build
  artifacts:
    expire_in: 1 week
    paths:
      - dist

2. 检查K8s Pod网络连通性

  • 进入Runner创建的Pod,手动测试网络:
    # 找到对应Pod名称
    kubectl get pods -n gitlab-runner
    # 进入Pod的build容器
    kubectl exec -it <pod-name> -n gitlab-runner -c build -- sh
    # 测试npm源连通性
    ping registry.npmjs.org
    # 或用curl测试
    curl -v https://registry.npmjs.org
    

如果无法访问,需检查K8s集群的网络策略、防火墙规则,确保Pod能访问外部网络。

3. 配置代理(若集群处于代理环境)

在gitlab-ci.yaml中添加代理配置:

build site:
  image: node:lts-alpine
  stage: build
  variables:
    HTTP_PROXY: "http://your-proxy-ip:port"
    HTTPS_PROXY: "http://your-proxy-ip:port"
    NO_PROXY: "localhost,127.0.0.1,gitlab-internal-url" # 排除内部Gitlab地址
  script:
    - npm config set proxy $HTTP_PROXY
    - npm config set https-proxy $HTTPS_PROXY
    - npm install --progress=false
    - npm run build
  artifacts:
    expire_in: 1 week
    paths:
      - dist

替换your-proxy-ip:port为实际代理地址,gitlab-internal-url为Gitlab实例的内部域名或IP。

4. 延长npm超时时间

网络不稳定时,可增加npm的重试超时时间:

build site:
  image: node:lts-alpine
  stage: build
  script:
    - npm config set fetch-retry-mintimeout 20000
    - npm config set fetch-retry-maxtimeout 120000
    - npm install --progress=false
    - npm run build
  artifacts:
    expire_in: 1 week
    paths:
      - dist

5. 检查Pod的DNS配置

如果Pod无法解析npm源域名,检查DNS设置:

kubectl exec -it <pod-name> -n gitlab-runner -c build -- cat /etc/resolv.conf

确保DNS服务器能正常解析源域名,若异常可在Runner配置中指定自定义DNS服务器。

内容的提问来源于stack exchange,提问作者Piroozeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:11:09