1. Nexus概述与环境规划
Nexus是一个开源的仓库管理器,用于存储和管理依赖包。Nexus支持Maven、npm、Docker等多种包格式,是企业级开发中不可或缺的工具。更多学习教程www.fgedu.net.cn
1.1 Nexus版本说明
Nexus目前主要版本为3.x系列,本教程以Nexus 3.57.0为例进行详细讲解。Nexus 3.x版本相比之前版本在性能、稳定性和功能方面都有显著提升,支持更多的仓库类型。
$ cat /opt/nexus/sonatype-work/nexus3/db/security/config | grep version
# 查看系统版本
$ cat /etc/os-release
NAME=”Oracle Linux Server”
VERSION=”8.9″
ID=”ol”
PRETTY_NAME=”Oracle Linux Server 8.9″
# 查看内核版本
$ uname -r
5.4.17-2136.302.7.2.el8uek.x86_64
1.2 环境规划
本次安装环境规划如下:
nexus01.fgedu.net.cn (192.168.1.126) – Nexus主服务器
nexus02.fgedu.net.cn (192.168.1.127) – Nexus备用服务器
Nexus版本:3.57.0
安装方式:二进制安装
数据存储:本地磁盘
2. 硬件环境要求
Nexus作为仓库管理器,对硬件资源要求根据存储容量和并发访问量而定。学习交流加群风哥微信: itpux-com
2.1 物理主机环境要求
– CPU:至少4核
– 内存:至少8GB
– 磁盘:系统盘120GB SSD + 数据盘1TB SSD
# 检查Nexus服务器资源
# free -h
total used free shared buff/cache available
Mem: 8G 2.4G 5.1G 128M 512M 5.4G
Swap: 4G 0B 4G
# 检查磁盘空间
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 120G 20G 100G 17% /
/dev/sdb1 1TB 100G 900G 10% /data
2.2 vSphere虚拟主机环境要求
– Nexus服务器:
– vCPU:4核
– 内存:8GB
– 磁盘:系统盘120GB SSD + 数据盘1TB SSD
– 网络:VMXNET3网卡,1Gbps网络
资源池配置:
– CPU预留:2GHz
– 内存预留:4GB
– 内存限制:8GB
– CPU份额:正常
– 内存份额:正常
2.3 云平台主机环境要求
– Nexus服务器:
– 实例规格:ecs.g6.2xlarge或同等规格
– vCPU:8核
– 内存:16GB
– 系统盘:SSD云盘 120GB
– 数据盘:SSD云盘 1TB
– 网络带宽:1Gbps以上
存储配置:
– OSS对象存储:用于存储备份数据
– NAS文件存储:用于共享配置文件
– 云盘快照:定期备份数据
3. 操作系统环境准备
在安装Nexus之前,需要对操作系统进行必要的配置和优化。
3.1 操作系统版本检查
# cat /etc/os-release
NAME=”Oracle Linux Server”
VERSION=”8.9″
ID=”ol”
PRETTY_NAME=”Oracle Linux Server 8.9″
# 检查内核版本
# uname -r
5.4.17-2136.302.7.2.el8uek.x86_64
# 检查SELinux状态
# getenforce
Enforcing
# 检查防火墙状态
# systemctl status firewalld
● firewalld.service – firewalld – dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running)
3.2 依赖服务安装
# dnf install -y wget curl tar gzip java-11-openjdk-devel
# 关闭防火墙
# systemctl stop firewalld
# systemctl disable firewalld
# 关闭SELinux
# setenforce 0
# sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/’ /etc/selinux/config
# 创建Nexus用户
# useradd -r -s /bin/false nexus
# 创建目录结构
# mkdir -p /data/nexus/{config,bin,data}
# chown -R nexus:nexus /data/nexus
3.3 配置网络
# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.1.126
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
# 重启网络
# systemctl restart NetworkManager
# 验证网络
# ping -c 4 google.com
# 配置主机名
# hostnamectl set-hostname nexus01.fgedu.net.cn
# 配置hosts文件
# vi /etc/hosts
192.168.1.126 nexus01.fgedu.net.cn nexus01
192.168.1.127 nexus02.fgedu.net.cn nexus02
4. Nexus安装配置
完成环境准备后,开始安装Nexus。
4.1 安装Nexus
# wget https://download.sonatype.com/nexus/3/nexus-3.57.0-01-unix.tar.gz
# 解压Nexus
# tar -xzf nexus-3.57.0-01-unix.tar.gz -C /opt/
# 创建软链接
# ln -s /opt/nexus-3.57.0-01 /opt/nexus
# 修改权限
# chown -R nexus:nexus /opt/nexus
# chown -R nexus:nexus /opt/sonatype-work
# 配置Nexus服务
# vi /etc/systemd/system/nexus.service
[Unit]
Description=Nexus Repository Manager
After=network.target
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
User=nexus
Restart=on-abort
[Install]
WantedBy=multi-user.target
# 启动Nexus服务
# systemctl start nexus
# systemctl enable nexus
# 验证服务状态
# systemctl status nexus
4.2 初始化Nexus
# cat /opt/sonatype-work/nexus3/admin.password
# 访问Nexus Web界面
# 打开浏览器访问 http://nexus01:8081
# 登录Nexus
# 用户名:admin
# 密码:查看admin.password文件
# 完成初始化向导
# 1. 更改默认密码
# 2. 配置匿名访问
# 3. 配置邮件通知
# 4. 完成初始化
5. Nexus配置优化
为了提高Nexus的性能和稳定性,需要进行一些配置优化。
5.1 基本配置优化
# vi /opt/nexus/etc/nexus-default.properties
application-port=8081
application-host=0.0.0.0
nexus-context-path=/
# 编辑JVM配置
# vi /opt/nexus/bin/nexus.vmoptions
-Xms4G
-Xmx8G
-XX:MaxDirectMemorySize=4G
-XX:+UnlockDiagnosticVMOptions
-XX:+UnsyncloadClass
-XX:+LogVMOutput
-XX:LogFile=/opt/sonatype-work/nexus3/log/jvm.log
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=/opt/nexus/system
-Dkaraf.base=/opt/nexus
-Dkaraf.etc=/opt/nexus/etc
-Djava.util.logging.config.file=/opt/nexus/etc/java.util.logging.properties
-Dkaraf.data=/opt/sonatype-work/nexus3
-Dkaraf.log=/opt/sonatype-work/nexus3/log
-Djava.io.tmpdir=/opt/sonatype-work/nexus3/tmp
# 重启Nexus服务
# systemctl restart nexus
5.2 高可用配置
# 1. 安装Nexus到所有节点
# 2. 配置共享存储
# 3. 配置负载均衡
# 配置共享存储
# mount -t nfs 192.168.1.100:/data/nexus /opt/sonatype-work/nexus3
# 配置负载均衡
# 编辑Nginx配置
# vi /etc/nginx/nginx.conf
upstream nexus {
server 192.168.1.126:8081;
server 192.168.1.127:8081;
}
server {
listen 80;
server_name nexus.fgedu.net.cn;
location / {
proxy_pass http://nexus;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
6. Nexus仓库配置
Nexus支持多种仓库类型,包括Maven、npm、Docker等。
6.1 配置Maven仓库
# 打开浏览器访问 http://nexus01:8081
# 创建Maven仓库
# 1. 点击左侧菜单 “Repository” -> “Repositories”
# 2. 点击 “Create repository”
# 3. 选择 “maven2 (hosted)”
# 4. 填写仓库信息
# – Name: maven-releases
# – Version policy: Release
# – Deployment policy: Allow redeploy
# 5. 点击 “Create repository”
# 配置Maven代理仓库
# 1. 点击 “Create repository”
# 2. 选择 “maven2 (proxy)”
# 3. 填写仓库信息
# – Name: maven-central
# – Remote storage: https://repo1.maven.org/maven2/
# 4. 点击 “Create repository”
# 配置Maven组仓库
# 1. 点击 “Create repository”
# 2. 选择 “maven2 (group)”
# 3. 填写仓库信息
# – Name: maven-group
# – Member repositories: 选择刚才创建的仓库
# 4. 点击 “Create repository”
6.2 配置npm仓库
# 打开浏览器访问 http://nexus01:8081
# 创建npm仓库
# 1. 点击左侧菜单 “Repository” -> “Repositories”
# 2. 点击 “Create repository”
# 3. 选择 “npm (hosted)”
# 4. 填写仓库信息
# – Name: npm-releases
# 5. 点击 “Create repository”
# 配置npm代理仓库
# 1. 点击 “Create repository”
# 2. 选择 “npm (proxy)”
# 3. 填写仓库信息
# – Name: npm-registry
# – Remote storage: https://registry.npmjs.org/
# 4. 点击 “Create repository”
# 配置npm组仓库
# 1. 点击 “Create repository”
# 2. 选择 “npm (group)”
# 3. 填写仓库信息
# – Name: npm-group
# – Member repositories: 选择刚才创建的仓库
# 4. 点击 “Create repository”
6.3 配置Docker仓库
# 打开浏览器访问 http://nexus01:8081
# 创建Docker仓库
# 1. 点击左侧菜单 “Repository” -> “Repositories”
# 2. 点击 “Create repository”
# 3. 选择 “docker (hosted)”
# 4. 填写仓库信息
# – Name: docker-releases
# – HTTP: 5000
# 5. 点击 “Create repository”
# 配置Docker代理仓库
# 1. 点击 “Create repository”
# 2. 选择 “docker (proxy)”
# 3. 填写仓库信息
# – Name: docker-hub
# – Remote storage: https://registry-1.docker.io
# – HTTP: 5001
# 4. 点击 “Create repository”
# 配置Docker组仓库
# 1. 点击 “Create repository”
# 2. 选择 “docker (group)”
# 3. 填写仓库信息
# – Name: docker-group
# – HTTP: 5002
# – Member repositories: 选择刚才创建的仓库
# 4. 点击 “Create repository”
7. Nexus安全配置
Nexus提供了多种安全功能,包括用户认证、授权、TLS加密等。
7.1 认证配置
# 打开浏览器访问 http://nexus01:8081
# 创建用户
# 1. 点击左侧菜单 “Security” -> “Users”
# 2. 点击 “Create local user”
# 3. 填写用户信息
# – User ID: developer
# – First name: Developer
# – Last name: User
# – Email: developer@fgedu.net.cn
# – Password: 设置密码
# – Status: Active
# 4. 点击 “Create local user”
# 创建角色
# 1. 点击左侧菜单 “Security” -> “Roles”
# 2. 点击 “Create role”
# 3. 填写角色信息
# – Role ID: developer-role
# – Name: Developer Role
# – Description: Developer role
# – Privileges: 选择适当的权限
# 4. 点击 “Create role”
# 分配角色
# 1. 点击左侧菜单 “Security” -> “Users”
# 2. 选择刚才创建的用户
# 3. 点击 “Roles”
# 4. 选择刚才创建的角色
# 5. 点击 “Save”
7.2 TLS加密配置
# mkdir -p /opt/nexus/etc/ssl
# openssl req -newkey rsa:2048 -nodes -keyout /opt/nexus/etc/ssl/nexus.key -x509 -days 365 -out /opt/nexus/etc/ssl/nexus.crt
# 配置Nexus使用HTTPS
# vi /opt/nexus/etc/nexus-default.properties
application-port=8443
application-host=0.0.0.0
nexus-context-path=/
# 编辑JVM配置
# vi /opt/nexus/bin/nexus.vmoptions
-Djavax.net.ssl.keyStore=/opt/nexus/etc/ssl/nexus.jks
-Djavax.net.ssl.keyStorePassword=password
# 转换证书为JKS格式
# keytool -importcert -keystore /opt/nexus/etc/ssl/nexus.jks -file /opt/nexus/etc/ssl/nexus.crt -alias nexus
# 重启Nexus服务
# systemctl restart nexus
8. Nexus性能优化
在生产环境中,需要对Nexus进行性能优化以提高存储和检索效率。from:www.itpux.com
8.1 内存优化
# vi /opt/nexus/bin/nexus.vmoptions
-Xms4G
-Xmx8G
-XX:MaxDirectMemorySize=4G
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:+ParallelRefProcEnabled
-XX:+AlwaysPreTouch
# 重启Nexus服务
# systemctl restart nexus
8.2 磁盘优化
# vi /etc/sysctl.conf
vm.swappiness = 0
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
# 应用配置
# sysctl -p
# 优化XFS文件系统
# vi /etc/fstab
/dev/sdb1 /data/nexus xfs defaults,noatime,nodiratime 0 0
# 重新挂载
# mount -o remount /data/nexus
8.3 网络优化
# vi /etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15
# 应用配置
# sysctl -p
9. Nexus升级迁移
本节介绍Nexus的版本升级和数据迁移方法。
9.1 Nexus版本升级
# tar -czf /backup/nexus-$(date +%Y%m%d).tar.gz /opt/nexus /opt/sonatype-work/nexus3
# 停止Nexus服务
# systemctl stop nexus
# 下载新版本Nexus
# wget https://download.sonatype.com/nexus/3/nexus-3.58.0-01-unix.tar.gz
# 解压Nexus
# tar -xzf nexus-3.58.0-01-unix.tar.gz -C /opt/
# 更新软链接
# rm /opt/nexus
# ln -s /opt/nexus-3.58.0-01 /opt/nexus
# 修改权限
# chown -R nexus:nexus /opt/nexus
# 启动Nexus服务
# systemctl start nexus
# 验证升级
# 打开浏览器访问 http://nexus01:8081
9.2 Nexus数据迁移
# tar -czf /backup/nexus-data-$(date +%Y%m%d).tar.gz /opt/sonatype-work/nexus3
# 在新服务器上恢复数据
# tar -xzf /backup/nexus-data-20230405.tar.gz -C /opt/
# 安装Nexus
# 重复安装步骤
# 启动Nexus服务
# systemctl start nexus
# 验证迁移
# 打开浏览器访问 http://nexus01:8081
10. Nexus备份恢复
本节介绍Nexus的备份和恢复方法。
10.1 Nexus备份
# vi /data/nexus/scripts/backup.sh
#!/bin/bash
BACKUP_DIR=”/backup/nexus”
DATE=$(date +%Y%m%d)
# 创建备份目录
mkdir -p $BACKUP_DIR
# 停止Nexus服务
systemctl stop nexus
# 备份配置文件
cp -r /opt/nexus/etc $BACKUP_DIR/config-$DATE
# 备份数据
cp -r /opt/sonatype-work/nexus3 $BACKUP_DIR/data-$DATE
# 启动Nexus服务
systemctl start nexus
# 清理旧备份(保留7天)
find $BACKUP_DIR -type f -mtime +7 -exec rm -f {} \;
# 添加执行权限
# chmod +x /data/nexus/scripts/backup.sh
# 添加定时任务
# crontab -e
0 0 * * * /data/nexus/scripts/backup.sh
10.2 Nexus恢复
# systemctl stop nexus
# 清理现有数据
# rm -rf /opt/sonatype-work/nexus3
# 恢复数据
# cp -r /backup/nexus/data-20230405 /opt/sonatype-work/nexus3
# 恢复配置文件
# cp -r /backup/nexus/config-20230405/* /opt/nexus/etc/
# 启动Nexus服务
# systemctl start nexus
# 验证恢复
# 打开浏览器访问 http://nexus01:8081
10.3 Nexus监控脚本
# vi /data/nexus/scripts/monitor.sh
#!/bin/bash
LOG_FILE=”/var/log/nexus_monitor.log”
ALERT_EMAIL=”admin@fgedu.net.cn”
check_nexus_status() {
echo “$(date): Checking nexus status…” >> $LOG_FILE
status=$(systemctl status nexus | grep Active | awk ‘{print $2}’)
if [ “$status” != “active” ]; then
echo “$(date): Nexus is not running” >> $LOG_FILE
echo “Nexus is not running” | mail -s “Nexus Alert” $ALERT_EMAIL
systemctl start nexus
else
echo “$(date): Nexus is running” >> $LOG_FILE
fi
}
check_nexus_web() {
echo “$(date): Checking nexus web…” >> $LOG_FILE
response=$(curl -s -o /dev/null -w “%{http_code}” http://nexus01:8081)
if [ “$response” != “200” ]; then
echo “$(date): Nexus web is not accessible, response code: $response” >> $LOG_FILE
echo “Nexus web is not accessible, response code: $response” | mail -s “Nexus Alert” $ALERT_EMAIL
else
echo “$(date): Nexus web is accessible” >> $LOG_FILE
fi
}
check_disk_space() {
echo “$(date): Checking disk space…” >> $LOG_FILE
usage=$(df -h /opt/sonatype-work/nexus3 | tail -1 | awk ‘{print $5}’ | sed ‘s/%//’)
if [ $usage -gt 90 ]; then
echo “$(date): Disk space warning: /opt/sonatype-work/nexus3 is $usage% full” >> $LOG_FILE
echo “Disk space warning: /opt/sonatype-work/nexus3 is $usage% full” | mail -s “Nexus Alert” $ALERT_EMAIL
else
echo “$(date): Disk space: /opt/sonatype-work/nexus3 is $usage% full” >> $LOG_FILE
fi
}
main() {
check_nexus_status
check_nexus_web
check_disk_space
}
main
# 添加执行权限
# chmod +x /data/nexus/scripts/monitor.sh
# 添加定时任务
# crontab -e
*/15 * * * * /data/nexus/scripts/monitor.sh
通过以上步骤,Nexus安装配置、性能优化、升级迁移、备份恢复等内容已全部完成。Nexus作为开源仓库管理器,能够高效地存储和管理依赖包,是企业级开发的重要工具。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
