温馨提示×

jenkinscentos如何调优

小樊
44
2026-09-09 08:00:51
栏目: 智能运维

下面从 CentOS 上 Jenkins 的性能调优 角度,系统性地给你一份实操指南,覆盖 系统层、JVM 层、Jenkins 配置层、插件与架构层。


一、系统层(CentOS)调优

1. 资源准备建议

  • CPU:构建并发多时,建议 ≥ 4 核
  • 内存:
    • Jenkins 本身:2–4G
    • 构建任务(Maven / Node / Docker):额外 4–8G
  • 磁盘:
    • 使用 SSD
    • 单独挂载 /var/lib/jenkins

2. 文件描述符限制

# /etc/security/limits.conf
jenkins soft nofile 65535
jenkins hard nofile 65535

3. 关闭 swap(可选,提升稳定性)

swapoff -a
# 注释 /etc/fstab 中 swap 行

4. 内核参数

# /etc/sysctl.conf
net.core.somaxconn = 1024
vm.swappiness = 10
sysctl -p

二、Jenkins JVM 调优(最重要)

1. 修改 Jenkins 启动参数(CentOS)

路径通常是:

/etc/sysconfig/jenkins

或(systemd):

/etc/systemd/system/jenkins.service.d/override.conf

2. 推荐 JVM 参数

JAVA_OPTS="-Xms2g -Xmx4g -XX:MaxMetaspaceSize=512m -Dfile.encoding=UTF-8"

生产建议:

-Xms4g -Xmx4g   # 避免频繁 GC

3. GC 优化(Java 11+ 推荐 G1)

-XX:+UseG1GC
-XX:+ParallelRefProcEnabled
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/var/log/jenkins/heapdump.hprof

三、Jenkins 自身配置调优

1. 限制构建并发数

Manage Jenkins → Configure System

  • # of executors:
    • 单节点:CPU 核数 × 1~1.5
    • 不建议无限开

2. 构建历史清理(非常重要)

防止磁盘爆炸:

# 每个 Job 配置
Discard old builds
- Days to keep: 7
- Max # of builds: 20

或全局:

Manage Jenkins → Script Console

3. 禁用不必要的功能

  • 关闭 Jenkins CLI
  • 关闭匿名用户权限
  • 关闭旧版 API

四、插件与架构优化

1. 使用 Agent(强烈推荐)

✅ Master 只做调度

  • Linux Agent
  • Docker Agent
  • Kubernetes Agent

示例:

agent {
  label 'build-node'
}

2. 慢插件排查

常见拖慢 Jenkins 的插件:

  • 老版本 Git
  • 无用报表插件
  • 大量 SCM Poll

使用:

Manage Jenkins → Manage Plugins → Installed

3. 使用 Pipeline 而非 Freestyle

  • 更易维护
  • 更好并行
  • 资源可控

五、构建性能优化

1. 依赖缓存

  • Maven:
/root/.m2/repository

挂载到 Agent

  • npm:
~/.npm

2. 并行构建

parallel {
  stage('Build') { ... }
  stage('Test') { ... }
}

3. Docker 构建优化

  • 使用层缓存
  • 使用 --cache-from
  • 使用独立构建节点

六、监控与问题定位

1. 监控工具

  • Prometheus + Grafana
  • Jenkins Monitoring Plugin
  • top, jstat, jstack

2. 常见卡顿原因

  • 磁盘 IO 满
  • JVM Full GC
  • 插件死锁
  • 构建节点不足

七、典型调优前后对比

项目 调优前 调优后
页面响应 5s <1s
构建排队 严重 明显减少
磁盘增长 爆炸 可控
OOM 常见 几乎无

八、如果你愿意,我可以:

  • ✅ 根据你的 Jenkins 版本 + 构建类型 给具体参数
  • ✅ 帮你写 systemd / docker / k8s Jenkins 调优方案
  • ✅ 分析你当前的 jenkins.log / thread dump

你可以直接贴:

Jenkins 版本 / 构建类型(Maven / npm / Docker)/ 服务器配置

0 踩