安裝 sonatype/nexus3:nuget maven docker k8s nginx(騷)


1.nexus的安裝 (linux 和 docker兩種安裝方式)重要參考

Sonatype Nexus3 Docker: sonatype/nexus3

Docker install:   https://hub.docker.com/r/sonatype/nexus3/

https://hub.docker.com/r/sonatype/nexus3/

利用 Docker 部署sonatype/nexus3 創建團隊私有 Maven 倉庫

docker-compose-nexus3-linux.yml

# 命令行執行:
# docker container prune #用來刪除不再使用的 docker 對象 

# mkdir -p /data/nexus3 && chown -R 200 /data/nexus3
# docker-compose -p nexus -f docker-compose-nexus3-linux.yml up -d

version: "3.6"
services:
  nexus:
    restart: always
    image: sonatype/nexus3
    container_name: nexus3
    environment:
      INSTALL4J_ADD_VM_PARAMS: |
        -Xms2g -Xmx2g -XX:MaxDirectMemorySize=3g
    ports:
      - 8081:8081
      - 8088:8088
      - 8089:8089
    volumes:
      - /data/nexus3:/nexus-data

# 訪問地址:http://123.56.57.211:8081/

# docker run -d -p 8081:8081 -p 8088:8088 -p 8089:8089 \
   --volume /data/nexus3:/nexus-data \
   --name nexus \
   -e INSTALL4J_ADD_VM_PARAMS="-Xms2g -Xmx2g -XX:MaxDirectMemorySize=3g" \
   sonatype/nexus3

docker-compose-nexus3-linux.yml
docker-compose-nexus3-linux.yml

docker install

#下面的暫時不用

#docker run -d -p 8081:8081 -p 8088:8088 -p 8089:8089 \
    --volume /data/nexus3:/opt/sonatype-work/nexus3 \
    --name nexus \
    -e INSTALL4J_ADD_VM_PARAMS=" \
      -Xms2g -Xmx2g -XX:MaxDirectMemorySize=3g \
      -Dkaraf.data=/opt/sonatype-work/nexus3 \
      -Djava.io.tmpdir=/opt/sonatype-work/nexus3/tmp \
      -XX:LogFile=/opt/sonatype-work/nexus3/log/jvm.log \
      -Dkaraf.log=/opt/sonatype-work/nexus3/log "\
    sonatype/nexus3

 

需要多等幾分鍾,才能正常訪問網頁:http://idocker.io:8081/ (配置hosts文件)

 

 nuget倉庫默認為空白的記錄:

 

 登錄admin賬戶修改默認密碼:

 

#docker exec  -it 1616c9443c43 /bin/bash
#cat /nexus-data/admin.password   #在容器里查看管理員密碼

 

 

 

 

修改默認存儲位置:(不修改也可以,默認就是在/nexus-data/的目錄下面)

/nexus-data/blogs/default-store

設置鏡像源:

cat /etc/docker/daemon.json 
{
  "registry-mirrors": [
    "https://hub-mirror.c.163.com"
  ],
  #linux上可以不用做下面的設置
  "insecure-registries": [
    "123.56.57.211:8088","123.56.57.211:8089"
  ]
}
/etc/docker/daemon.json

重啟docker服務

service docker restart

Maven私服搭建(Nexus Repository Manager 3)

1、maven-hosted(type:maven2 hosted)開發環境,我們運行重復發布,因此Delpoyment policy 我們選擇Allow redeploy。這個很重要!

2、maven-proxy (type: maven2 proxy)

https://repo1.maven.org/maven2/

3、maven-aliyun (type:maven2 proxy)

https://maven.aliyun.com/repository/central

 4、maven-aliyun-gradle-plugin(type:maven2 proxy)

https://maven.aliyun.com/repository/gradle-plugin

 

5、maven-aliyun-spring(type:maven2 proxy)

https://maven.aliyun.com/repository/spring

6、maven-gradle(type:proxy)

https://plugins.gradle.org/m2/

 

 7、maven-public(type:maven2 group)

設置maven配置文件使用本地倉庫

D:\maven\apache-maven-3.2.5\conf\settings.xml

<mirror>
      <id>mirrorId</id>
      <mirrorOf>*</mirrorOf>
      <name>192.168.0.112 for this Mirror.</name>
      <url>http://192.168.0.112/repository/maven-public/</url>
</mirror>

將項目源碼和部署的jar包上傳到私有倉

創建上傳release倉庫的角色

 

 創建上傳snapshot倉庫的角色

 創建上傳release用戶並選擇對應的角色

 

 創建上傳snapshot用戶並選擇對應的角色

 修改release倉庫設置,允許重新部署

 修改snapshot倉庫設置,允許重新部署

 本地開發機設置maven的settings.xml文件,使用nexus3的用戶名和密碼,注意:id很重要,和項目里的pom.xml對應

 項目里的pom.xml文件,設置遠程倉庫的地址,同時id值和上面保持一致。

在 idea開發工具里,使用maven,部署jar到nexus3的私有倉庫里

 release倉庫里的jar包,注意版本號那里

snapshot倉庫里的jar包,注意版本號那里

 

其他項目里引用私有倉庫里的jar

1、局部項目設置:(二選一)

   在其他項目的最外層的pom.xml文件增加私有倉庫的地址

2、全局設置:(二選一)

  在maven的settings.xml文件里,設置也可以

 3、項目里添加依賴

在某一個項目的pom.xml文件里添加jar依賴


NuGet Proxy Repositories

1、nuget-hosted (type: hosted)

 2、nuget.org-proxy (type:proxy)

nuget官方:https://www.nuget.org/api/v2/
博客園:https://nuget.cnblogs.com/v3/index.json

或者增加、換成博客園的都可以

3、nuget.org-proxy-v3 (type:proxy)

http://api.nuget.org/v3/index.json (不是https的)

 4、nuget-group (type: group)

 

設置vs里nuget包管理器,使用本地倉庫

http://192.168.0.82:18081/repository/nuget-group/  (建議使用,hosted和proxy都包含進去了)
http://192.168.0.82:18081/repository/nuget-hosted/ (使用這個只能看到本地上傳的文件,看不到其他第三方的)
http://8.131.227.221:18081/repository/nuget.org-proxy/ (使用v2的版本)
http://192.168.0.82:18081/repository/nuget.org-proxy-v3/index.json (使用v3的版本,路徑的基礎上還需要加index.json這個文件名)

 私有 nuget倉庫自動就添加了dll文件

 

ci用到的nuget.config文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="internal" 
      value="http://192.168.0.82:18081/repository/nuget.org-proxy-v3/index.json" 
      protocolVersion="3" />
  </packageSources>
</configuration>

如果私有倉設置了訪問密碼,如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
    <add key="nexus3_port" value="http://ipadress:port/repository/nuget-group/" />
  </packageSources>
  <packageSourceCredentials>
    <nexus3_port>
      <add key="Username" value="***" />
      <add key="ClearTextPassword" value="###" />
    </nexus3_port>
  </packageSourceCredentials>
</configuration>

將項目dll文件上傳到私有倉

官方文檔

添加Nuget Realms

因為Nexus認證Nuget是通過Realms來認證,因此,要添加Nuget Realms。

配置包屬性

在解決方案資源管理器中右鍵單擊該項目,然后選擇“屬性” 菜單命令,然后選擇“包” 選項卡

(可選)在生成期間生成包

可以將 Visual Studio 配置為在生成項目時自動生成 NuGet 包。

准備Package

 

獲取 API 密鑰

使用 dotnet CLI 或 nuget.exe CLI 發布

進入要發布的文件所在目錄里執行cmd

命令行里執行下面的命令

dotnet nuget push {dll}.nupkg --api-key {apiKey} --source http://{ipadress}:18081/repository/nuget-hosted/

發布成功之后,nexus3里面能看到最新上傳的dll文件

 


設置Docker倉庫和讀取docker倉庫的push和pull操作權限

2.使用nexus3配置docker私有倉庫

創建一個存儲,支持File和S3

1、docker-proxy(docker 代理,使用微軟地址)

  • docker-proxy-microsoft(type:proxy)
https://mcr.microsoft.com

 

 

 

  •  docker-proxy(type:proxy)
https://registry-1.docker.io

 

注意Docker Index: Use Docker Hub

 

 

  •  docker-proxy-elastic(type:proxy)
https://docker.elastic.co

 

 

 2、docker-hosted(gitlab docker push的地址:8088)

 

 3、docker-group(K8S docker pull的地址:8089)

 

 

創建Role:

1、nx-docker-pull:(繼承nx-repository-view-docker-*-的browser和read的角色)

2、nx-docker-push:(繼承:nx-repository-admin-docker-docker-hosted-*和nx-repository-view-docker-docker-hosted-*角色)

 或(nx-repository-admin-docker-docker)

創建User:

1、docker-pull:(注意要設置密碼)

 2、docker-push:(注意要設置密碼)

配置docker命令連接docker倉,因為group倉庫並不能推送鏡像,因為你推送自己制作的鏡像到倉庫還得通過本地倉庫的端口去推送,很不方便!

有一個解決方法:通過Nginx來判斷推鏡像還是拉鏡像,然后代理到不同端口。

需要使用HTTP的證書,安裝nginx,配置文件里面需要配置域名: ip地址 + port;還有https的域名證書

注意在nginx里面用的是:docker-hosted(gitlab docker push的地址:8088) 這個端口,這個用來負責接收推上來的image

 1 # ip地址可以換成內網ip
 2 upstream nexus_docker_get {
 3     server 127.0.0.1:8089;
 4 }
 5  
 6 upstream nexus_docker_put {
 7     server 127.0.0.1:8088;
 8 }
 9 server {
10     listen 80;
11     listen 443 ssl;
12     server_name idocker.io;
13     access_log /var/log/nginx/idocker.io.log;
14     # 證書
15     ssl_certificate /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.crt; # 證書路徑根據上面生成的來定
16     ssl_certificate_key /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.key.pem;
17     ssl_protocols TLSv1.1 TLSv1.2;
18     ssl_ciphers '!aNULL:kECDH+AESGCM:ECDH+AESGCM:RSA+AESGCM:kECDH+AES:ECDH+AES:RSA+AES:';
19     ssl_prefer_server_ciphers on;
20     ssl_session_cache shared:SSL:10m;
21     # disable any limits to avoid HTTP 413 for large image uploads
22     client_max_body_size 0;
23     # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
24     chunked_transfer_encoding on;
25     # 設置默認使用推送代理
26     set $upstream "nexus_docker_put";
27     # 當請求是GET,也就是拉取鏡像的時候,這里改為拉取代理,如此便解決了拉取和推送的端口統一
28     if ( $request_method ~* 'GET') {
29         set $upstream "nexus_docker_get";
30     }
31     # 只有本地倉庫才支持搜索,所以將搜索請求轉發到本地倉庫,否則出現500報錯
32     if ($request_uri ~ '/search') {
33         set $upstream "nexus_docker_put"; 
34     }
35     index index.html index.htm index.php;
36     location / {
37         proxy_pass http://$upstream;
38         proxy_set_header Host $host;
39         proxy_connect_timeout 3600;
40         proxy_send_timeout 3600;
41         proxy_read_timeout 3600;
42         proxy_set_header X-Real-IP $remote_addr;
43         proxy_buffering off;
44         proxy_request_buffering off;
45         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
46         proxy_set_header X-Forwarded-Proto http;
47     }
48 }
cat /etc/nginx/nginx.conf

2.使用nexus3配置docker私有倉庫(文章下面有nginx配置)

以下都是Linux操作:(windows10再往下)

配置沒有證書的信任,否則docker pull 獲取鏡像失敗

 

Error response from daemon: Get https://nexus3:8089/v2/: http: server gave HTTP response to HTTPS client

 

配置docker的后台服務文件:

 

vim /etc/docker/daemon.json

{
  "registry-mirrors": ["https://阿里雲的docker鏡像倉庫地址"]
  ,"insecure-registries": ["nexus3:8088","nexus3:8089"]
}
sudo systemctl restart docker
sudo systemctl daemon-reload

1、pull鏡像

#docker pull docker:stable

stable: Pulling from library/docker
188c0c94c7c5: Pull complete 
0c7c675703d6: Pull complete 
cc8c12a437cb: Pull complete 
c5dafad2182a: Pull complete 
5aa711733414: Pull complete 
058f73b55e4b: Pull complete 
8c9c664faf12: Pull complete 
Digest: sha256:9170b902404a630a982a2a6473442d3e2cc2342b66345f7a9cf148f8affcf5d3
Status: Downloaded newer image for docker:stable
docker.io/library/docker:stable

 2、登錄私服

docker login -u docker-push -p 123456 idocker.io

WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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

 3、打標簽

docker tag docker.io/library/docker:stable idocker.io/docker:stable
docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker              stable              6972c414f322        4 weeks ago         217MB
idocker.io/docker   stable              6972c414f322        4 weeks ago         217MB

 4、push鏡像

docker push idocker.io/docker:stable

The push refers to repository [idocker.io/docker]
1d278c39f50a: Pushed 
3730a0890c46: Pushed 
383f309b5d37: Pushed 
e1a73c32da76: Pushed 
31b11ec6ff38: Pushed 
911954e2fe49: Pushed 
ace0eda3e3be: Pushed 
stable: digest: sha256:279beeb5de99e09af79f13e85e20194ce68db4255e8b2d955e408be69d082b5a size: 1780

 這里上傳成功了,再去nexus3里邊看看是有上去了。

 5、測試從私服拉鏡像

docker rmi -f idocker.io/docker:stable
docker images

[root@iZ2zejfqk9lif5tpnjai4oZ nexus3]# docker rmi idocker.io/docker:stable
Untagged: idocker.io/docker:stable
Untagged: idocker.io/docker@sha256:279beeb5de99e09af79f13e85e20194ce68db4255e8b2d955e408be69d082b5a
[root@iZ2zejfqk9lif5tpnjai4oZ nexus3]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker              stable              6972c414f322        4 weeks ago         217MB
sonatype/nexus3     latest              d4fbb85e8101        4 weeks ago         634MB

docker pull idocker.io/docker:stable   #秒下完成

stable: Pulling from docker
Digest: sha256:9170b902404a630a982a2a6473442d3e2cc2342b66345f7a9cf148f8affcf5d3
Status: Downloaded newer image for idocker.io/docker:stable
idocker.io/docker:stable

6、代理功能展示

 當某一個鏡像在我們本地倉庫沒有的時候,就需要從遠程倉庫拉取了,其他的私有倉庫的操作大概都是要從遠程拉取,然后在重復如上操作推到本地私有倉庫,而nexus因為有了proxy功能,因此,當我們在pull遠程鏡像的時候,本地就會自動同步下來了。

以拉取gitlab鏡像為例:

docker pull idocker.io/gitlab/gitlab-ce:latest

latest: Pulling from gitlab/gitlab-ce
2c11b7cecaa5: Pull complete 
04637fa56252: Pull complete 
d6e6af23a0f3: Pull complete 
b4a424de92ad: Pull complete 
3c6e223a17cd: Pull complete 
b4f67e514b6e: Pull complete 
5ac452e41439: Pull complete 
64c3887f6641: Pull complete 
0e4c3bedc9a6: Pull complete 
849d4c8d3353: Waiting 
849d4c8d3353: Pull complete 
Digest: sha256:f6a1b040ca245c6e315aa6bc82f769324c4b7bb7c304fecc230e4818c7ed8aad
Status: Downloaded newer image for idocker.io/gitlab/gitlab-ce:latest
idocker.io/gitlab/gitlab-ce:latest

 

因為本地沒有這個鏡像,所以從遠程倉庫拉取,然后去私有倉庫里看看啥情況:

 

 經過查看可以發現:docker-hosted里沒有,docker-group和docker-proxy里有

 

 注意:刪除的話只能在docker-hosted或docker-proxy中刪除,當在這兩者中執行刪除操作后,docker-group里會自動沒有的(需要先登錄才能刪除)

登陸的時候若是提示這個錯誤:Error response from daemon: login attempt to https://idocker.io/v2/ failed with status: 401 Unauthorized
這是nexus版本問題,需要通過WEB管理端設置權限

windows10的docker操作

windows10 docker desktop 設置
{
  "registry-mirrors": [
    "https://hub-mirror.c.163.com"
  ],
  "insecure-registries": [
     "123.56.57.211:8088"
,"123.56.57.211:8089" ,"idocker.io:8088" ,"idocker.io:8089" ] } #powershell命令行操作,只寫域名不加端口不行,可以pull,login docker pull idocker.io:8089/dotnet/aspnet:5.0 docker pull 123.56.57.211:8089/dotnet/aspnet:5.0 docker login -u docker-push -p 123456 idocker.io:8088 docker login -u docker-push -p 123456 idocker.io:8089

C:\Users\Administrator\.docker\daemon.json

{
   "registry-mirrors": [
      "https://hub-mirror.c.163.com"
   ],
   "insecure-registries": [
      "nexus3:8088",
      "nexus3:8089"
   ],
   "allow-nondistributable-artifacts": [
      "nexus3:8088",
      "nexus3:8089"
   ],
   "debug": false,
   "experimental": false,
   "features": {
      "buildkit": true
   }
}

# debian命令行里docker pull可以,但是docker login不行的解決方法
# 報錯:
# Error saving credentials: error storing credentials - err: exec: "docker-credential-desktop.exe": executable file not found in $PATH, out: ``
# 解決方法:
# sudo ln -s /mnt/c/Program\ Files/Docker/Docker/resources/bin/docker-credential-desktop.exe /usr/bin/docker-credential-desktop.exe

 

至此,基本上關於使用nexus3搭建docker私有倉庫的知識點。


.gitlab-ci.yml

variables:
  # docker private rigist
  CI_REGISTRY: ip or domain
  CI_REGISTRY_USER: username
  CI_REGISTRY_PASSWORD: password

.docker_build:
  image: "docker:stable"
  services:
    - docker:stable-dind
  variables:
    DOCKER_TLS_CERTDIR: ""
  before_script:
    - echo "Logging to GitLab Container Registry with CI credentials..."
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"

docker push $image_name:latest

配置k8s命令連接docker倉,需要使用HTTP的證書,安裝nginx,配置文件里面需要配置域名: ip地址 + port;還有https的域名證書

注意在nginx里面用的是:docker-group(K8S docker pull的地址:8089)這個端口,這個用來負責拉取k8s需要用的image

yml文件

initContainers:
        - command:
            - /bin/bash
            - -c
          args:
            - if [ ! -d /app/mnt/templates ];
              then mkdir -p /app/mnt/templates;
              fi;
              cp -rf templates/* /app/mnt/templates;
              dotnet MeShop.CMD.INIT.dll
          image: {{values.chart.shop.image.registry}}/meshop/shop/init:{{values.chart.shop.image.tag}}
          imagePullPolicy: {{values.chart.shop.image.imagePullPolicy}}
          name: init-and-upgrade
          resources:
            requests:
              cpu: 200m
              memory: 200Mi
          volumeMounts:
            - name: configs
              mountPath: /app/k8s.json
              subPath: k8s.json
            - mountPath: /app/mnt/
              name: mnt

禁止匿名用戶訪問設置

1、禁止登陸,和下載jar包

 

 2、禁止匿名用戶訪問nexus頁面(看不到jar包,只能登陸后才能搜索對應的jar)

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM