Nexus自建本地仓库

一 背景介绍

有的生产环境中有访问限制不允许服务器访问公网,或者是混合云架构IDC资源是纯内网。这些情况下服务器无法直接安装软件,只能通过拷贝或者走代理方式解决软件安装问题。
但服务器资源量大就无法人肉一台台操作,此时自建Nexus仓库既能保持安全合规也能解决这类问题。

  • Nexus介绍
    nexus全称Nexus Repository Manager,是一个强大的仓库管理器,极大简化了内部仓库的维护和外部仓库的访问。
    可用来做maven、docker、pypi、yum、npm、apt等的私有仓库,功能很强大。

二 Nexus安装(Linux)

  • 安装jdk环境

    1
    2
    3
    yum install java -y
    # java-1.8.0-openjdk-1.8.0.302.b08-0.el7_9.x86_64
    # 这里图简单直接用自带openjdk安装,建议使用oracle官网下载jdk安装
  • 下载nexus源码包

    1
    2
    3
    https://help.sonatype.com/repomanager3/download
    # 按需下载,此处下载unix archive
    wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz
  • 安装nexus

1
2
3
4
5
6
7
8
9
10
11
# 解压
cd /data/softapps
tar -zxvf latest-unix.tar.gz

# 添加环境变量相关
echo 'NEXUS_HOME="/data/softapps/nexus-3.33.0-01"' >> ~/.bashrc
echo 'run_as_user="root"' >> /data/softapps/nexus-3.33.0-01/bin/nexus.rc

# 默认端口8081,如存在冲突可自定义端口
vim nexus-3.33.0-01/etc/nexus-default.properties
修改: application-port=8080
  • 配置系统服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vim /etc/systemd/system/nexus.service

[Unit]
Description=nexus service
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/data/softapps/nexus-3.33.0-01/bin/nexus start
ExecStop=/data/softapps/nexus-3.33.0-01/bin/nexus stop
User=root
Restart=on-abort

[Install]
WantedBy=multi-user.target
  • 启动登录
1
2
3
4
5
6
7
8
systemctl start nexus
# java程序启动你懂的,大约需要10秒后可以启动完成,监听在上述设置的端口。如有报错查看日志对症处理

# 浏览器访问
http://ip:8080

# 查看密码,登录后修改密码
cat /data/softapps/sonatype-work/nexus3/admin.password

三 配置Nexus

1
2
3
4
创建步骤概述
a. 创建blob存储,为其创建单独的存储空间
b. 创建proxy类型的仓库,可创建多个
c. 创建group类型仓库,选择上述2个类型仓库,暴露给用户使用
  • Yum仓库
1
2
3
4
5
6
7
# nexus仓库配置
a. 创建blob,命名yum,如下图所示
b. 创建proxy Repositories,选择Recipe:yum(proxy);Name:yum-proxy;Proxy: 添加需要添加的仓库地址,这里是腾讯云内网yum源;Storage:yum,选择上面创建的blob
c. 创建group Repositories,选择Recipe:yum(group);Name: yum-group; 选择Blob:yum;添加Group:选择yum-proxy到Members里

yum:http://mirrors.tencentyun.com/centos/
epel:http://mirrors.tencentyun.com/epel/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 内网服务器上设置
[root@VM-12-38-centos yum.repos.d]# cat centos-base.repo
[nexus]
name=Nexus Yum Repository
gpgkey=http://10.228.20.78:8080/repository/yum-group/RPM-GPG-KEY-EPEL-7
gpgcheck=1
baseurl=http://10.228.20.78:8080/repository/yum-group/$releasever/os/$basearch/
enabled=1
gpgcheck=0

[root@VM-12-38-centos yum.repos.d]# cat centos-epel.repo
[epel]
name=EPEL for redhat/centos $releasever - $basearch
failovermethod=priority
gpgcheck=1
gpgkey=http://10.228.20.78:8080/repository/yum-group-epel/RPM-GPG-KEY-EPEL-7
enabled=1
baseurl=http://10.228.20.78:8080/repository/yum-group-epel/$releasever/$basearch/
  • pypi
1
2
3
4
5
6
# nexus仓库配置
a. 创建blob,命名pypi
b. 创建Repository,选择pypi(proxy),命名自定义。
c. 创建Repository,选择pypi(group),命名自定义 关联上面pypi-proxy

pypi:http://mirrors.tencentyun.com/pypi/
1
2
3
4
5
6
# 客户端配置
# 默认没.pip文件夹,需先创建
[root@liyk ~]# cat ~/.pip/pip.conf
[global]
trusted-host = 10.228.20.78
index-url = http://10.228.20.78:8080/repository/group-pypi/simple

  • npm
1
2
3
4
5
6
7
# nexus仓库配置
a. 创建blob,命名npm
b. 创建Repository,选择npm(proxy),命名自定义。
c. 创建Repository,选择npm(group),命名自定义 关联上面npm-proxy

npm:http://mirrors.cloud.tencent.com/npm/
淘宝npm:https://registry.npm.taobao.org
1
2
# 客户端配置
npm config set registry http://10.228.20.78:8080/repository/group-npm/npm/
-------------本文结束感谢您的阅读-------------
原创技术分享,感谢您的支持。