docker私服搭建nexus3


docker私服搭建有官方的registry鏡像,也有改版后的NexusOss3.x,因為maven的原因搭建了nexus,所以一並將docker私服也搭建到nexus上。

nexus的安裝過程就單獨說了,如果是2.x系列需要升級到2.14版本再升級到3.y系列,如果3.x到3.y直接升級就可以。

從3.0版本開始,nexus不再只是一個maven倉庫,還可以是docker、npm、bower的私有倉庫。

配置SSL

docker的倉庫鏈接是基於HTTPS的,故一般情況下需要將nexus的訪問方式改為支持https。
配置SSL主要的難點在於證書,證書可以用公網證書,而一般情況下,私服部署在內網,沒有域名,用內網IP訪問,這種情況下用自簽名的證書是最好的選擇。證書生成工具用jdk自帶的就可以。
配置過程,進入${nexus}/etc/ssl目錄下面,執行命令過程中會輸入多次密碼,記下密碼,后面有用處:

$ keytool -genkeypair -keystore example.jks -storepass password -alias example.com \
>  -keyalg RSA -keysize 2048 -validity 5000 -keypass password \
>  -dname 'CN=*.example.com, OU=Sonatype, O=Sonatype, L=Unspecified, ST=Unspecified, C=US' \
>  -ext 'SAN=DNS:nexus.example.com,DNS:clm.example.com,DNS:repo.example.com,DNS:www.example.com'

Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore example.jks -destkeystore example.jks -deststoretype pkcs12".
$ keytool -exportcert -keystore example.jks -alias example.com -rfc > example.cert
Enter keystore password:  password

Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore example.jks -destkeystore example.jks -deststoretype pkcs12".
$ keytool -importkeystore -srckeystore example.jks -destkeystore example.p12 -deststoretype PKCS12
Importing keystore example.jks to example.p12...
Enter destination keystore password:  
Re-enter new password: 
Enter source keystore password:  
Entry for alias example.com successfully imported.
Import command completed:  1 entries successfully imported, 0 entries failed or cancelled
$ openssl pkcs12 -nocerts -nodes -in example.p12 -out example.key
Enter Import Password:
MAC verified OK

修改配置文件:etc/nexus.properties

原:
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
修改后:
application-port-ssl=8433
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-https.xml,${jetty.etc}/jetty-requestlog.xml

修改配置文件:etc/jetty/jetty-https.xml

 <New id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
    <Set name="KeyStorePath"><Property name="ssl.etc"/>/keystore.jks</Set> // 文件名和前面證書的一致
    <Set name="KeyStorePassword">password</Set> // 密碼用前面的
    <Set name="KeyManagerPassword">password</Set>
    <Set name="TrustStorePath"><Property name="ssl.etc"/>/keystore.jks</Set>
    <Set name="TrustStorePassword">password</Set>
    <Set name="EndpointIdentificationAlgorithm"></Set>
    <Set name="NeedClientAuth"><Property name="jetty.ssl.needClientAuth" default="false"/></Set>
    <Set name="WantClientAuth"><Property name="jetty.ssl.wantClientAuth" default="false"/></Set>
    <Set name="ExcludeCipherSuites">
      <Array type="String">
        <Item>SSL_RSA_WITH_DES_CBC_SHA</Item>
        <Item>SSL_DHE_RSA_WITH_DES_CBC_SHA</Item>
        <Item>SSL_DHE_DSS_WITH_DES_CBC_SHA</Item>
        <Item>SSL_RSA_EXPORT_WITH_RC4_40_MD5</Item>
        <Item>SSL_RSA_EXPORT_WITH_DES40_CBC_SHA</Item>
        <Item>SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA</Item>
        <Item>SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA</Item>
      </Array>
    </Set>
  </New>

重啟nexus,打開瀏覽器訪問,https://ip:8443/ 會提示出來證書簽名有問題,直接略過就行。

創建倉庫(私有,代理,組合)

創建三個倉庫,如果需要代理多個來源,可以創建多個代理庫。
私有倉庫:注意將http或者https的端口打開。

代理中央庫的注意地址:
remote: https://registry-1.docker.io
docer index : use Docker hub

組合倉庫將剛建立的Hosted和Proxy倉庫都加入就可以了。

配置docker本地

本地配置最主要的是配置insecure-registries和docker login。

配置daemon.json

/etc/docker/daemon.json

{
  "registry-mirrors": ["https://IP:18443"],
  "insecure-registries":["IP:18443","IP:18444"]
}

為什么需要兩個insecure-registry呢?因為group倉庫不可以作為push倉庫,如果單純的進行pull,可以只配置一個。

docker login

分別login兩個insecure-registry。

$ docker login IP:18444
Username (admin): admin
Password: 
Login Succeeded
$ docker login IP:18443
Username (admin): admin
Password: 
Login Succeeded

這樣可以愉快的進行push和pull操作了。

push和pull的坑

nexus有一個設定,push只能對Hosted,pull從三種倉庫都可以。因為push的操作給proxy的,是無法推送給被代理庫的。group的倉庫接受push后,無法確定是給proxy還是hosted。
但是nexus可以通過push給hosted的倉庫,但是通過group倉庫pull。

$ docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
alpine                               latest              3fd9065eaf02        5 months ago        4.15MB
IP:18443/alpine            latest              3fd9065eaf02        5 months ago        4.15MB
$ docker tag alpine IP:18444/alpine
$ docker push IP:18444/alpine
The push refers to repository [IP:18444/alpine]
cd7100a72410: Layer already exists 
latest: digest: sha256:8c03bb07a531c53ad7d0f6e7041b64d81f99c6e493cb39abba56d956b40eacbc size: 528
$ docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
alpine                               latest              3fd9065eaf02        5 months ago        4.15MB
IP:18444/alpine            latest              3fd9065eaf02        5 months ago        4.15MB
$ docker pull IP:18443/alpine
Using default tag: latest
latest: Pulling from alpine
Digest: sha256:8c03bb07a531c53ad7d0f6e7041b64d81f99c6e493cb39abba56d956b40eacbc
Status: Image is up to date for IP:18443/alpine:latest
$ docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
alpine                               latest              3fd9065eaf02        5 months ago        4.15MB
IP:18443/alpine            latest              3fd9065eaf02        5 months ago        4.15MB
IP:18444/alpine            latest              3fd9065eaf02        5 months ago        4.15MB

完成。


免責聲明!

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



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