Nexus3 搭建私有仓库 linux环境

引用地址:Sonatype Nexus3 搭建私有仓库_sonatype/nexus3_JontyWang的博客-CSDN博客

Nexus是Sonatype提供的仓库管理平台,Nuexus Repository OSS3能够支持Maven、npm、Docker、YUM、Helm等格式数据的存储和发布;并且能够与Jekins、SonaQube和Eclipse等工具进行集成。
Nexus支持作为宿主和代理存储库的本地Maven/Docker存储库,可以直接将这些存储库暴露给客户端工具;也可以以存储库组的方式暴露给客户端工具,存储库组是合并了多个存储库的内容的存储库,能够通过一个URL将多个存储库暴露给客户端工具,从而便于用户的使用。通过nexus自建能够有效减少访问获取镜像的时间和对带宽使用,并能够通过自有的镜像仓库共享企业自己的镜像。
SSL证书

需要 2 个域名,一个用来代理 Nexus 管理面板,另一个用做 docker 仓库,docker 需要单独的端口

Nexus 前台:registry.jonty.top

Docker 仓库:hub.jonty.top
自签名证书

我们通过Nginx代理Nexus服务,需要先生成自签名的SSL证书,通过内部DNS域名访问(无需在docker pull的时候还要带一个端口)

使用一键生成工具:ssl,两个域名都要签发证书

# 克隆仓库
git clone https://github.com/Fishdrowned/ssl.git
cd ssl
# 根据你的域名更改
/bin/bash ./gen.cert.sh hub.jonty.top

  

    PS:如果是打算做外网仓库服务,可以直接申请一个免费的SSL证书(云厂商都提供),本文使用内网域名,使用自签名证书

[root@nexus3 ssl-master]# ls
ca.cnf  docs  flush.sh  gen.cert.sh  gen.root.sh  LICENSE  out  README.md
[root@nexus3 ssl-master]# cd out/
[root@nexus3 out]# ls
cert.key.pem  index.txt  index.txt.attr  index.txt.attr.old  index.txt.old  newcerts  root.crt  root.key.pem  serial  serial.old
[root@nexus3 out]# cd ..
[root@nexus3 ssl-master]# /bin/bash gen.cert.sh hub.jonty.top # 换成你的域名
Using configuration from ./ca.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'CN'
stateOrProvinceName   :ASN.1 12:'Guangdong'
localityName          :ASN.1 12:'Guangzhou'
organizationName      :ASN.1 12:'Fishdrowned'
organizationalUnitName:ASN.1 12:'hub.jonty.top'
commonName            :ASN.1 12:'*.hub.jonty.top'
Certificate is to be certified until Jul 11 08:06:41 2024 GMT (730 days)

Write out database with 1 new entries
Data Base Updated

Certificates are located in:
lrwxrwxrwx. 1 root root 44 Jul 12 16:06 /root/docker/ssl-master/out/hub.jonty.top/hub.xxx.bundle.crt -> ./20220712-1606/hub.jonty.top.bundle.crt
lrwxrwxrwx. 1 root root 37 Jul 12 16:06 /root/docker/ssl-master/out/hub.jonty.top/hub.xxx.crt -> ./20220712-1606/hub.jonty.top.crt
lrwxrwxrwx. 1 root root 15 Jul 12 16:06 /root/docker/ssl-master/out/hub.jonty.top/hub.xxx.key.pem -> ../cert.key.pem
lrwxrwxrwx. 1 root root 11 Jul 12 16:06 /root/docker/ssl-master/out/hub.jonty.top/root.crt -> ../root.crt
[root@nexus3 ssl-master]# cd out/hub.jonty.top/
[root@nexus3 hub.jonty.top]# ls
20220712-1606  hub.jonty.top.bundle.crt  hub.jonty.top.crt  hub.jonty.top.key.pem  root.crt

   

阿里云签发

如果有域名,可以购买Aliyun免费ssl证书,一年有效期,可以有效避免自签名证书不适用的问题

image-20220801155820025

申请完成后,下载Nginx证书并上传到服务器

image-20220801160226471
本地域名解析

Windows:C:\Windows\System32\drivers\etc\hosts

Linux:vi /etc/hosts

将以下解析加入,测试是否可以ping通

192.168.2.xx hub.jonty.top
192.168.2.xx registry.jonty.top

  

    服务端和客户端都需要配置

部署
环境准备

安装Docker-Engine

    Install Docker Engine on CentOS | Docker Documentation

[root@nexus3 ~]# docker -v
Docker version 20.10.17, build 100c701
[root@nexus3 ~]# docker compose version
Docker Compose version v2.6.0

 

创建数据路径并设置权限

mkdir -p $PWD/nexus3/data
chmod 777 $PWD/nexus3/data
cd $PWD/nexus3

 

将生成的证书复制到$PWD/nexus3/certs目录下(2个域名的证书都需要)

[root@nexus3 hub.jonty.top]# cp hub.jonty.top.crt ~/nexus3/certs/
[root@nexus3 hub.jonty.top]# cp hub.jonty.top.key.pem ~/nexus3/certs/
[root@nexus3 hub.jonty.top]# cd ~/nexus3/certs/
[root@nexus3 certs]# ls
hub.jonty.top.crt hub.jonty.top.key.pem

 

docker-compose.yml

在~/nexus3目录下

version: "3.7"

services:
  nexus3:
    image: sonatype/nexus3:3.33.1
    container_name: nexus3
    restart: always
    privileged: true
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - $PWD/data:/nexus-data
      
  nginx:
    image: nginx:1.21.1-alpine
    container_name: nginx
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - $PWD/nginx.conf:/etc/nginx/nginx.conf:ro     # nginx配置
      - $PWD/certs:/etc/nginx/certs                    # SSL证书
      - $PWD/log:/var/log/nginx
    depends_on:
      - nexus3
    logging:
      driver: "json-file"
      options:
        max-size: "5g"  # 限制日志大小

nginx.conf

在同目录下创建nginx.conf,nginx配置文件

请参考ssl

worker_processes 4;
worker_rlimit_nofile 40000;

events {
    worker_connections 8192;
}

http {
    upstream nexus3_http {
        server nexus3:8081;
    }

    server{
        listen 80;
        server_name registry.jonty.top;
        return 301 https://$server_name$request_uri;
    }

    server {
        listen 443 ssl;
        server_name registry.jonty.top;
        
        # SSL
        ssl_certificate /certs/registry.jonty.top/registry.jonty.top.pem;
        ssl_certificate_key /certs/registry.jonty.top/registry.jonty.top.key;
        
        client_max_body_size 5000m;  # 上传大文件
        fastcgi_connect_timeout 300s;
        fastcgi_send_timeout 300s;
        fastcgi_read_timeout 300s;
        location / {
            proxy_pass http://nexus3_http;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }
    }

    upstream nexus_docker_get {
        server nexus3:8082;
    }
 
    upstream nexus_docker_put {
        server nexus3:8083;
    }
    
    server{
        listen 80;
        server_name hub.jonty.top;
        return 301 https://$server_name$request_uri;
    }

    server {
        listen 443 ssl;
        server_name hub.jonty.top;
        # 证书
        ssl_certificate /certs/hub.jonty.top/hub.jonty.top.pem;
        ssl_certificate_key /certs/hub.jonty.top/hub.jonty.top.key;
        ssl_protocols TLSv1.1 TLSv1.2;
        ssl_ciphers '!aNULL:kECDH+AESGCM:ECDH+AESGCM:RSA+AESGCM:kECDH+AES:ECDH+AES:RSA+AES:';
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        client_max_body_size 5000m;
        chunked_transfer_encoding on;
        set $upstream "nexus_docker_put";
        if ( $request_method ~* 'GET') {
            set $upstream "nexus_docker_get";
        }
        location / {
                proxy_pass http://$upstream;
                proxy_set_header Host $host;
                proxy_connect_timeout 3600;
                proxy_send_timeout 3600;
                proxy_read_timeout 3600;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_buffering off;
                proxy_request_buffering off;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto http;
        }
    }
}

 

查看目录

yum install -y tree
cd nexus3
tree -C -L 2
tree -C

 

image-20220805163921371

image-20220805164026553
运行

docker-compose up -d

 

image-20220712162731428

访问:https://registry.jonty.top

    Nexus初始化需要3~5min,初始化完成后可访问

image-20220712163303455
使用
密码配置

查看admin默认密码:

[root@nexus3 nexus3]# cat data/admin.password
098fb8d9-e07b-4f7f-b498-806cdce1291d

 

image-20220712164027694
Docker存储库
创建Blob存储

创建用于存放docker镜像文件的存储区域

image-20220712164443301
创建存储库

存储库有以下三种类型:

    proxy:表示代理仓库,请求包(package)的时候,如果本地有,它就从本地提供,如果本地没有,则从代理地址下载到本地,然后提供这个包。

    hosted:表示托管仓库,一般用于推送开发的包到该仓库。

    group:表示仓库组,它结合了proxy和hosted,能对外提供上述两者中的包,对外的出口

image-20220712164651836
hosted:本地仓库

创建hosted类型,用于存储本地推送的镜像

image-20220801170444372

端口设置为8083,对应nginx.conf配置nexus_docker_put
image-20220801170157413
proxy :代理仓库

代理官方源:https://registry-1.docker.io

image-20220805164336262

代理阿里云私有仓库(可公开拉取):

image-20220805164414466

如果代理的私有库需要授权:
image-20220801170854550
group : 仓库组

端口设置为8082,对应nginx.conf配置nexus_docker_get

编辑组成员,根据顺序可排优先级

image-20220805164454963
上传docker镜像
配置授信

    使用自签名证书需要配置此步骤

sudo vi /etc/docker/daemon.json

    1

加入以下配置

{  
   "insecure-registries":["https://hub.jonty.top"]
}

    1
    2
    3

重启docker

sudo systemctl restart docker

    1

    docker登录私库时提示 x509: certificate signed by unknown authority

登录

[root@nexus3 nexus3]# docker login hub.jonty.top -u admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

    1
    2
    3
    4
    5
    6
    7

推送镜像

[root@nexus3 nexus3]# docker images
REPOSITORY        TAG             IMAGE ID       CREATED         SIZE
nginx             1.21.1-alpine   1318bf5f63b4   10 months ago   22.8MB
sonatype/nexus3   3.33.1          a0d390a200d2   10 months ago   655MB
[root@nexus3 nexus3]# docker tag nginx:1.21.1-alpine hub.jonty.top/nginx:1.21.1-alpine
[root@nexus3 nexus3]# docker push hub.jonty.top/nginx:1.21.1-alpine
The push refers to repository [hub.jonty.top/nginx]
45d993692050: Pushed
1ea998b95474: Pushed
95b99a5c3767: Pushed
fc03e3cb8568: Pushed
24934e5e6c61: Pushed
e2eb06d8af82: Pushed
1.21.1-alpine: digest: sha256:bd0aa91fe6a182db22032463c17644cd2ff3bbe415e7b84964283bba687acaa6 size: 1568

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

image-20220712170930285
拉取镜像

    注意,客户端需要配置授信和域名解析,如果开启允许匿名拉取则不需授信,配置了路由器DNS则不需要配置hosts文件

[root@test ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.2.xx hub.jonty.top
192.168.2.xx registry.jonty.top
[root@test ~]# cat /etc/docker/daemon.json
{
  "insecure-registries": ["https://hub.jonty.top"]
}
[root@test ~]# docker pull hub.jonty.top/mssql:2019-latest
2019-latest: Pulling from mssql
d5fd17ec1767: Already exists
cf291b38357f: Pull complete
af7e8d6f1719: Pull complete
Digest: sha256:584a7fc7e2a378bdd4e8fe3bad36dae18e85527308193cb5c43d90863d7a7d4a
Status: Downloaded newer image for hub.jonty.top/mssql:2019-latest
hub.jonty.top/mssql:2019-latest

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

Gitlab-CI

    在 Docker 容器中运行 CI/CD 作业|GitLab

[root@nexus3 ~]# cat ~/.docker/config.json
{
    "auths": {
        "https://index.docker.io/v1/": {
            "auth": "am9udHltYXg6Sm9udHlNYXgzMjE="
        },
        "hub.jonty.top": {
            "auth": "YWRtaW46Z2N0bmV4dXMz"
        }
    }
}[root@nexus3 ~]#
[root@nexus3 ~]# echo -n "admin:nexus3" | base64
YWRtaW46Z2N0bmV4dXMz

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

CI配置:

variables:
  DOCKER_AUTH_CONFIG: '{"auths": {"hub.jonty.top": {"auth": "YWRtaW46SGFyYm9yMTIzNDU="}}}'

    1
    2

设置匿名拉取

按需开启,开启则不需要配置以上授信部分
image-20220727093218720
NuGet存储库
上传nuget包

Nexus默认已经创建好了Nuget的仓库,并且是创建了3个不同类型的仓库:nuget-group,nuget-hosted,nuget.org-proxy

image-20220712172140135
添加Nuget Realms

Nexus认证Nuget是通过Realms来认证

image-20220712172321065
获取仓库地址以及Nuget API Key

仓库列表>复制地址

image-20220712172440616

获取NuGet API Key

image-20220712172607085
推送本地NuGet包

dotnet nuget push .\abp.7.2.1.nupkg -k 238d37fc-9fae-335d-a812-29c2799d8f0e --source https://registry.jonty.top/repository/nuget-hosted/
正在将 abp.7.2.1.nupkg 推送到 'https://registry.jonty.top/repository/nuget-hosted/'...
  PUT https://registry.jonty.top/repository/nuget-hosted/
  Created https://registry.jonty.top/repository/nuget-hosted/ 288 毫秒
已推送包。

    1
    2
    3
    4
    5

image-20220712172736234
配置本地NuGet包源

在VS中添加了本地源

image-20220712172917172

image-20220712173030239
新增Nuget代理

    代理公网的私有源,如Nuget、Gitlab

启用NuGet V3版本

image-20220805165042203

私有源授权image-20220801173138755
添加仓库组

image-20220805165114055
NuGet V3

正常访问nuget-group:

https://registry.jonty.top/repository/nuget-group/

    1

使用V3版本需要添加

https://registry.jonty.top/repository/nuget-group/index.json

    1

使用私有NuGet源

如果是代理私有库,先删除本地源

dotnet nuget list source
dotnet nuget remove source <NAME>

    1
    2

添加私有源

dotnet nuget add source https://registry.jonty.top/repository/nuget-group/index.json -n nexus3 -u admin -p nexus3 --store-password-in-clear-text

    1

清空本地nuget缓存

dotnet nuget locals http-cache --clear
dotnet nuget locals global-packages --clear

    1
    2

    当代理的源更新后,Nexus本地缓存会导致无法找到最新包,可手动清理

image-20220801164922446
NPM存储库
NPM代理

官方源:

https://registry.npmjs.org

image-20220801175628966

私有库授权:

image-20220801175743567
NPM仓库组

image-20220805165359080
使用NPM代理
配置代理

npm i --legacy-peer-deps  -verbose

# 查看npm源
npm config get registry

# 注册私有源
npm config set registry https://registry.jonty.top/repository/npm-group

# 登录私服
npm login registry="https://registry.jonty.top/repository/npm-group/"  

npm cache clean --force

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    npm 新版本 -g需要替换为--location=global

# 查看yarn配置
yarn config list

# 配置私有源
yarn config set registry https://registry.jonty.top/repository/npm-group/

yarn cache clean

    1
    2
    3
    4
    5
    6
    7

查看密钥

查看私服密钥

[root@nexus3 ~]# cat ~/.npmrc
registry=http://registry.jonty.top/repository/npm-group/
//registry.jonty.top/repository/npm-group/:_authToken=NpmToken.72b83be3-4b24-3dd1-850f-056cd78bb513

    1
    2
    3

.npmrc

@delivery:registry=https://registry.jonty.top/repository/npm-group/
//registry.jonty.top/repository/npm-group/:_authToken=NpmToken.612e0fd9-1526-3acd-9165-4e604d49a73d
always-auth=true

    1
    2
    3

DevOps
目的

Deeper look into the basic CI/CD workflow

主要是配合Gitlab Runner CI/CD编译打包

    Runner执行流程

Runner执行流程
效果

后端项目从平均12min提升到3min以内,包括拉取代码、执行还原、编译打包、推送等操作

前端项目从平均大于10min(30min也很常见)提升到7min以内,restore速度很快,主要是build操作缓慢,并且随着依赖增多变得更慢

使用前

ci-before

使用后

ci-after

    搞定~

挠屁股
其他问题
502 Bad Gateway

配置 nexus3 时使用 http 而非 https
no basic auth credentials

需要先 docker login 登录
401 Unauthorized

docker login -u admin -p Harbor12345 hub.haifengat.com 登录时报错

img

参考文档:

解决Error response from daemon: Get https://: http: server gave HTTP response to HTTPS client_SerryYang的博客-CSDN博客

docker登录私库时提示 x509: certificate signed by unknown authority_舟行于无涯之海的博客-CSDN博客_docker login x509
————————————————
版权声明:本文为CSDN博主「JontyWang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44251065/article/details/126851104

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Nexus搭建 Go 私有库,需要执行以下步骤: 1. 安装 Nexus:首先需要按照 Nexus 的安装指南进行安装,可以选择从官网下载安装包,也可以使用 Docker 安装。 2. 创建一个 Go 存储库:在 Nexus 界面上,单击“Repositories”选项卡,然后单击“Create Repository”按钮。在弹出的对话框中,选择“Go(Proxy)”或“Go(Hosted)”或“Go(Group)”,具体选择哪个取决于你的需求。 - Go(Proxy)存储库:用于代理 Go 中央仓库(https://proxy.golang.org)。这是一个只读的存储库,可以缓存 Go 依赖项,并提供更快的下载速度。 - Go(Hosted)存储库:用于存储你的私有 Go 模块。这是一个可读可写的存储库。 - Go(Group)存储库:用于将多个存储库组合在一起,以便从单个 URL 中获取依赖项。这是一个只读存储库。 3. 配置 Go 环境变量:需要将 GOPROXY 环境变量设置为你的 Nexus Go 存储库的 URL。例如,如果你的 Nexus URL 是 https://nexus.example.com,那么将 GOPROXY 设置为 https://nexus.example.com/repository/go-proxy/。 4. 将代码上传到 Nexus:使用 go mod 命令构建你的代码,并将其发布到 Nexus。你可以使用以下命令将代码发布到 Nexus: ``` go mod init example.com/your-project go mod tidy go mod vendor curl -v -u username:password --upload-file ./vendor.tar.gz https://nexus.example.com/repository/go-hosted/example.com/your-project/vendor.tar.gz ``` 其中,username 和 password 分别是 Nexus 的用户名和密码。 5. 下载依赖项:使用 go mod 命令下载你的依赖项。当你运行 go build 或 go run 命令时,Go 将从 Nexus 中央仓库或你的私有存储库中获取依赖项。可以使用以下命令下载依赖项: ``` go mod download ``` 6. 完成:完成上述步骤后,你就可以使用 Nexus 搭建私有库来管理你的 Go 依赖项了。 希望这些步骤能够帮助你成功搭建 Go 私有库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值