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


總結:

客戶端主機操作
1.echo "192.168.0.254 idocker.io" >> /etc/hosts
2.mkdir -p /etc/docker/certs.d/idocker.io
3.把192.168.0.254主機上的/etc/nginx/ssl/out/root.crt復制到客戶端的/etc/docker/certs.d/idocker.io目錄下
4.客戶端使用:
(1) 登陸:docker login -u admin -p newnode7852 idocker.io
(2) 打標簽:docker tag image:label idocker.io/image:label
(3) 推送:docker push idocker.io/image:label
(4) 拉取:docker pull idocker.io/image:label

1,配置

1,創建blob存儲

登陸之后,先創建一個用於存儲鏡像的空間。

定義一個name,下邊的內容會自動補全。

然后保存。

注意:實際生產中使用,建議服務器存儲500G或以上。

2,創建一個hosted類型的docker倉庫

Hosted類型倉庫用作我們的私有倉庫,替代harbor的功能。

點擊步驟如下:

而后可見所支持種類之豐富,可見一斑。

這里我們看到docker類型有三種:

  • hosted : 本地存儲,即同 docker 官方倉庫一樣提供本地私服功能。
  • proxy : 提供代理其他倉庫的類型,如 docker 中央倉庫。
  • group : 組類型,實質作用是組合多個倉庫為一個地址。

先來創建一個hosted類型的私有倉庫。

點擊 Repository下面的 Repositories – Create repository – docker(hosted) :

Name: 定義一個名稱docker-local

Online: 勾選。這個開關可以設置這個Docker repo是在線還是離線。

Repository Connectors
  • 下面包含HTTP和HTTPS兩種類型的port。

  • 有什么用呢?說明講得很清楚:

  • 連接器允許docker客戶端直接連接到docker倉庫,並實現一些請求操作,如docker pull, docker push, API查詢等。但這個連接器並不是一定需要配置的,尤其是我們后面會用group類型的docker倉庫來聚合它。

我們把HTTP這里勾選上,然后設置端口為8083。

Allow anonymous docker pull 

不勾選。這樣的話就不允許匿名訪問了,執行docker pull或 docker push之前,都要先登錄:docker login

Docker Registry API Support

Docker registry默認使用的是API v2, 但是為了兼容性,我們可以勾選啟用API v1。

Storage

Blob store:我們下拉選擇前面創建好的專用blob:docker-hub。

Hosted

開發環境,我們運行重復發布,因此Delpoyment policy 我們選擇Allow redeploy。

整體配置截圖如下:

3,創建一個proxy類型的docker倉庫

proxy類型倉庫,可以幫助我們訪問不能直接到達的網絡,如另一個私有倉庫,或者國外的公共倉庫,如官方的dockerhub鏡像庫。

創建一個proxy類型的倉庫

Name: proxy-docker-hub

Repository Connectors: 不設置。

Proxy

Remote Storage: docker hub的proxy,這里填寫: https://registry-1.docker.io 這個是官方默認的一個鏈接

Docker Index: Use Docker Hub

Storage:idocker-hub

整體配置截圖如下:


4,創建一個group類型的docker倉庫

group類型的docker倉庫,是一個聚合類型的倉庫。它可以將前面我們創建的3個倉庫聚合成一個URL對外提供服務,可以屏蔽后端的差異性,實現類似透明代理的功能。

name:docker-group

Repository Connectors:啟用了一個監聽在8082端口的http連接器;

Storage:選擇專用的blob存儲idocker-hub。

group : 將左邊可選的3個倉庫,添加到右邊的members下。

整體配置截圖如下:

最終效果

到這兒,nexus在docker這一塊是部署已經完成了,但是這樣並不能很好的使用。因為group倉庫並不能推送鏡像,因為你推送自己制作的鏡像到倉庫還得通過本地倉庫的端口去推送,很不方便!

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

5,nginx代理方式訪問倉庫

在部署 Nginx 部分,需要先生成自簽名 SSL 證書,因為后面不想在 docker pull 的時候還要帶一個端口!這里需要 2 個域名,一個用來展示 nexus 前台,另一個用做 docker 倉庫,比如:

  • nexus 前台:repo.ald.com
  • docker 倉庫:idocker.io

1.安裝nginx

yum -y install nginx

2.生成證書

這里推薦一個一鍵生成工具,大家可以嘗試使用:https://github.com/Fishdrowned/ssl ,使用方法請參考作者說明。

Ps:如果你打算做外網倉庫服務,那也可以去申請一個免費SSL證書,我這邊是內部oa域名使用,所以只能用自簽名證書了。

創建證書方式如下:

#直接切換到應用目錄
# cd /etc/nginx/conf.d/
 
#下載工具
# git clone https://github.com/Fishdrowned/ssl.git
Cloning into 'ssl'...
remote: Enumerating objects: 106, done.
remote: Total 106 (delta 0), reused 0 (delta 0), pack-reused 106
Receiving objects: 100% (106/106), 171.53 KiB | 286.00 KiB/s, done.
Resolving deltas: 100% (48/48), done.
 
#生成證書
# cd ssl
# ./gen.cert.sh idocker.io

Removing dir out
Creating output structure
Done
Generating a 2048 bit RSA private key
......+++
......................................................................................................................+++
writing new private key to 'out/root.key.pem'
-----
Generating RSA private key, 2048 bit long modulus
...............................................................................+++
.................................+++
e is 65537 (0x10001)
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:'idocker.io'
commonName            :ASN.1 12:'*.idocker.io'
Certificate is to be certified until Jun 12 04:29:18 2022 GMT (730 days)

Write out database with 1 new entries
Data Base Updated

Certificates are located in:
lrwxrwxrwx 1 root root 37 6月  12 12:29 /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.bundle.crt -> ./20200612-1229/idocker.io.bundle.crt
lrwxrwxrwx 1 root root 30 6月  12 12:29 /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.crt -> ./20200612-1229/idocker.io.crt
lrwxrwxrwx 1 root root 15 6月  12 12:29 /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.key.pem -> ../cert.key.pem
lrwxrwxrwx 1 root root 11 6月  12 12:29 /etc/nginx/conf.d/ssl/out/idocker.io/root.crt -> ../root.crt

3.配置nginx

# ip地址可以換成內網ip
upstream nexus_docker_get {
    server 192.168.75.11:8082;
}
 
upstream nexus_docker_put {
    server 192.168.75.11:8083;
}
server {
    listen 80;
    listen 443 ssl;
    server_name idocker.io;
    access_log /var/log/nginx/idocker.io.log;
    # 證書
    ssl_certificate /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.crt; # 證書路徑根據上面生成的來定
    ssl_certificate_key /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.key.pem;
    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;
    # disable any limits to avoid HTTP 413 for large image uploads
    client_max_body_size 0;
    # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
    chunked_transfer_encoding on;
    # 設置默認使用推送代理
    set $upstream "nexus_docker_put";
    # 當請求是GET,也就是拉取鏡像的時候,這里改為拉取代理,如此便解決了拉取和推送的端口統一
    if ( $request_method ~* 'GET') {
        set $upstream "nexus_docker_get";
    }
    # 只有本地倉庫才支持搜索,所以將搜索請求轉發到本地倉庫,否則出現500報錯
    if ($request_uri ~ '/search') {
        set $upstream "nexus_docker_put"; 
    }
    index index.html index.htm index.php;
    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;
    }
}

nginx -t 檢查沒有問題的話,就可以啟動nginx了。

4.客戶端配置

部署完成之后,我們就可以找一台測試機器進行測試了,不過因為我們剛剛定義的是內部使用的域名,所以需要在測試機器上寫hosts解析,並將證書拷貝過去,否則會報不信任的錯誤。

在上文介紹的一鍵生成自簽名工具中,會生成一個根證書,名稱為/etc/nginx/conf.d/ssl/out/idocker.io/root.crt,我們將這個文件上傳到客戶端服務器的 /etc/docker/certs.d/idocker.io 目錄即可(注意目錄需要創建,最后的文件夾名稱和倉庫域名保持一致:idocker.io)。

現在到一台新主機192.168.75.10上測試:

# 主機192.168.75.10上的操作
echo "192.168.75.11 idocker.io" >> /etc/hosts
mkdir -p /etc/docker/certs.d/idocker.io

# 然后去nexus主機上,將剛才的證書拷過來
scp root.crt root@192.168.75.10:/etc/docker/certs.d/idocker.io

接下來,就可以開始真正的使用了。

6,正式驗證

1,pull鏡像

[root@master ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
8559a31e96f4: Pull complete 
85a6a5c53ff0: Pull complete 
b69876b7abed: Pull complete 
a72d84b9df6a: Pull complete 
5ce7b314b19c: Pull complete 
04c4bfb0b023: Pull complete 
Digest: sha256:800f2587bf3376cb01e6307afe599ddce9439deafbd4fb8562829da96085c9c5
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

2,登陸私服

這個地方也可能在登陸的時候會報錯,說證書過期什么的,如下:

Error response from daemon: Get https://idocker.io/v1/users/: x509: certificate has expired or is not yet valid

報這個錯的情況下,大概原因只有一個,那就是,兩台服務器的時間不一致,只需要將兩台服務器時間保持一致即可。

yum -y install ntpdate && ntpdate -u cn.pool.ntp.org

分別在兩台主機執行之后,發現登陸就成功了。

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

[root@master ~]# docker login -u admin -p admin 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/redis:latest idocker.io/nginx

4,push鏡像

[root@master ~]# docker push idocker.io/nginx 
The push refers to repository [idocker.io/nginx]
7b9c5be81844: Pushed 
67c707dbd847: Pushed 
72d3a7e6fe02: Pushed 
cdaf0fb0082b: Pushed 
e6b49c7dcaac: Pushed 
13cb14c2acd3: Pushed 
latest: digest: sha256:76ff608805ca40008d6e0f08180d634732d8bf4728b85c18ab9bdbfa0911408d size: 1572

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

5,測試從私服拉鏡像

[root@master ~]# docker images
REPOSITORY                                                                    TAG                 IMAGE ID            CREATED             SIZE
redis                                                                         latest              235592615444        43 hours ago        104MB
idocker.io/redis                                                              latest              235592615444        43 hours ago        104MB
[root@master ~]# docker rmi idocker.io/redis 
Untagged: idocker.io/redis:latest
Untagged: idocker.io/redis@sha256:76ff608805ca40008d6e0f08180d634732d8bf4728b85c18ab9bdbfa0911408d
[root@master ~]# docker images
REPOSITORY                                                                    TAG                 IMAGE ID            CREATED             SIZE
redis                                                                         latest              235592615444        43 hours ago        104MB
[root@master ~]# docker pull idocker.io/redis    
Using default tag: latest
latest: Pulling from redis
Digest: sha256:76ff608805ca40008d6e0f08180d634732d8bf4728b85c18ab9bdbfa0911408d
Status: Downloaded newer image for idocker.io/redis:latest
idocker.io/redis:latest
[root@master ~]# docker images
REPOSITORY                                                                    TAG                 IMAGE ID            CREATED             SIZE
redis                                                                         latest              235592615444        43 hours ago        104MB
idocker.io/redis                                                              latest              235592615444        43 hours ago        104MB

7,代理的功能展示

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

以拉取gitlab鏡像為例:

docker pull idocker.io/gitlab/gitlab-ce
Using default tag: latest
Trying to pull repository docker.io/gitlab/gitlab-ce ...
latest: Pulling from docker.io/gitlab/gitlab-ce
3b37166ec614: Pull complete
504facff238f: Pull complete
ebbcacd28e10: Pull complete
c7fb3351ecad: Pull complete
2e3debadcbf7: Pull complete
8e5e9b12009c: Pull complete
0720fffe6e22: Pull complete
2f336a213238: Pull complete
1656ee3e1127: Pull complete
25fa5248fd38: Pull complete
36b8c1d869a0: Pull complete
Digest: sha256:0dd22880358959d9a9233163147adc4c8f1f5d5af90097ff8dfa383c6be7e25a
Status: Downloaded newer image for docker.io/gitlab/gitlab-ce:latest

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

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

注意:刪除的話只能在docker-local或proxy-docker-hub中刪除,當在這兩者中執行刪除操作后,docker-group里會自動沒有的

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


免責聲明!

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



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