• 五、Docker仓库之https的registry搭建(二)


    本节我们分享更多的实用的仓库搭建使用方式。包括支持https的registry、Harbor、nexus。

    支持HTTPS的registry

    准备站点证书(测试使用,用一个自己的测试域名,配置本地hosts)
    如果你拥有一个公网域名,那很好,你很牛。当然也可以使用 openssl 自行签发证书。
    这里我计划将要搭建的私有仓库地址为 docker.margu.com,下面介绍使用 openssl 自行签发 docker.margu.com 的站点 SSL 证书。
    1、创建 CA 私钥

    [root@k8s-m1 ~]# openssl genrsa -out "root-ca.key" 2048
    Generating RSA private key, 2048 bit long modulus
    
    • 1
    • 2

    2、利用私钥创建 CA 根证书请求文件

    [root@k8s-m1 ~]# openssl req  -new -key "root-ca.key"  -out "root-ca.csr" -sha256  -subj '/C=CN/ST=sichuang/L=chengdu/O=margu/CN=docker.margu.com'
    [root@k8s-m1 ~]# ll root-ca.csr 
    -rw-r--r-- 1 root root 989 May 22 21:54 root-ca.csr
    
    • 1
    • 2
    • 3

    以上命令中 -subj 参数里的 /C 表示国家,如 CN;/ST 表示省;/L 表示城市或者地区;/O 表示组织名;/CN 通用名称。

    3、配置 CA 根证书,新建 root-ca.cnf

    [root@k8s-m1 ~]#vim root-ca.cnf
    [root_ca]
    basicConstraints = critical,CA:TRUE,pathlen:1
    keyUsage = critical, nonRepudiation, cRLSign, keyCertSign
    subjectKeyIdentifier=hash
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4、签发根证书

    [root@k8s-m1 ~]# openssl x509 -req  -days 3650  -in "root-ca.csr"  -signkey "root-ca.key" -sha256 -out "root-ca.crt" -extfile "root-ca.cnf" -extensions root_ca
    Signature ok
    subject=/C=CN/ST=sichuang/L=chengdu/O=margu/CN=docker.margu.com
    Getting Private key
    
    • 1
    • 2
    • 3
    • 4

    5、生成站点 SSL 私钥

    [root@k8s-m1 ~]#  openssl genrsa -out "docker.margu.com.key" 2048
    Generating RSA private key, 2048 bit long modulus
    .....................................................................................................................+++
    .........................................................+++
    e is 65537 (0x10001)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    6、使用私钥生成证书请求文件

    [root@k8s-m1 ~]# openssl req -new -key "docker.margu.com.key" -out "site.csr" -sha256  -subj '/C=CN/ST=sichuan/L=chengdu/O=margu/CN=docker.margu.com'
    
    • 1

    7、配置证书,新建 site.cnf 文件

    [root@k8s-m1 ~]#vim site.cnf
    [server]
    authorityKeyIdentifier=keyid,issuer
    basicConstraints = critical,CA:FALSE
    extendedKeyUsage=serverAuth
    keyUsage = critical, digitalSignature, keyEncipherment
    subjectAltName = DNS:docker.domain.com, IP:192.168.2.140
    subjectKeyIdentifier=hash
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    8、签署站点 SSL 证书

    [root@k8s-m1 ~]# openssl x509 -req -days 750 -in "site.csr" -sha256     -CA "root-ca.crt" -CAkey "root-ca.key"  -CAcreateserial     -out "docker.margu.com.crt" -extfile "site.cnf" -extensions server
    Signature ok
    subject=/C=CN/ST=sichuan/L=chengdu/O=margu/CN=docker.margu.com
    Getting CA Private Key
    
    • 1
    • 2
    • 3
    • 4

    这样已经拥有了 docker.domain.com 的网站 SSL 私钥 docker.domain.com.key 和 SSL 证书 docker.domain.com.crt 及 CA 根证书 root-ca.crt。
    新建 /root/registry/ssl文件夹并将 docker.domain.com.key docker.domain.com.crt root-ca.crt 这三个文件移入,其他文件也可以移进去。

    9、配置私有仓库

    [root@k8s-m1 ~]# mkdir -p /root/registry/ssl
    [root@k8s-m1 ~]# mv  docker.margu.com.key docker.margu.com.crt root-ca.crt  /root/registry/ssl
    [root@k8s-m1 ~]# docker ps
    CONTAINER ID   IMAGE                                               COMMAND                  CREATED          STATUS          PORTS                    NAMES
    d7e4249bc561   registry                                            "/entrypoint.sh /etc…"   44 minutes ago   Up 24 minutes   0.0.0.0:5000->5000/tcp   registry
    [root@k8s-m1 ~]# docker exec -it d7e /bin/sh
    [root@k8s-m1 auth]#  docker exec -it d7e /bin/sh
    / # ls -al /etc/docker/registry/config.yml
    -rw-rw-r--    1 root     root           295 Nov 13  2021 /etc/docker/registry/config.yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    私有仓库默认的配置文件位于 /etc/docker/registry/config.yml,我们先在本地编辑 config.yml,之后挂载到容器中使用。

    version: 0.1
    log:
      accesslog:
        disabled: true
      level: debug
      formatter: text
      fields:
        service: registry
        environment: staging
    storage:
      delete:
        enabled: true
      cache:
        blobdescriptor: inmemory
      filesystem:
        rootdirectory: /var/lib/registry
    auth:
      htpasswd:
        realm: basic-realm
        path: /etc/docker/registry/auth/registry
    http:
      addr: :443
      host: https://docker.margu.com
      headers:
        X-Content-Type-Options: [nosniff]
      http2:
        disabled: false
      tls:
        certificate: /etc/docker/registry/ssl/docker.margu.com.crt
        key: /etc/docker/registry/ssl/docker.margu.com.key
    health:
      storagedriver:
        enabled: true
        interval: 10s
    threshold: 3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    10、生成 http 认证文件

    [root@k8s-m1 ~]#mkdir -p /root/registry/auth
    [root@k8s-m1 ~]#docker run --rm     --entrypoint htpasswd     httpd:alpine   -Bbn margu 123456 > /root/registry/auth/registry
    
    • 1
    • 2

    将上面的 username password 替换为自己想用的用户名和密码。
    11、添加编辑 docker-compose.yml

    version: '3'
    services:
      registry:
        image: registry
        ports:
          - "443:443"
        volumes:
          - /root/registry:/etc/docker/registry
          - registry-data:/var/lib/registry
    volumes:
      registry-data:
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    放在registry目录下
    注意最后的所有文件路径:

    [root@k8s-m1 registry]# tree
    .
    ├── auth
    │   └── registry
    ├── config.yml
    ├── docker-compose.yml
    └── ssl
        ├── docker.margu.com.crt
        ├── docker.margu.com.key
        ├── root-ca.crt
        ├── site.cnf
        └── ssl.conf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    修改 hosts
    往/etc/hosts中添加:
    192.168.2.140 docker.margu.com
    启动

    [root@k8s-m1 ~]#docker-compose up -d
    
    • 1

    这样我们就搭建好了一个具有权限认证、TLS 的私有仓库,接下来我们测试其功能是否正常。
    测试私有仓库功能。
    由于自行签发的 CA 根证书不被系统信任,所以我们需要将 CA 根证书 ssl/root-ca.crt 移入 /etc/docker/certs.d/docker.margu.com 文件夹中。

    [root@k8s-m1 ~]# mkdir -p /etc/docker/certs.d/docker.margu.com
    
    [root@k8s-m1 ~]#cp /root/registry/ssl/root-ca.crt /etc/docker/certs.d/docker.margu.com/ca.crt
    
    • 1
    • 2
    • 3

    登录到私有仓库。

    [root@k8s-m1 ~]#docker login docker.margu.com
    
    • 1

    尝试推送、拉取镜像。

    [root@k8s-m1 ssl]# docker tag  registry:latest  docker.margu.com/registry:latest
    [root@k8s-m1 ssl]# docker push docker.margu.com/registry:latest 
    The push refers to repository [docker.margu.com/registry]
    aeccf26589a7: Layer already exists 
    f640be0d5aad: Layer already exists 
    aa4330046b37: Pushed 
    ad10b481abe7: Layer already exists 
    69715584ec78: Layer already exists 
    latest: digest: sha256:36cb5b157911061fb610d8884dc09e0b0300a767a350563cbfd88b4b85324ce4 size: 1363
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    如果我们退出登录,尝试推送镜像。

    [root@k8s-m1 ssl]# docker logout  docker.margu.com
    Removing login credentials for docker.margu.com
    [root@k8s-m1 ssl]# docker push docker.margu.com/registry:latest 
    The push refers to repository [docker.margu.com/registry]
    aeccf26589a7: Preparing 
    f640be0d5aad: Preparing 
    aa4330046b37: Preparing 
    ad10b481abe7: Preparing 
    69715584ec78: Preparing 
    no basic auth credentials
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    发现会提示没有登录,不能将镜像推送到私有仓库中。

    web界面登录也是正常的。
    在这里插入图片描述

    由于篇幅原因,其他方式的搭建请看其他章节。

  • 相关阅读:
    Qt的一个无边界窗口公共类
    六、【常用工具组】
    【数据集NO.5】小目标检测数据集汇总
    Tableau可视化项目
    初识Linux:目录&路径
    .9图片详解
    IntelliJ IDEA、.NET 工具变贵,JetBrains 宣布全家桶涨价!
    (一)Neo4j下载安装以及初次使用
    NOIP2023模拟19联测40 诡异键盘
    基于SpringBoot体育用品购物商城-协同过滤推荐算法项目源代码
  • 原文地址:https://blog.csdn.net/margu_168/article/details/130816097