找回密码
立即注册
搜索
热搜: Java Python Linux Go
发回帖 发新帖

478

积分

0

好友

63

主题
发表于 15 小时前 | 查看: 2| 回复: 0

概述

在企业环境(结合运维/DevOps最佳实践)中,通过Nexus Repository Manager搭建本地YUM代理仓库,能够带来以下显著优势:

  • 提高软件包下载速度(利用内网传输)
  • 减少对外部网络的依赖
  • 统一管理和控制软件包版本
  • 增强安全性(支持内部扫描和验证)
  • 节省带宽成本

一、环境准备

1.1 Nexus Repository Manager 3安装

Docker安装方式(推荐)
# 创建数据目录
mkdir -p /opt/nexus-data && chown -R 200:200 /opt/nexus-data

# 使用Docker运行Nexus
docker run -d \
  --name nexus3 \
  --restart=always \
  -p 8081:8081 \
  -p 8082:8082 \
  -v /opt/nexus-data:/nexus-data \
  -e NEXUS_CONTEXT=nexus \
  sonatype/nexus3:latest
系统服务配置
# 创建systemd服务文件
cat > /etc/systemd/system/nexus.service << EOF
[Unit]
Description=Nexus Repository Manager
After=network.target

[Service]
Type=simple
User=nexus
Group=nexus
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

1.2 初始配置

  1. 访问Nexus控制台:http://<服务器IP>:8081
  2. 获取初始管理员密码:
docker exec nexus3 cat /nexus-data/admin.password
# 或
cat /opt/nexus-data/admin.password
  1. 完成初始设置:
    • 修改默认密码
    • 配置匿名访问策略
    • 设置SMTP通知(可选)

二、创建YUM代理仓库

2.1 创建代理仓库

步骤:
  1. 登录Nexus控制台
  2. 点击左侧菜单的"Repository" → "Repositories"
  3. 点击"Create repository"
  4. 选择"yum (proxy)"
常用仓库配置示例:

CentOS 7 Base仓库

Name: centos-7-base
Type: proxy
Format: yum

# 代理设置
Remote storage: https://mirrors.aliyun.com/centos/7/os/x86_64/
Blob store: default

# 代理连接设置
HTTP port: 8082
Enable HTTP Authentication: false

# 存储设置
Strict Content Type Validation: true

EPEL 7仓库

Name: epel-7
Type: proxy
Format: yum
Remote storage: https://mirrors.aliyun.com/epel/7/x86_64/

2.2 创建仓库组

创建仓库组可以合并多个仓库,客户端只需配置一个地址:

Name: centos-all
Format: yum (group)
Member repositories:
  - centos-7-base
  - centos-7-updates
  - centos-7-extras
  - centos-7-centosplus
  - epel-7

三、客户端配置

3.1 配置客户端YUM源

方法一:手动创建repo文件
cat > /etc/yum.repos.d/nexus.repo << 'EOF'
# Nexus代理的CentOS仓库
[nexus-centos-base]
name=Nexus CentOS 7 Base Repository
baseurl=http://nexus-server:8082/repository/centos-7-base/
enabled=1
gpgcheck=1
gpgkey=http://nexus-server:8082/repository/centos-7-base/RPM-GPG-KEY-CentOS-7

[nexus-centos-updates]
name=Nexus CentOS 7 Updates
baseurl=http://nexus-server:8082/repository/centos-7-updates/
enabled=1
gpgcheck=1
gpgkey=http://nexus-server:8082/repository/centos-7-updates/RPM-GPG-KEY-CentOS-7

[nexus-centos-extras]
name=Nexus CentOS 7 Extras
baseurl=http://nexus-server:8082/repository/centos-7-extras/
enabled=1
gpgcheck=1
gpgkey=http://nexus-server:8082/repository/centos-7-extras/RPM-GPG-KEY-CentOS-7

[nexus-epel]
name=Nexus EPEL 7 Repository
baseurl=http://nexus-server:8082/repository/epel-7/
enabled=1
gpgcheck=1
gpgkey=http://nexus-server:8082/repository/epel-7/RPM-GPG-KEY-EPEL-7
EOF
方法二:使用仓库组配置
cat > /etc/yum.repos.d/nexus-group.repo << 'EOF'
[nexus-all]
name=Nexus All Repositories
baseurl=http://nexus-server:8082/repository/centos-all/
enabled=1
gpgcheck=0
# 如果启用GPG检查,需要导入所有仓库的GPG密钥
# gpgkey=http://nexus-server:8082/repository/centos-all/keys/
EOF

3.2 测试客户端连接

# 清理现有缓存
yum clean all
rm -rf /var/cache/yum/*

# 禁用所有外部源(仅测试Nexus源)
yum --disablerepo="*" --enablerepo="nexus-*" repolist

# 更新缓存
yum makecache --disablerepo="*" --enablerepo="nexus-*"

# 测试安装包
yum --disablerepo="*" --enablerepo="nexus-*" install -y nginx

四、同步策略配置

4.1 自动同步设置

配置代理仓库的下载设置

在Nexus中配置代理仓库的"Proxy"选项卡:

1. 远程存储设置

Remote storage URL: [远程仓库地址]
Authentication: [如果需要认证]

2. HTTP请求设置

User Agent: Nexus/3.x (适用于大多数情况)
Connection Timeout: 30
Connection Retries: 3

3. 负面缓存设置

Negative Cache: Enabled
Time To Live: 60 minutes

4.2 创建定时同步任务

在Nexus中创建定期更新仓库元数据的任务:

1. 创建任务

任务类型: Publish YUM repository metadata
名称: Daily YUM Metadata Update
目标仓库: [选择需要同步的仓库或仓库组]

2. 设置调度计划

执行频率: Daily
开始时间: 02:00
重复间隔: 24小时

3. Cron表达式示例

# 每天凌晨2点执行
0 0 2 * * ?

# 每4小时执行一次
0 0 */4 * * ?

4.3 API触发同步

#!/bin/bash
# sync-yum-repos.sh

NEXUS_URL="http://nexus-server:8081"
NEXUS_USER="admin"
NEXUS_PASS="your-password"

# 要同步的仓库列表
REPOS=("centos-7-base" "centos-7-updates" "centos-7-extras" "epel-7")

# 同步函数
sync_repository() {
    local repo_name=$1
    echo "开始同步仓库: $repo_name"

    # 触发元数据重建
    curl -X POST -u "${NEXUS_USER}:${NEXUS_PASS}" \
        -H "Content-Type: application/json" \
        "${NEXUS_URL}/service/rest/v1/repositories/yum/proxy/${repo_name}/rebuild-index"

    # 检查同步状态
    if [ $? -eq 0 ]; then
        echo "✅ 仓库 $repo_name 同步已触发"
    else
        echo "❌ 仓库 $repo_name 同步失败"
    fi
}

# 批量同步所有仓库
for repo in "${REPOS[@]}"; do
    sync_repository "$repo"
    sleep 5  # 避免同时触发过多请求
done

五、高级配置

5.1 缓存优化配置

代理仓库缓存设置
# 在仓库配置中设置
Cache Configuration:
  Time To Live (TTL): 1440  # 缓存保留时间(分钟)
  Last Verified Time: 60    # 最后验证时间(分钟)
  Expiration Policy: 24 hours
Blob存储配置
# 创建专用的Blob存储
名称: yum-blobstore
类型: File
路径: /nexus-data/blobs/yum-store

5.2 安全配置

创建专用用户
# 通过Nexus UI或API创建只读用户
用户名: yum-client
权限: nx-repository-view-yum-*-read
客户端HTTPS配置
# 生成SSL证书
openssl req -x509 -nodes -days 365 \
  -newkey rsa:2048 \
  -keyout /etc/ssl/private/nexus.key \
  -out /etc/ssl/certs/nexus.crt

# 配置Nexus使用HTTPS
# 在$NEXUS_HOME/etc/nexus.properties中添加:
application-port-ssl=8443
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-https.xml
ssl.etc=${karaf.data}/etc/ssl

5.3 存储空间管理

清理策略配置

创建自动清理策略:

策略名称: yum-cleanup-policy
格式: yum
条件:
  - 最后下载时间超过180天
  - 最后修改时间超过365天
  - 文件大小超过指定限制
手动清理脚本
#!/bin/bash
# cleanup-nexus-yum.sh

NEXUS_URL="http://localhost:8081"
ADMIN_USER="admin"
ADMIN_PASS="password"

# 清理90天未访问的组件
curl -X DELETE -u "${ADMIN_USER}:${ADMIN_PASS}" \
  -H "Content-Type: application/json" \
  "${NEXUS_URL}/service/rest/v1/components?repository=yum-proxy-repo&lastDownloadedBefore=90d"

# 清理孤立Blob
curl -X POST -u "${ADMIN_USER}:${ADMIN_PASS}" \
  -H "Content-Type: application/json" \
  "${NEXUX_URL}/service/rest/v1/blobstores/file/cleanup"

六、监控和维护

6.1 健康检查脚本

#!/bin/bash
# nexus-health-check.sh

NEXUS_HOST="nexus-server"
NEXUS_PORT="8081"
REPOS=("centos-7-base" "epel-7")

check_nexus_status() {
    echo "=== Nexus服务状态检查 ==="
    if curl -s "http://${NEXUS_HOST}:${NEXUS_PORT}" > /dev/null; then
        echo "✅ Nexus服务运行正常"
        return 0
    else
        echo "❌ Nexus服务无法访问"
        return 1
    fi
}

check_repository_status() {
    local repo=$1
    echo "检查仓库: $repo"
    STATUS=$(curl -s "http://${NEXUS_HOST}:${NEXUS_PORT}/service/rest/v1/repositories" \
        | jq -r ".[] | select(.name==\"$repo\") | .status.online")
    if [ "$STATUS" = "true" ]; then
        echo "  ✅ 在线"
        return 0
    else
        echo "  ❌ 离线"
        return 1
    fi
}

check_disk_usage() {
    echo "=== 磁盘使用情况 ==="
    df -h /nexus-data | tail -1 | awk '{print "使用率: "$5", 可用: "$4}'
}

# 执行检查
check_nexus_status
if [ $? -eq 0 ]; then
    for repo in "${REPOS[@]}"; do
        check_repository_status "$repo"
    done
fi
check_disk_usage

6.2 性能监控

Prometheus监控配置
# nexus-exporter配置
scrape_configs:
  - job_name: 'nexus'
    static_configs:
      - targets: ['nexus-server:8081']
    metrics_path: '/service/metrics/prometheus'
Grafana仪表板指标
  • 仓库同步状态
  • 存储空间使用率
  • 请求响应时间
  • 缓存命中率
  • 并发连接数

七、故障排除

7.1 常见问题及解决方案

问题1:同步失败
# 检查网络连接
ping mirrors.aliyun.com

# 检查DNS解析
nslookup mirrors.aliyun.com

# 检查防火墙规则
iptables -L -n | grep 8081
iptables -L -n | grep 8082
问题2:客户端无法访问
# 测试基本连接
curl -v http://nexus-server:8082/repository/centos-7-base/repodata/repomd.xml

# 检查Nexus日志
tail -f /opt/nexus-data/log/nexus.log | grep -i "yum\|repository"

# 检查仓库状态
curl -u admin:password http://nexus-server:8081/service/rest/v1/repositories/centos-7-base
问题3:存储空间不足
# 查看存储使用情况
du -sh /nexus-data/blobs/default/
du -sh /nexus-data/blobs/yum-store/

# 清理临时文件
find /nexus-data/tmp -type f -mtime +7 -delete

# 清理旧缓存
nexus cleanup -name yum-cleanup-policy

7.2 日志分析

关键日志位置
# Nexus主日志
/opt/nexus-data/log/nexus.log

# 请求日志
/opt/nexus-data/log/request.log

# 任务日志
/opt/nexus-data/log/tasklog/
日志监控命令
# 实时监控同步错误
tail -f /opt/nexus-data/log/nexus.log | grep -i "sync.*fail\|error.*repository"

# 查看任务执行日志
grep "Publish YUM repository" /opt/nexus-data/log/nexus.log

# 分析访问日志
awk '{print $1}' /opt/nexus-data/log/request.log | sort | uniq -c | sort -rn

八、最佳实践

8.1 架构设计建议

1. 分层存储架构

第一层:本地Nexus代理(快速缓存)
第二层:区域Nexus镜像(区域缓存)
第三层:中央Nexus主库(统一管理)

2. 网络拓扑优化

生产环境:专线连接
开发环境:VPN连接
分支机构:本地缓存+定时同步

8.2 备份与恢复策略

备份脚本
#!/bin/bash
# nexus-backup.sh

BACKUP_DIR="/backup/nexus"
DATE=$(date +%Y%m%d_%H%M%S)

# 停止Nexus服务(如果使用Docker)
docker stop nexus3

# 备份数据目录
tar -czf "${BACKUP_DIR}/nexus-data-${DATE}.tar.gz" /opt/nexus-data

# 备份配置文件
cp /opt/nexus/etc/*.properties "${BACKUP_DIR}/"

# 启动Nexus服务
docker start nexus3

# 保留最近30天的备份
find "${BACKUP_DIR}" -name "*.tar.gz" -mtime +30 -delete
恢复步骤
# 停止服务
docker stop nexus3

# 恢复数据
tar -xzf /backup/nexus/nexus-data-YYYYMMDD.tar.gz -C /

# 恢复配置
cp /backup/nexus/*.properties /opt/nexus/etc/

# 启动服务
docker start nexus3

8.3 性能优化建议

1. 硬件配置

内存: 8GB以上(推荐16GB)
存储: SSD硬盘,RAID配置
CPU: 4核以上
网络: 千兆以太网

2. JVM优化

# 修改$NEXUS_HOME/bin/nexus.vmoptions
-Xms4096m
-Xmx4096m
-XX:MaxDirectMemorySize=2g

3. 数据库优化

# 定期清理任务历史
DELETE FROM task WHERE ended < DATE_SUB(NOW(), INTERVAL 30 DAY);

九、扩展配置

9.1 多区域部署

区域同步配置
# 主从同步配置
主Nexus: nexus-master.example.com
从Nexus:
  - nexus-asia.example.com
  - nexus-europe.example.com
  - nexus-america.example.com

# 同步策略
同步频率: 每小时
同步方式: 增量同步
失败重试: 3次

9.2 集成CI/CD流水线

集成CI/CD流水线是现代DevOps工作流的关键部分,以下是一个Jenkins Pipeline示例:

# Jenkins Pipeline示例
pipeline {
    agent any
    stages {
        stage('Setup Environment') {
            steps {
                script {
                    // 配置使用Nexus YUM源
                    sh '''
                        cat > /etc/yum.repos.d/nexus-ci.repo << EOF
                        [nexus-ci]
                        name=Nexus CI Repository
                        baseurl=http://nexus-server:8082/repository/centos-all/
                        enabled=1
                        gpgcheck=0
                        EOF
                    '''
                }
            }
        }
        stage('Install Dependencies') {
            steps {
                sh '''
                    yum clean all
                    yum makecache --disablerepo="*" --enablerepo="nexus-ci"
                    yum install -y --disablerepo="*" --enablerepo="nexus-ci" \
                        git gcc make rpm-build
                '''
            }
        }
    }
}

总结

通过Nexus Repository Manager配置YUM代理仓库,企业可以实现:

  1. 高效管理:统一管理所有软件包依赖,提升运维效率
  2. 快速部署:内网高速下载,显著提升部署速度
  3. 安全可控:内部验证和扫描,确保软件安全
  4. 成本优化:减少外网带宽消耗,降低运营成本
  5. 高可用性:支持多区域部署和故障转移,保障业务连续性

这种方案不仅简化了软件包管理流程,还为持续集成和自动化部署奠定了坚实基础。




上一篇:Redis缓存雪崩排查实战:穿透、击穿与热点失效的协同解决方案
下一篇:LMCache实战指南:基于KV缓存复用优化LLM推理TTFT,集成vLLM提升性能
您需要登录后才可以回帖 登录 | 立即注册

手机版|小黑屋|网站地图|云栈社区 ( 苏ICP备2022046150号-2 )

GMT+8, 2025-12-10 21:03 , Processed in 0.097778 second(s), 40 queries , Gzip On.

Powered by Discuz! X3.5

© 2025-2025 云栈社区.

快速回复 返回顶部 返回列表